From 95ea40a7c4db6c5fbd4a2465072832e5dbb3759c Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 20 Mar 2016 22:40:42 +0900 Subject: [PATCH] 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. :-) --- ...04_000000_add_default_like_permissions.php | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/extensions/likes/migrations/2015_09_04_000000_add_default_like_permissions.php b/extensions/likes/migrations/2015_09_04_000000_add_default_like_permissions.php index 02d1cca83..4f2c6fe44 100644 --- a/extensions/likes/migrations/2015_09_04_000000_add_default_like_permissions.php +++ b/extensions/likes/migrations/2015_09_04_000000_add_default_like_permissions.php @@ -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, - 'permission' => 'discussion.likePosts', - ]; -}; +$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(); } ];