Clean up migrations

* Make filenames and order more consistent

* Split foreign keys into their own migrations, add statements to ensure
  data integrity prior to adding them

* Add renameColumns helper, use other helpers where possible
This commit is contained in:
Toby Zerner 2018-07-21 15:23:37 +09:30
parent 39501f1dd0
commit 33973fdb78
32 changed files with 412 additions and 295 deletions

View File

@ -19,19 +19,27 @@ return [
$table->renameColumn('lifetime', 'lifetime_seconds'); $table->renameColumn('lifetime', 'lifetime_seconds');
$table->renameColumn('last_activity', 'last_activity_at'); $table->renameColumn('last_activity', 'last_activity_at');
$table->dateTime('created_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) { 'down' => function (Builder $schema) {
$schema->table('access_tokens', function (Blueprint $table) { $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('lifetime_seconds', 'lifetime');
$table->renameColumn('last_activity_at', 'last_activity'); $table->renameColumn('last_activity_at', 'last_activity');
$table->dropColumn('created_at'); $table->dropColumn('created_at');
$table->renameColumn('token', 'id'); $table->integer('user_id')->change();
$table->dropForeign(['user_id']);
}); });
} }
]; ];

View File

@ -14,14 +14,23 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $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) { 'down' => function (Builder $schema) {
$schema->table('access_tokens', function (Blueprint $table) { $schema->table('access_tokens', function (Blueprint $table) {
$table->integer('last_activity_at')->change(); $table->dropForeign(['user_id']);
}); });
} }
]; ];

View File

@ -1,29 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
});
}
];

View File

@ -14,6 +14,12 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $schema->table('api_keys', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('allowed_ips')->nullable(); $table->string('allowed_ips')->nullable();
@ -31,5 +37,11 @@ return [
$table->dropForeign(['user_id']); $table->dropForeign(['user_id']);
$table->dropColumn('id', 'allowed_ips', 'user_id', 'scopes', 'created_at'); $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');
});
} }
]; ];

View File

@ -9,19 +9,6 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Illuminate\Database\Schema\Blueprint; use Flarum\Database\Migration;
use Illuminate\Database\Schema\Builder;
return [ return Migration::renameColumn('registration_tokens', 'id', 'token');
'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');
});
}
];

View File

@ -1,56 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);
});
}
];

View File

@ -0,0 +1,25 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);
});
}
];

View File

@ -11,4 +11,4 @@
use Flarum\Database\Migration; use Flarum\Database\Migration;
return Migration::renameTable('users_discussions', 'discussions_users'); return Migration::renameTable('users_discussions', 'discussion_user');

View File

@ -0,0 +1,17 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);

View File

@ -1,34 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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']);
});
}
];

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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']);
});
}
];

View File

@ -0,0 +1,14 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');

View File

@ -14,17 +14,22 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { 'up' => function (Builder $schema) {
$schema->table('email_tokens', function (Blueprint $table) { // Delete rows with non-existent users so that we will be able to create
$table->renameColumn('id', 'token'); // 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'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}); });
}, },
'down' => function (Builder $schema) { 'down' => function (Builder $schema) {
$schema->table('email_tokens', function (Blueprint $table) { $schema->table('email_tokens', function (Blueprint $table) {
$table->renameColumn('token', 'id');
$table->dropForeign(['user_id']); $table->dropForeign(['user_id']);
}); });
} }

View File

@ -14,6 +14,15 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $schema->table('group_permission', function (Blueprint $table) {
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade'); $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
}); });

View File

@ -14,6 +14,18 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $schema->table('group_user', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade'); $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');

View File

@ -9,21 +9,16 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Flarum\Database\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
return [ return Migration::createTable(
'up' => function (Builder $schema) { 'notifications_from',
$schema->create('notifications_from', function (Blueprint $table) { function (Blueprint $table) {
$table->integer('id')->unsigned(); $table->integer('id')->unsigned();
$table->integer('from_user_id')->unsigned(); $table->integer('from_user_id')->unsigned();
$table->foreign('id')->references('id')->on('notifications')->onDelete('cascade'); $table->foreign('id')->references('id')->on('notifications')->onDelete('cascade');
$table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('from_user_id')->references('id')->on('users')->onDelete('cascade');
});
},
'down' => function (Builder $schema) {
$schema->drop('notifications_from');
} }
]; );

