From 6cfc9182f45c250d5b6d38155f7e3e3da4e3ad55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Tue, 9 Jul 2019 21:41:57 +0200 Subject: [PATCH] added the drop column statement for user.preferences and tested migrations --- ...igrate_users_table_preferences_columns.php | 39 +++++++++---------- ...9_212000_change_users_drop_preferences.php | 4 +- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php b/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php index c95c10654..f4f97351f 100644 --- a/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php +++ b/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php @@ -18,28 +18,27 @@ return [ $db = $builder->getConnection(); $db->table('users') + ->select(['id', 'preferences']) ->whereNotNull('preferences') ->orderBy('id') - ->chunk(50, function (Collection $users) use ($db) { - $users->each(function ($user) use ($db) { - collect(json_decode(Arr::get($user, 'preferences', '{}'))) - ->each(function ($value, $key) use ($user, $db) { - if ($key === 'discloses_online') { - $db->table('users') - ->where('id', $user['id']) - ->update(['discloses_online' => (bool) $value]); - } - if (preg_match('/^notify_(?[^_]+)_(?.*)$/', $key, $matches)) { - $db->table('notification_preferences') - ->insert([ - 'user_id' => $user['id'], - 'type' => $matches['type'], - 'channel' => $matches['channel'], - 'enabled' => (bool) $value - ]); - } - }); - }); + ->each(function ($user) use ($db) { + collect(json_decode($user->preferences ?? '{}')) + ->each(function ($value, $key) use ($user, $db) { + if ($key === 'discloseOnline') { + $db->table('users') + ->where('id', $user->id) + ->update(['disclose_online' => (bool) $value]); + } + if (preg_match('/^notify_(?[^_]+)_(?.*)$/', $key, $matches)) { + $db->table('notification_preferences') + ->insert([ + 'user_id' => $user->id, + 'type' => $matches['type'], + 'channel' => $matches['channel'], + 'enabled' => (bool) $value + ]); + } + }); }); }, diff --git a/migrations/2019_07_09_212000_change_users_drop_preferences.php b/migrations/2019_07_09_212000_change_users_drop_preferences.php index 5335e72b6..8c1dfdbf5 100644 --- a/migrations/2019_07_09_212000_change_users_drop_preferences.php +++ b/migrations/2019_07_09_212000_change_users_drop_preferences.php @@ -14,13 +14,13 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { - $schema->table('posts', function (Blueprint $table) { + $schema->table('users', function (Blueprint $table) { $table->dropColumn('preferences'); }); }, 'down' => function (Builder $schema) { - $schema->table('posts', function (Blueprint $table) { + $schema->table('users', function (Blueprint $table) { $table->binary('preferences')->nullable(); }); }