From a3c6833b54af705ff139743630aeccb194e8ad47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Klabbers?= Date: Tue, 12 Jun 2018 21:33:17 +0200 Subject: [PATCH] migrating user preferences obviously works on empty table --- ..._create_notification_preferences_table.php | 30 +++++++++++ ...create_users_table_preferences_columns.php | 27 ++++++++++ ...igrate_users_table_preferences_columns.php | 51 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 framework/core/migrations/2018_01_30_222800_create_notification_preferences_table.php create mode 100644 framework/core/migrations/2018_01_30_222800_create_users_table_preferences_columns.php create mode 100644 framework/core/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php diff --git a/framework/core/migrations/2018_01_30_222800_create_notification_preferences_table.php b/framework/core/migrations/2018_01_30_222800_create_notification_preferences_table.php new file mode 100644 index 000000000..17f1b8b62 --- /dev/null +++ b/framework/core/migrations/2018_01_30_222800_create_notification_preferences_table.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + $schema->create('notification_preferences', function (Blueprint $table) { + $table->integer('user_id')->unsigned(); + $table->string('type'); + $table->string('channel'); + $table->boolean('enabled')->default(false); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->drop('notification_preferences'); + } +]; diff --git a/framework/core/migrations/2018_01_30_222800_create_users_table_preferences_columns.php b/framework/core/migrations/2018_01_30_222800_create_users_table_preferences_columns.php new file mode 100644 index 000000000..6fa9b5c93 --- /dev/null +++ b/framework/core/migrations/2018_01_30_222800_create_users_table_preferences_columns.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->boolean('disclose_online')->default(false); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('users', function (Blueprint $table) { + $table->dropColumn('disclose_online'); + }); + } +]; diff --git a/framework/core/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php b/framework/core/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php new file mode 100644 index 000000000..c95c10654 --- /dev/null +++ b/framework/core/migrations/2018_01_30_222801_migrate_users_table_preferences_columns.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Schema\Builder; +use Illuminate\Support\Arr; + +return [ + 'up' => function (Builder $builder) { + $db = $builder->getConnection(); + + $db->table('users') + ->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 + ]); + } + }); + }); + }); + }, + + 'down' => function (Builder $builder) { + $db = $builder->getConnection(); + + $db->table('notification_preferences')->truncate(); + } +];