View File

@ -13,16 +13,19 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { 'up' => function (Builder $schema) {
$schema->getConnection()->table('notifications')->chunkById(100, function ($notifications) use ($schema) { $query = $schema->getConnection()->table('notifications')
foreach ($notifications as $notification) { ->whereExists(function ($query) {
$insert = [ $query->selectRaw(1)->from('users')->whereRaw('id = sender_id');
'id' => $notification->id, });
'from_user_id' => $notification->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) { 'down' => function (Builder $schema) {

View File

@ -21,8 +21,6 @@ return [
$table->timestamp('read_at')->nullable(); $table->timestamp('read_at')->nullable();
$table->timestamp('deleted_at')->nullable(); $table->timestamp('deleted_at')->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}); });
$schema->getConnection()->table('notifications') $schema->getConnection()->table('notifications')
@ -48,13 +46,12 @@ return [
$table->boolean('is_read'); $table->boolean('is_read');
$table->boolean('is_deleted'); $table->boolean('is_deleted');
$table->dropForeign(['user_id']);
}); });
$schema->getConnection()->table('notifications') $schema->getConnection()->table('notifications')
->whereNotNull('read_at') ->whereNotNull('read_at')
->update(['is_read' => 1]); ->update(['is_read' => 1]);
$schema->getConnection()->table('notifications') $schema->getConnection()->table('notifications')
->whereNotNull('deleted_at') ->whereNotNull('deleted_at')
->update(['is_deleted' => 1]); ->update(['is_deleted' => 1]);

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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']);
});
}
];

View File

@ -14,6 +14,15 @@ use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $schema->table('password_tokens', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
}); });

View File

@ -0,0 +1,20 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);

View File

@ -9,21 +9,29 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder; use Illuminate\Database\Schema\Builder;
return [ return [
'up' => function (Builder $schema) { '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) { $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('user_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('edited_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'); $table->foreign('hidden_user_id')->references('id')->on('users')->onDelete('set null');
@ -32,15 +40,6 @@ return [
'down' => function (Builder $schema) { 'down' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) { $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([ $table->dropForeign([
'user_id', 'discussion_id', 'user_id', 'discussion_id',
'edited_user_id', 'hidden_user_id' 'edited_user_id', 'hidden_user_id'

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
);

View File

@ -1,29 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
];

View File

@ -1,41 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
});
}
];

View File

@ -0,0 +1,23 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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'
]);

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
);

View File

@ -1,29 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* 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');
}
];

View File

@ -78,16 +78,28 @@ abstract class Migration
* Rename a column. * Rename a column.
*/ */
public static function renameColumn($tableName, $from, $to) public static function renameColumn($tableName, $from, $to)
{
return static::renameColumns($tableName, [$from => $to]);
}
/**
* Rename multiple columns.
*/
public static function renameColumns($tableName, array $columnNames)
{ {
return [ return [
'up' => function (Builder $schema) use ($tableName, $from, $to) { 'up' => function (Builder $schema) use ($tableName, $columnNames) {
$schema->table($tableName, function (Blueprint $table) use ($from, $to) { $schema->table($tableName, function (Blueprint $table) use ($columnNames) {
$table->renameColumn($from, $to); foreach ($columnNames as $from => $to) {
$table->renameColumn($from, $to);
}
}); });
}, },
'down' => function (Builder $schema) use ($tableName, $from, $to) { 'down' => function (Builder $schema) use ($tableName, $columnNames) {
$schema->table($tableName, function (Blueprint $table) use ($from, $to) { $schema->table($tableName, function (Blueprint $table) use ($columnNames) {
$table->renameColumn($to, $from); foreach ($columnNames as $to => $from) {
$table->renameColumn($from, $to);
}
}); });
} }
]; ];