Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #17 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixes not-raw SQL statement for migrations #16
  • Loading branch information
DarkGhostHunter authored Jul 28, 2021
2 parents b427aeb + 890cf1a commit 8695a09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,22 +427,7 @@ Laraconfig uses one single bag called `default`. If you have declared in the man
```php
// app/Models/User.php
/**
* Returns the bags this model uses for settings.
*
* @return array|string
*/
public function getSettingsBags(): array|string
{
$bags = ['notifications'];
if ($this->is_premium) {
$bags[] = 'theme';
}
return $bags;
}
i
```
The above will apply a filter to the query when retrieving settings from the database. This makes easy to swap bags when a user has a different role or property, or programmatically.
Expand Down
24 changes: 13 additions & 11 deletions src/Migrator/Pipes/CreateNewMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

/**
* @internal
Expand Down Expand Up @@ -145,13 +146,14 @@ protected function fillSettings(Metadata $metadata, Collection $models): int
foreach ($models as $model) {
$affected += Setting::query()->insertUsing(
['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'],
$model->newQuery()->select([
$metadata->getKey(). ' as metadata_id',
$model->getKeyName(). ' as settable_id',
$model->getMorphClass(). ' as settable_type',
$metadata->getRawOriginal('default', 'NULL').' as value',
$this->now->toDateTimeString(). ' as created_at',
$this->now->toDateTimeString(). ' as updated_at',
$model->newQuery()
->select([
DB::raw("'{$metadata->getKey()}' as metadata_id"),
DB::raw("{$model->getKeyName()} as settable_id"),
DB::raw("'{$model->getMorphClass()}' as settable_type"),
DB::raw("'{$metadata->getRawOriginal('default', 'NULL')}' as value"),
DB::raw("'{$this->now->toDateTimeString()}' as created_at"),
DB::raw("'{$this->now->toDateTimeString()}' as updated_at"),
])->getQuery()
);
}
Expand All @@ -175,12 +177,12 @@ protected function copySettings(Metadata $new, Metadata $old): int
['metadata_id', 'settable_id', 'settable_type', 'value', 'created_at', 'updated_at'],
Setting::query()->where('metadata_id', $old->getKey())
->select([
$new->getKey(). ' as metadata_id',
DB::raw("'{$new->getKey()}' as metadata_id"),
'settable_id',
'settable_type',
'value', // Here we will just instruct to copy the value raw to the new setting.
$this->now->toDateTimeString(). ' as created_at',
$this->now->toDateTimeString(). ' as updated_at',
DB::raw("'{$this->now->toDateTimeString()}' as created_at"),
DB::raw("'{$this->now->toDateTimeString()}' as updated_at"),
])->getQuery()
);
}
Expand Down Expand Up @@ -216,4 +218,4 @@ protected function migrateSettings(Declaration $declaration, Metadata $new, Meta

return $affected;
}
}
}

0 comments on commit 8695a09

Please sign in to comment.