Do not use model classes in the migrations

The model classes encapsulate knowledge about the database that
may change. That knowledge may be table names, or the value of
constants (such as `Group::MEMBER_ID` in this case).

Models may even disappear after a while due to refactorings.

In all these cases, this migration would break. Thus, we use
Laravel's query builder features which makes building queries
almost as easy as using the model - but correct in all cases. :-)
This commit is contained in:
Franz Liedke 2016-03-20 22:40:42 +09:00
parent 10e929f2dc
commit 95ea40a7c4

View File

@ -8,27 +8,23 @@
* file that was distributed with this source code.
*/
use Flarum\Core\Group;
use Flarum\Core\Permission;
use Illuminate\Database\ConnectionInterface;
$getPermissionAttributes = function () {
return [
'group_id' => Group::MEMBER_ID,
$permissionAttributes = [
'group_id' => 3, // Default group ID of members
'permission' => 'discussion.likePosts',
];
};
];
return [
'up' => function () use ($getPermissionAttributes) {
Permission::unguard();
'up' => function (ConnectionInterface $db) use ($permissionAttributes) {
$instance = $db->table('permissions')->where($permissionAttributes)->first();
$permission = Permission::firstOrNew($getPermissionAttributes());
$permission->save();
if (is_null($instance)) {
$db->table('permissions')->insert($permissionAttributes);
}
},
'down' => function () use ($getPermissionAttributes) {
Permission::where($getPermissionAttributes())->delete();
'down' => function (ConnectionInterface $db) use ($permissionAttributes) {
$db->table('permissions')->where($permissionAttributes)->delete();
}
];