Skip to content

Commit 2da8cb5

Browse files
authored
Feat: add exists in migrator (#289)
* Add exists in Migrator * Fix README
1 parent f22f59c commit 2da8cb5

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,19 @@ public function up(): void
410410
}
411411
```
412412

413+
#### Checking a property if it exists
414+
415+
There might be times when you want to check if a property exists in the database. This can be done as such:
416+
417+
```php
418+
public function up(): void
419+
{
420+
if ($this->migrator->exists('general.timezone')) {
421+
// do something
422+
}
423+
}
424+
```
425+
413426
#### Operations in group
414427

415428
When you're working on a big settings class with many properties, it can be a bit cumbersome always to have to prepend the settings group. That's why you can also perform operations within a settings group:

src/Migrations/SettingsMigrator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public function decrypt(string $property): void
114114
$this->update($property, fn ($payload) => Crypto::decrypt($payload));
115115
}
116116

117+
public function exists(string $property): bool
118+
{
119+
return $this->checkIfPropertyExists($property);
120+
}
121+
117122
public function inGroup(string $group, Closure $closure): void
118123
{
119124
$closure(new SettingsBlueprint($group, $this));

tests/SettingsMigratorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@
100100
$this->settingsMigrator->update('user.name', fn (string $name) => 'Ruben Van Assche');
101101
})->throws(SettingDoesNotExist::class);
102102

103+
it('can check if a setting exists', function () {
104+
$this->settingsMigrator->add('settings.exists', true);
105+
106+
expect($this->settingsMigrator->exists('settings.exists'))->toBeTrue();
107+
});
108+
109+
it('can check if a setting does not exists', function () {
110+
expect($this->settingsMigrator->exists('settings.does_not_exists'))->toBeFalse();
111+
});
112+
103113
it('can perform migrations within a group', function () {
104114
$this->settingsMigrator->inGroup('test', function (SettingsBlueprint $blueprint): void {
105115
$blueprint->add('a', 'Alpha');

0 commit comments

Comments
 (0)