diff --git a/framework/core/migrations/2018_01_11_093900_change_access_tokens_b8_columns.php b/framework/core/migrations/2018_01_11_093900_change_access_tokens_columns.php similarity index 67% rename from framework/core/migrations/2018_01_11_093900_change_access_tokens_b8_columns.php rename to framework/core/migrations/2018_01_11_093900_change_access_tokens_columns.php index 4e4965d3b..a27bad442 100644 --- a/framework/core/migrations/2018_01_11_093900_change_access_tokens_b8_columns.php +++ b/framework/core/migrations/2018_01_11_093900_change_access_tokens_columns.php @@ -19,19 +19,27 @@ return [ $table->renameColumn('lifetime', 'lifetime_seconds'); $table->renameColumn('last_activity', 'last_activity_at'); $table->dateTime('created_at'); + $table->integer('user_id')->unsigned()->change(); + }); - $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + // Use a separate schema instance because this column gets renamed + // in the first one. + $schema->table('access_tokens', function (Blueprint $table) { + $table->dateTime('last_activity_at')->change(); }); }, 'down' => function (Builder $schema) { $schema->table('access_tokens', function (Blueprint $table) { + $table->integer('last_activity_at')->change(); + }); + + $schema->table('access_tokens', function (Blueprint $table) { + $table->renameColumn('token', 'id'); $table->renameColumn('lifetime_seconds', 'lifetime'); $table->renameColumn('last_activity_at', 'last_activity'); $table->dropColumn('created_at'); - $table->renameColumn('token', 'id'); - - $table->dropForeign(['user_id']); + $table->integer('user_id')->change(); }); } ]; diff --git a/framework/core/migrations/2018_01_11_094800_change_access_tokens_make_activity_at_column_datetime.php b/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php similarity index 53% rename from framework/core/migrations/2018_01_11_094800_change_access_tokens_make_activity_at_column_datetime.php rename to framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php index 3e555f1ca..b6125506d 100644 --- a/framework/core/migrations/2018_01_11_094800_change_access_tokens_make_activity_at_column_datetime.php +++ b/framework/core/migrations/2018_01_11_094000_change_access_tokens_add_foreign_keys.php @@ -14,14 +14,23 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + // Delete rows with non-existent users so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('access_tokens') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->delete(); + $schema->table('access_tokens', function (Blueprint $table) { - $table->dateTime('last_activity_at')->change(); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); }, 'down' => function (Builder $schema) { $schema->table('access_tokens', function (Blueprint $table) { - $table->integer('last_activity_at')->change(); + $table->dropForeign(['user_id']); }); } ]; diff --git a/framework/core/migrations/2018_01_11_094900_change_api_keys_rename_id_to_key.php b/framework/core/migrations/2018_01_11_094900_change_api_keys_rename_id_to_key.php deleted file mode 100644 index 7135025a7..000000000 --- a/framework/core/migrations/2018_01_11_094900_change_api_keys_rename_id_to_key.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * 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('api_keys', function (Blueprint $table) { - $table->renameColumn('id', 'key'); - $table->dropPrimary(['id', 'key']); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('api_keys', function (Blueprint $table) { - $table->renameColumn('key', 'id'); - $table->primary('id'); - }); - } -]; diff --git a/framework/core/migrations/2018_01_11_095000_change_api_keys_b8_columns.php b/framework/core/migrations/2018_01_11_095000_change_api_keys_columns.php similarity index 73% rename from framework/core/migrations/2018_01_11_095000_change_api_keys_b8_columns.php rename to framework/core/migrations/2018_01_11_095000_change_api_keys_columns.php index 478c578cf..eaa6246b4 100644 --- a/framework/core/migrations/2018_01_11_095000_change_api_keys_b8_columns.php +++ b/framework/core/migrations/2018_01_11_095000_change_api_keys_columns.php @@ -14,6 +14,12 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + $schema->table('api_keys', function (Blueprint $table) { + $table->dropPrimary(['id']); + $table->renameColumn('id', 'key'); + $table->unique('key'); + }); + $schema->table('api_keys', function (Blueprint $table) { $table->increments('id'); $table->string('allowed_ips')->nullable(); @@ -31,5 +37,11 @@ return [ $table->dropForeign(['user_id']); $table->dropColumn('id', 'allowed_ips', 'user_id', 'scopes', 'created_at'); }); + + $schema->table('api_keys', function (Blueprint $table) { + $table->dropUnique(['key']); + $table->renameColumn('key', 'id'); + $table->primary('id'); + }); } ]; diff --git a/framework/core/migrations/2018_01_11_102000_change_registration_tokens_rename_id_to_token.php b/framework/core/migrations/2018_01_11_102000_change_registration_tokens_rename_id_to_token.php index 6a538622b..3a630513e 100644 --- a/framework/core/migrations/2018_01_11_102000_change_registration_tokens_rename_id_to_token.php +++ b/framework/core/migrations/2018_01_11_102000_change_registration_tokens_rename_id_to_token.php @@ -9,19 +9,6 @@ * file that was distributed with this source code. */ -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Database\Schema\Builder; +use Flarum\Database\Migration; -return [ - 'up' => function (Builder $schema) { - $schema->table('registration_tokens', function (Blueprint $table) { - $table->renameColumn('id', 'token'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('registration_tokens', function (Blueprint $table) { - $table->renameColumn('token', 'id'); - }); - } -]; +return Migration::renameColumn('registration_tokens', 'id', 'token'); diff --git a/framework/core/migrations/2018_01_11_155200_change_discussions_b8_columns.php b/framework/core/migrations/2018_01_11_155200_change_discussions_b8_columns.php deleted file mode 100644 index 6485fdcab..000000000 --- a/framework/core/migrations/2018_01_11_155200_change_discussions_b8_columns.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * 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('discussions', function (Blueprint $table) { - $table->renameColumn('comments_count', 'comment_count'); - $table->renameColumn('participants_count', 'participant_count'); - $table->renameColumn('number_index', 'post_number_index'); - $table->renameColumn('start_time', 'created_at'); - $table->renameColumn('start_user_id', 'user_id'); - $table->renameColumn('start_post_id', 'first_post_id'); - $table->renameColumn('last_time', 'last_posted_at'); - $table->renameColumn('last_user_id', 'last_posted_user_id'); - $table->renameColumn('hide_time', 'hidden_at'); - $table->renameColumn('hide_user_id', 'hidden_user_id'); - - $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); - $table->foreign('last_posted_user_id')->references('id')->on('users')->onDelete('set null'); - $table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null'); - $table->foreign('first_post_id')->references('id')->on('posts'); - $table->foreign('last_post_id')->references('id')->on('posts'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('discussions', function (Blueprint $table) { - $table->renameColumn('comment_count', 'comments_count'); - $table->renameColumn('participant_count', 'participants_count'); - $table->renameColumn('post_number_index', 'number_index'); - $table->renameColumn('created_at', 'start_time'); - $table->renameColumn('user_id', 'start_user_id'); - $table->renameColumn('first_post_id', 'start_post_id'); - $table->renameColumn('last_posted_at', 'last_time'); - $table->renameColumn('last_posted_user_id', 'last_user_id'); - $table->renameColumn('hidden_at', 'hide_time'); - $table->renameColumn('hidden_user_id', 'hide_user_id'); - - $table->dropForeign([ - 'user_id', 'last_posted_user_id', 'hidden_user_id', - 'first_post_id', 'last_post_id' - ]); - }); - } -]; diff --git a/framework/core/migrations/2018_01_11_155200_change_discussions_rename_columns.php b/framework/core/migrations/2018_01_11_155200_change_discussions_rename_columns.php new file mode 100644 index 000000000..3d4e7467d --- /dev/null +++ b/framework/core/migrations/2018_01_11_155200_change_discussions_rename_columns.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameColumns('discussions', [ + 'comments_count' => 'comment_count', + 'participants_count' => 'participant_count', + 'number_index' => 'post_number_index', + 'start_time' => 'created_at', + 'start_user_id' => 'user_id', + 'start_post_id' => 'first_post_id', + 'last_time' => 'last_posted_at', + 'last_user_id' => 'last_posted_user_id', + 'hide_time' => 'hidden_at', + 'hide_user_id' => 'hidden_user_id' +]); diff --git a/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php new file mode 100644 index 000000000..6cf2adc44 --- /dev/null +++ b/framework/core/migrations/2018_01_11_155300_change_discussions_add_foreign_keys.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Query\Expression; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +return [ + 'up' => function (Builder $schema) { + // Set non-existent entity IDs to NULL so that we will be able to create + // foreign keys without any issues. + $connection = $schema->getConnection(); + + $selectId = function ($table, $column) use ($connection) { + return new Expression( + '('.$connection->table($table)->whereRaw("id = $column")->select('id')->toSql().')' + ); + }; + + $connection->table('discussions')->update([ + 'user_id' => $selectId('users', 'user_id'), + 'last_posted_user_id' => $selectId('users', 'last_posted_user_id'), + 'hidden_user_id' => $selectId('users', 'hidden_user_id'), + 'first_post_id' => $selectId('posts', 'first_post_id'), + 'last_post_id' => $selectId('posts', 'last_post_id'), + ]); + + $schema->table('discussions', function (Blueprint $table) { + $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); + $table->foreign('last_posted_user_id')->references('id')->on('users')->onDelete('set null'); + $table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null'); + $table->foreign('first_post_id')->references('id')->on('posts'); + $table->foreign('last_post_id')->references('id')->on('posts'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('discussions', function (Blueprint $table) { + $table->dropForeign([ + 'user_id', 'last_posted_user_id', 'hidden_user_id', + 'first_post_id', 'last_post_id' + ]); + }); + } +]; diff --git a/framework/core/migrations/2018_01_15_071700_change_user_discussions_discussions_users.php b/framework/core/migrations/2018_01_15_071700_rename_users_discussions_to_discussion_user.php similarity index 77% rename from framework/core/migrations/2018_01_15_071700_change_user_discussions_discussions_users.php rename to framework/core/migrations/2018_01_15_071700_rename_users_discussions_to_discussion_user.php index e1ac5e026..eaa51709d 100644 --- a/framework/core/migrations/2018_01_15_071700_change_user_discussions_discussions_users.php +++ b/framework/core/migrations/2018_01_15_071700_rename_users_discussions_to_discussion_user.php @@ -11,4 +11,4 @@ use Flarum\Database\Migration; -return Migration::renameTable('users_discussions', 'discussions_users'); +return Migration::renameTable('users_discussions', 'discussion_user'); diff --git a/framework/core/migrations/2018_01_15_071800_change_discussion_user_rename_columns.php b/framework/core/migrations/2018_01_15_071800_change_discussion_user_rename_columns.php new file mode 100644 index 000000000..5fff7ea1e --- /dev/null +++ b/framework/core/migrations/2018_01_15_071800_change_discussion_user_rename_columns.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameColumns('discussion_user', [ + 'read_time' => 'last_read_at', + 'read_number' => 'last_read_post_number' +]); diff --git a/framework/core/migrations/2018_01_15_071800_change_discussions_users_b8_columns.php b/framework/core/migrations/2018_01_15_071800_change_discussions_users_b8_columns.php deleted file mode 100644 index a0176f29d..000000000 --- a/framework/core/migrations/2018_01_15_071800_change_discussions_users_b8_columns.php +++ /dev/null @@ -1,34 +0,0 @@ - - * - * 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('discussions_users', function (Blueprint $table) { - $table->renameColumn('read_time', 'last_read_at'); - $table->renameColumn('read_number', 'last_read_post_number'); - - $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); - $table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('discussions_users', function (Blueprint $table) { - $table->renameColumn('last_read_at', 'read_time'); - $table->renameColumn('last_read_post_number', 'read_number'); - - $table->dropForeign(['users_user_id', 'users_discussion_id']); - }); - } -]; diff --git a/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php b/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php new file mode 100644 index 000000000..4a9d69a07 --- /dev/null +++ b/framework/core/migrations/2018_01_15_071900_change_discussion_user_add_foreign_keys.php @@ -0,0 +1,40 @@ + + * + * 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) { + // Delete rows with non-existent entities so that we will be able to create + // foreign keys without any issues. + $connection = $schema->getConnection(); + $connection->table('discussion_user') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->orWhereNotExists(function ($query) { + $query->selectRaw(1)->from('discussions')->whereRaw('id = discussion_id'); + }) + ->delete(); + + $schema->table('discussion_user', function (Blueprint $table) { + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + $table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('discussion_user', function (Blueprint $table) { + $table->dropForeign(['user_id', 'discussion_id']); + }); + } +]; diff --git a/framework/core/migrations/2018_01_15_072600_change_email_tokens_rename_id_to_token.php b/framework/core/migrations/2018_01_15_072600_change_email_tokens_rename_id_to_token.php new file mode 100644 index 000000000..64586a4aa --- /dev/null +++ b/framework/core/migrations/2018_01_15_072600_change_email_tokens_rename_id_to_token.php @@ -0,0 +1,14 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameColumn('email_tokens', 'id', 'token'); diff --git a/framework/core/migrations/2018_01_15_072600_change_email_tokens_b8_columns.php b/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php similarity index 66% rename from framework/core/migrations/2018_01_15_072600_change_email_tokens_b8_columns.php rename to framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php index afcd3306f..c840045e6 100644 --- a/framework/core/migrations/2018_01_15_072600_change_email_tokens_b8_columns.php +++ b/framework/core/migrations/2018_01_15_072700_change_email_tokens_add_foreign_keys.php @@ -14,17 +14,22 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { - $schema->table('email_tokens', function (Blueprint $table) { - $table->renameColumn('id', 'token'); + // Delete rows with non-existent users so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('email_tokens') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->delete(); + $schema->table('email_tokens', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); }, 'down' => function (Builder $schema) { $schema->table('email_tokens', function (Blueprint $table) { - $table->renameColumn('token', 'id'); - $table->dropForeign(['user_id']); }); } diff --git a/framework/core/migrations/2018_01_18_130400_rename_permissions_to_groups_permissions.php b/framework/core/migrations/2018_01_18_130400_rename_permissions_to_group_permission.php similarity index 100% rename from framework/core/migrations/2018_01_18_130400_rename_permissions_to_groups_permissions.php rename to framework/core/migrations/2018_01_18_130400_rename_permissions_to_group_permission.php diff --git a/framework/core/migrations/2018_07_19_101301_constraints_group_permission.php b/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php similarity index 66% rename from framework/core/migrations/2018_07_19_101301_constraints_group_permission.php rename to framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php index 0f3092f64..f783547a6 100644 --- a/framework/core/migrations/2018_07_19_101301_constraints_group_permission.php +++ b/framework/core/migrations/2018_01_18_130500_change_group_permission_add_foreign_keys.php @@ -14,6 +14,15 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + // Delete rows with non-existent groups so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('group_permission') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('groups')->whereRaw('id = group_id'); + }) + ->delete(); + $schema->table('group_permission', function (Blueprint $table) { $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade'); }); diff --git a/framework/core/migrations/2018_01_18_130400_rename_users_groups_to_group_user.php b/framework/core/migrations/2018_01_18_130600_rename_users_groups_to_group_user.php similarity index 100% rename from framework/core/migrations/2018_01_18_130400_rename_users_groups_to_group_user.php rename to framework/core/migrations/2018_01_18_130600_rename_users_groups_to_group_user.php diff --git a/framework/core/migrations/2018_07_19_101300_constraints_group_user.php b/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php similarity index 61% rename from framework/core/migrations/2018_07_19_101300_constraints_group_user.php rename to framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php index 9f11a9f2a..028b35e94 100644 --- a/framework/core/migrations/2018_07_19_101300_constraints_group_user.php +++ b/framework/core/migrations/2018_01_18_130700_change_group_user_add_foreign_keys.php @@ -14,6 +14,18 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + // Delete rows with non-existent entities so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('group_user') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->orWhereNotExists(function ($query) { + $query->selectRaw(1)->from('groups')->whereRaw('id = group_id'); + }) + ->delete(); + $schema->table('group_user', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade'); diff --git a/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php b/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php index 63fa38c8b..9ee23e2ed 100644 --- a/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php +++ b/framework/core/migrations/2018_01_18_132900_create_notifications_from_table.php @@ -9,21 +9,16 @@ * file that was distributed with this source code. */ +use Flarum\Database\Migration; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Database\Schema\Builder; -return [ - 'up' => function (Builder $schema) { - $schema->create('notifications_from', function (Blueprint $table) { - $table->integer('id')->unsigned(); - $table->integer('from_user_id')->unsigned(); +return Migration::createTable( + 'notifications_from', + function (Blueprint $table) { + $table->integer('id')->unsigned(); + $table->integer('from_user_id')->unsigned(); - $table->foreign('id')->references('id')->on('notifications')->onDelete('cascade'); - $table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->drop('notifications_from'); + $table->foreign('id')->references('id')->on('notifications')->onDelete('cascade'); + $table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade'); } -]; +); diff --git a/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php b/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php index c12fd4c22..c58ba7df2 100644 --- a/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php +++ b/framework/core/migrations/2018_01_18_132901_seed_notifications_from_table.php @@ -13,16 +13,19 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { - $schema->getConnection()->table('notifications')->chunkById(100, function ($notifications) use ($schema) { - foreach ($notifications as $notification) { - $insert = [ - 'id' => $notification->id, - 'from_user_id' => $notification->sender_id - ]; + $query = $schema->getConnection()->table('notifications') + ->whereExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = sender_id'); + }); - $schema->getConnection()->table('notifications_from')->updateOrInsert($insert, $insert); - } - }); + foreach ($query->cursor() as $notification) { + $insert = [ + 'id' => $notification->id, + 'from_user_id' => $notification->sender_id + ]; + + $schema->getConnection()->table('notifications_from')->updateOrInsert($insert, $insert); + } }, 'down' => function (Builder $schema) { diff --git a/framework/core/migrations/2018_01_18_133000_change_notifications_b8_columns.php b/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php similarity index 93% rename from framework/core/migrations/2018_01_18_133000_change_notifications_b8_columns.php rename to framework/core/migrations/2018_01_18_133000_change_notifications_columns.php index 01f2e34d6..963844b02 100644 --- a/framework/core/migrations/2018_01_18_133000_change_notifications_b8_columns.php +++ b/framework/core/migrations/2018_01_18_133000_change_notifications_columns.php @@ -21,8 +21,6 @@ return [ $table->timestamp('read_at')->nullable(); $table->timestamp('deleted_at')->nullable(); - - $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); $schema->getConnection()->table('notifications') @@ -48,13 +46,12 @@ return [ $table->boolean('is_read'); $table->boolean('is_deleted'); - - $table->dropForeign(['user_id']); }); $schema->getConnection()->table('notifications') ->whereNotNull('read_at') ->update(['is_read' => 1]); + $schema->getConnection()->table('notifications') ->whereNotNull('deleted_at') ->update(['is_deleted' => 1]); diff --git a/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php new file mode 100644 index 000000000..c71327d10 --- /dev/null +++ b/framework/core/migrations/2018_01_18_133100_change_notifications_add_foreign_keys.php @@ -0,0 +1,36 @@ + + * + * 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) { + // Delete rows with non-existent users so that we will be able to create + // foreign keys without any issues. + $schema->getConnection() + ->table('notifications') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->delete(); + + $schema->table('notifications', function (Blueprint $table) { + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + }); + }, + + 'down' => function (Builder $schema) { + $schema->table('notifications', function (Blueprint $table) { + $table->dropForeign(['user_id']); + }); + } +]; diff --git a/framework/core/migrations/2018_07_19_101301_constraints_password_tokens.php b/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php similarity index 65% rename from framework/core/migrations/2018_07_19_101301_constraints_password_tokens.php rename to framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php index b5f499f87..7e3c3c6d4 100644 --- a/framework/core/migrations/2018_07_19_101301_constraints_password_tokens.php +++ b/framework/core/migrations/2018_01_18_134500_change_password_tokens_add_foreign_keys.php @@ -14,6 +14,15 @@ use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + // Delete rows with non-existent users so that we will be able to create + // foreign keys without any issues. + $connection = $schema->getConnection(); + $connection->table('password_tokens') + ->whereNotExists(function ($query) { + $query->selectRaw(1)->from('users')->whereRaw('id = user_id'); + }) + ->delete(); + $schema->table('password_tokens', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); diff --git a/framework/core/migrations/2018_01_18_135000_change_posts_rename_columns.php b/framework/core/migrations/2018_01_18_135000_change_posts_rename_columns.php new file mode 100644 index 000000000..701c58aa1 --- /dev/null +++ b/framework/core/migrations/2018_01_18_135000_change_posts_rename_columns.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameColumns('posts', [ + 'time' => 'created_at', + 'edit_time' => 'edited_at', + 'hide_time' => 'hidden_at', + 'edit_user_id' => 'edited_user_id', + 'hide_user_id' => 'hidden_user_id' +]); diff --git a/framework/core/migrations/2018_01_18_135000_change_posts_b8_columns.php b/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php similarity index 57% rename from framework/core/migrations/2018_01_18_135000_change_posts_b8_columns.php rename to framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php index bc7d57271..2a0623052 100644 --- a/framework/core/migrations/2018_01_18_135000_change_posts_b8_columns.php +++ b/framework/core/migrations/2018_01_18_135100_change_posts_add_foreign_keys.php @@ -9,21 +9,29 @@ * file that was distributed with this source code. */ +use Illuminate\Database\Query\Expression; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; return [ 'up' => function (Builder $schema) { + // Set non-existent entity IDs to NULL so that we will be able to create + // foreign keys without any issues. + $connection = $schema->getConnection(); + + $selectId = function ($table, $column) use ($connection) { + return new Expression( + '('.$connection->table($table)->whereRaw("id = $column")->select('id')->toSql().')' + ); + }; + + $connection->table('posts')->update([ + 'user_id' => $selectId('users', 'user_id'), + 'edited_user_id' => $selectId('users', 'edited_user_id'), + 'hidden_user_id' => $selectId('users', 'hidden_user_id'), + ]); + $schema->table('posts', function (Blueprint $table) { - $table->renameColumn('time', 'created_at'); - $table->renameColumn('edit_time', 'edited_at'); - $table->renameColumn('hide_time', 'hidden_at'); - - $table->renameColumn('edit_user_id', 'edited_user_id'); - $table->renameColumn('hide_user_id', 'hidden_user_id'); - - $table->longText('content')->change(); - $table->foreign('user_id')->references('id')->on('users')->onDelete('set null'); $table->foreign('edited_user_id')->references('id')->on('users')->onDelete('set null'); $table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null'); @@ -32,15 +40,6 @@ return [ 'down' => function (Builder $schema) { $schema->table('posts', function (Blueprint $table) { - $table->renameColumn('created_at', 'time'); - $table->renameColumn('edited_at', 'edit_time'); - $table->renameColumn('hidden_at', 'hide_time'); - - $table->renameColumn('edited_user_id', 'edit_user_id'); - $table->renameColumn('edited_user_id', 'hidden_user_id'); - - $table->mediumText('content')->change(); - $table->dropForeign([ 'user_id', 'discussion_id', 'edited_user_id', 'hidden_user_id' diff --git a/framework/core/migrations/2018_01_30_220100_create_post_user_table.php b/framework/core/migrations/2018_01_30_220100_create_post_user_table.php new file mode 100644 index 000000000..577ce3511 --- /dev/null +++ b/framework/core/migrations/2018_01_30_220100_create_post_user_table.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; +use Illuminate\Database\Schema\Blueprint; + +return Migration::createTable( + 'post_user', + function (Blueprint $table) { + $table->integer('post_id')->unsigned(); + $table->integer('user_id')->unsigned(); + + $table->primary(['post_id', 'user_id']); + + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + } +); diff --git a/framework/core/migrations/2018_01_30_220100_create_posts_users_table.php b/framework/core/migrations/2018_01_30_220100_create_posts_users_table.php deleted file mode 100644 index ba1d3e952..000000000 --- a/framework/core/migrations/2018_01_30_220100_create_posts_users_table.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * 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('posts_users', function (Blueprint $table) { - $table->integer('post_id')->unsigned(); - $table->integer('user_id')->unsigned(); - - $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); - $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->drop('posts_users'); - } -]; diff --git a/framework/core/migrations/2018_01_30_222900_change_users_b8_columns.php b/framework/core/migrations/2018_01_30_222900_change_users_b8_columns.php deleted file mode 100644 index e7bb77c9e..000000000 --- a/framework/core/migrations/2018_01_30_222900_change_users_b8_columns.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * 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->renameColumn('is_activated', 'is_email_confirmed'); - $table->renameColumn('join_time', 'joined_at'); - $table->renameColumn('last_seen_time', 'last_seen_at'); - $table->renameColumn('discussions_count', 'discussion_count'); - $table->renameColumn('comments_count', 'comment_count'); - $table->renameColumn('read_time', 'marked_all_as_read_at'); - $table->renameColumn('notifications_read_time', 'read_notifications_at'); - $table->renameColumn('avatar_path', 'avatar_url'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->table('users', function (Blueprint $table) { - $table->renameColumn('is_email_confirmed', 'is_activated'); - $table->renameColumn('joined_at', 'join_time'); - $table->renameColumn('last_seen_at', 'last_seen_time'); - $table->renameColumn('discussion_count', 'discussions_count'); - $table->renameColumn('comment_count', 'comments_count'); - $table->renameColumn('marked_all_as_read_at', 'read_time'); - $table->renameColumn('read_notifications_at', 'notifications_read_time'); - $table->renameColumn('avatar_url', 'avatar_path'); - }); - } -]; diff --git a/framework/core/migrations/2018_01_30_222900_change_users_rename_columns.php b/framework/core/migrations/2018_01_30_222900_change_users_rename_columns.php new file mode 100644 index 000000000..4b6965bde --- /dev/null +++ b/framework/core/migrations/2018_01_30_222900_change_users_rename_columns.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; + +return Migration::renameColumns('users', [ + 'is_activated' => 'is_email_confirmed', + 'join_time' => 'joined_at', + 'last_seen_time' => 'last_seen_at', + 'discussions_count' => 'discussion_count', + 'comments_count' => 'comment_count', + 'read_time' => 'marked_all_as_read_at', + 'notifications_read_time' => 'read_notifications_at', + 'avatar_path' => 'avatar_url' +]); diff --git a/framework/core/migrations/2018_01_30_223700_create_user_user_table.php b/framework/core/migrations/2018_01_30_223700_create_user_user_table.php new file mode 100644 index 000000000..60c845ff6 --- /dev/null +++ b/framework/core/migrations/2018_01_30_223700_create_user_user_table.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Flarum\Database\Migration; +use Illuminate\Database\Schema\Blueprint; + +return Migration::createTable( + 'user_user', + function (Blueprint $table) { + $table->integer('user_id')->unsigned(); + $table->integer('other_user_id')->unsigned(); + + $table->foreign('user_id')->references('id')->on('posts')->onDelete('cascade'); + $table->foreign('other_user_id')->references('id')->on('users')->onDelete('cascade'); + } +); diff --git a/framework/core/migrations/2018_01_30_223700_create_users_users_table.php b/framework/core/migrations/2018_01_30_223700_create_users_users_table.php deleted file mode 100644 index ed74651c1..000000000 --- a/framework/core/migrations/2018_01_30_223700_create_users_users_table.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * 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('users_users', function (Blueprint $table) { - $table->integer('user_id')->unsigned(); - $table->integer('other_user_id')->unsigned(); - - $table->foreign('user_id')->references('id')->on('posts')->onDelete('cascade'); - $table->foreign('other_user_id')->references('id')->on('users')->onDelete('cascade'); - }); - }, - - 'down' => function (Builder $schema) { - $schema->drop('users_users'); - } -]; diff --git a/framework/core/src/Database/Migration.php b/framework/core/src/Database/Migration.php index 79332813d..6469b847f 100644 --- a/framework/core/src/Database/Migration.php +++ b/framework/core/src/Database/Migration.php @@ -78,16 +78,28 @@ abstract class Migration * Rename a column. */ public static function renameColumn($tableName, $from, $to) + { + return static::renameColumns($tableName, [$from => $to]); + } + + /** + * Rename multiple columns. + */ + public static function renameColumns($tableName, array $columnNames) { return [ - 'up' => function (Builder $schema) use ($tableName, $from, $to) { - $schema->table($tableName, function (Blueprint $table) use ($from, $to) { - $table->renameColumn($from, $to); + 'up' => function (Builder $schema) use ($tableName, $columnNames) { + $schema->table($tableName, function (Blueprint $table) use ($columnNames) { + foreach ($columnNames as $from => $to) { + $table->renameColumn($from, $to); + } }); }, - 'down' => function (Builder $schema) use ($tableName, $from, $to) { - $schema->table($tableName, function (Blueprint $table) use ($from, $to) { - $table->renameColumn($to, $from); + 'down' => function (Builder $schema) use ($tableName, $columnNames) { + $schema->table($tableName, function (Blueprint $table) use ($columnNames) { + foreach ($columnNames as $to => $from) { + $table->renameColumn($from, $to); + } }); } ];