Skip to content

Commit

Permalink
Feat: add exists in migrator (#289)
Browse files Browse the repository at this point in the history
* Add exists in Migrator

* Fix README
  • Loading branch information
akshit-arora authored Sep 20, 2024
1 parent f22f59c commit 2da8cb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,19 @@ public function up(): void
}
```

#### Checking a property if it exists

There might be times when you want to check if a property exists in the database. This can be done as such:

```php
public function up(): void
{
if ($this->migrator->exists('general.timezone')) {
// do something
}
}
```

#### Operations in group

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:
Expand Down
5 changes: 5 additions & 0 deletions src/Migrations/SettingsMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public function decrypt(string $property): void
$this->update($property, fn ($payload) => Crypto::decrypt($payload));
}

public function exists(string $property): bool
{
return $this->checkIfPropertyExists($property);
}

public function inGroup(string $group, Closure $closure): void
{
$closure(new SettingsBlueprint($group, $this));
Expand Down
10 changes: 10 additions & 0 deletions tests/SettingsMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@
$this->settingsMigrator->update('user.name', fn (string $name) => 'Ruben Van Assche');
})->throws(SettingDoesNotExist::class);

it('can check if a setting exists', function () {
$this->settingsMigrator->add('settings.exists', true);

expect($this->settingsMigrator->exists('settings.exists'))->toBeTrue();
});

it('can check if a setting does not exists', function () {
expect($this->settingsMigrator->exists('settings.does_not_exists'))->toBeFalse();
});

it('can perform migrations within a group', function () {
$this->settingsMigrator->inGroup('test', function (SettingsBlueprint $blueprint): void {
$blueprint->add('a', 'Alpha');
Expand Down

0 comments on commit 2da8cb5

Please sign in to comment.