Revamp migration structure

They are now simply files that return an array of closures, for
running the named "up" and "down" actions, respectively.

Related to #732.
This commit is contained in:
Franz Liedke 2016-02-24 22:23:49 +09:00
parent 61a0523e12
commit 98546801bc
21 changed files with 172 additions and 277 deletions

View File

@ -8,25 +8,20 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateAccessTokensTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('access_tokens', function (Blueprint $table) {
{
$this->schema->create('access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->timestamp('created_at'); $table->timestamp('created_at');
$table->timestamp('expires_at'); $table->timestamp('expires_at');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('access_tokens');
$this->schema->drop('access_tokens');
} }
} ];

View File

@ -8,22 +8,17 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateApiKeysTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('api_keys', function (Blueprint $table) {
{
$this->schema->create('api_keys', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('api_keys');
$this->schema->drop('api_keys');
} }
} ];

View File

@ -8,23 +8,18 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateConfigTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('config', function (Blueprint $table) {
{
$this->schema->create('config', function (Blueprint $table) {
$table->string('key', 100)->primary(); $table->string('key', 100)->primary();
$table->binary('value')->nullable(); $table->binary('value')->nullable();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('config');
$this->schema->drop('config');
} }
} ];

View File

@ -8,16 +8,12 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateDiscussionsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('discussions', function (Blueprint $table) {
{
$this->schema->create('discussions', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('title', 200); $table->string('title', 200);
$table->integer('comments_count')->unsigned()->default(0); $table->integer('comments_count')->unsigned()->default(0);
@ -33,10 +29,9 @@ class CreateDiscussionsTable extends AbstractMigration
$table->integer('last_post_id')->unsigned()->nullable(); $table->integer('last_post_id')->unsigned()->nullable();
$table->integer('last_post_number')->unsigned()->nullable(); $table->integer('last_post_number')->unsigned()->nullable();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('discussions');
$this->schema->drop('discussions');
} }
} ];

View File

@ -8,25 +8,20 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateEmailTokensTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('email_tokens', function (Blueprint $table) {
{
$this->schema->create('email_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
$table->string('email', 150); $table->string('email', 150);
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->timestamp('created_at'); $table->timestamp('created_at');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('email_tokens');
$this->schema->drop('email_tokens');
} }
} ];

View File

@ -8,26 +8,21 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateGroupsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('groups', function (Blueprint $table) {
{
$this->schema->create('groups', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('name_singular', 100); $table->string('name_singular', 100);
$table->string('name_plural', 100); $table->string('name_plural', 100);
$table->string('color', 20)->nullable(); $table->string('color', 20)->nullable();
$table->string('icon', 100)->nullable(); $table->string('icon', 100)->nullable();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('groups');
$this->schema->drop('groups');
} }
} ];

View File

@ -8,16 +8,12 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateNotificationsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('notifications', function (Blueprint $table) {
{
$this->schema->create('notifications', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->integer('sender_id')->unsigned()->nullable(); $table->integer('sender_id')->unsigned()->nullable();
@ -29,10 +25,9 @@ class CreateNotificationsTable extends AbstractMigration
$table->boolean('is_read')->default(0); $table->boolean('is_read')->default(0);
$table->boolean('is_deleted')->default(0); $table->boolean('is_deleted')->default(0);
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('notifications');
$this->schema->drop('notifications');
} }
} ];

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreatePasswordTokensTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('password_tokens', function (Blueprint $table) {
{
$this->schema->create('password_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->timestamp('created_at'); $table->timestamp('created_at');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('password_tokens');
$this->schema->drop('password_tokens');
} }
} ];

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreatePermissionsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('permissions', function (Blueprint $table) {
{
$this->schema->create('permissions', function (Blueprint $table) {
$table->integer('group_id')->unsigned(); $table->integer('group_id')->unsigned();
$table->string('permission', 100); $table->string('permission', 100);
$table->primary(['group_id', 'permission']); $table->primary(['group_id', 'permission']);
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('permissions');
$this->schema->drop('permissions');
} }
} ];

View File

@ -8,16 +8,12 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreatePostsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('posts', function (Blueprint $table) {
{
$this->schema->create('posts', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->integer('discussion_id')->unsigned(); $table->integer('discussion_id')->unsigned();
$table->integer('number')->unsigned()->nullable(); $table->integer('number')->unsigned()->nullable();
@ -37,12 +33,11 @@ class CreatePostsTable extends AbstractMigration
$table->engine = 'MyISAM'; $table->engine = 'MyISAM';
}); });
$prefix = $this->schema->getConnection()->getTablePrefix(); $prefix = $schema->getConnection()->getTablePrefix();
$this->schema->getConnection()->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)'); $schema->getConnection()->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('posts');
$this->schema->drop('posts');
} }
} ];

View File

@ -8,26 +8,21 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateUsersDiscussionsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('users_discussions', function (Blueprint $table) {
{
$this->schema->create('users_discussions', function (Blueprint $table) {
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->integer('discussion_id')->unsigned(); $table->integer('discussion_id')->unsigned();
$table->dateTime('read_time')->nullable(); $table->dateTime('read_time')->nullable();
$table->integer('read_number')->unsigned()->nullable(); $table->integer('read_number')->unsigned()->nullable();
$table->primary(['user_id', 'discussion_id']); $table->primary(['user_id', 'discussion_id']);
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('users_discussions');
$this->schema->drop('users_discussions');
} }
} ];

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateUsersGroupsTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('users_groups', function (Blueprint $table) {
{
$this->schema->create('users_groups', function (Blueprint $table) {
$table->integer('user_id')->unsigned(); $table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned(); $table->integer('group_id')->unsigned();
$table->primary(['user_id', 'group_id']); $table->primary(['user_id', 'group_id']);
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('users_groups');
$this->schema->drop('users_groups');
} }
} ];

View File

@ -8,16 +8,12 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateUsersTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('users', function (Blueprint $table) {
{
$this->schema->create('users', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->string('username', 100)->unique(); $table->string('username', 100)->unique();
$table->string('email', 150)->unique(); $table->string('email', 150)->unique();
@ -33,10 +29,9 @@ class CreateUsersTable extends AbstractMigration
$table->integer('discussions_count')->unsigned()->default(0); $table->integer('discussions_count')->unsigned()->default(0);
$table->integer('comments_count')->unsigned()->default(0); $table->integer('comments_count')->unsigned()->default(0);
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('users');
$this->schema->drop('users');
} }
} ];

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CreateAuthTokensTable extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->create('auth_tokens', function (Blueprint $table) {
{
$this->schema->create('auth_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary(); $table->string('id', 100)->primary();
$table->string('payload', 150); $table->string('payload', 150);
$table->timestamp('created_at'); $table->timestamp('created_at');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->drop('auth_tokens');
$this->schema->drop('auth_tokens');
} }
} ];

View File

@ -8,25 +8,20 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class AddHideToDiscussions extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('discussions', function (Blueprint $table) {
{
$this->schema->table('discussions', function (Blueprint $table) {
$table->dateTime('hide_time')->nullable(); $table->dateTime('hide_time')->nullable();
$table->integer('hide_user_id')->unsigned()->nullable(); $table->integer('hide_user_id')->unsigned()->nullable();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('discussions', function (Blueprint $table) {
$this->schema->table('discussions', function (Blueprint $table) {
$table->dropColumn(['hide_time', 'hide_user_id']); $table->dropColumn(['hide_time', 'hide_user_id']);
}); });
} }
} ];

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class RenameNotificationReadTime extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('users', function (Blueprint $table) {
{
$this->schema->table('users', function (Blueprint $table) {
$table->renameColumn('notification_read_time', 'notifications_read_time'); $table->renameColumn('notification_read_time', 'notifications_read_time');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('users', function (Blueprint $table) {
$this->schema->table('users', function (Blueprint $table) {
$table->renameColumn('notifications_read_time', 'notification_read_time'); $table->renameColumn('notifications_read_time', 'notification_read_time');
}); });
} }
} ];

View File

@ -8,19 +8,14 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration; use Illuminate\Database\Schema\Builder;
use Flarum\Database\AbstractMigration; return [
'up' => function (Builder $schema) {
$schema->rename('config', 'settings');
},
class RenameConfigToSettings extends AbstractMigration 'down' => function (Builder $schema) {
{ $schema->rename('settings', 'config');
public function up()
{
$this->schema->rename('config', 'settings');
} }
];
public function down()
{
$this->schema->rename('settings', 'config');
}
}

View File

@ -8,24 +8,19 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class AddIpAddressToPosts extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('posts', function (Blueprint $table) {
{
$this->schema->table('posts', function (Blueprint $table) {
$table->string('ip_address', 45)->nullable(); $table->string('ip_address', 45)->nullable();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('posts', function (Blueprint $table) {
$this->schema->table('posts', function (Blueprint $table) {
$table->dropColumn(['ip_address']); $table->dropColumn(['ip_address']);
}); });
} }
} ];

View File

@ -1,26 +1,21 @@
<?php <?php
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class ChangeAccessTokensColumns extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('access_tokens', function (Blueprint $table) {
{
$this->schema->table('access_tokens', function (Blueprint $table) {
$table->string('id', 40)->change(); $table->string('id', 40)->change();
$table->dropColumn('created_at'); $table->dropColumn('created_at');
$table->dropColumn('expires_at'); $table->dropColumn('expires_at');
$table->integer('last_activity'); $table->integer('last_activity');
$table->integer('lifetime'); $table->integer('lifetime');
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('access_tokens', function (Blueprint $table) {
$this->schema->table('access_tokens', function (Blueprint $table) {
$table->string('id', 100)->change(); $table->string('id', 100)->change();
$table->dropColumn('last_activity'); $table->dropColumn('last_activity');
$table->dropColumn('lifetime'); $table->dropColumn('lifetime');
@ -28,4 +23,4 @@ class ChangeAccessTokensColumns extends AbstractMigration
$table->timestamp('expires_at'); $table->timestamp('expires_at');
}); });
} }
} ];

View File

@ -1,23 +1,18 @@
<?php <?php
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class ChangeSettingsValueColumnToText extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('settings', function (Blueprint $table) {
{
$this->schema->table('settings', function (Blueprint $table) {
$table->text('value')->change(); $table->text('value')->change();
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('settings', function (Blueprint $table) {
$this->schema->table('settings', function (Blueprint $table) {
$table->binary('value')->change(); $table->binary('value')->change();
}); });
} }
} ];

View File

@ -8,34 +8,29 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Flarum\Core\Migration;
use Flarum\Database\AbstractMigration;
use Flarum\Util\Str; use Flarum\Util\Str;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class AddSlugToDiscussions extends AbstractMigration return [
{ 'up' => function (Builder $schema) {
public function up() $schema->table('discussions', function (Blueprint $table) {
{
$this->schema->table('discussions', function (Blueprint $table) {
$table->string('slug'); $table->string('slug');
}); });
// Store slugs for existing discussions // Store slugs for existing discussions
$this->schema->getConnection()->table('discussions')->chunk(100, function ($discussions) { $schema->getConnection()->table('discussions')->chunk(100, function ($discussions) use ($schema) {
foreach ($discussions as $discussion) { foreach ($discussions as $discussion) {
$this->schema->getConnection()->table('discussions')->where('id', $discussion->id)->update([ $schema->getConnection()->table('discussions')->where('id', $discussion->id)->update([
'slug' => Str::slug($discussion->title) 'slug' => Str::slug($discussion->title)
]); ]);
} }
}); });
} },
public function down() 'down' => function (Builder $schema) {
{ $schema->table('discussions', function (Blueprint $table) {
$this->schema->table('discussions', function (Blueprint $table) {
$table->dropColumn('slug'); $table->dropColumn('slug');
}); });
} }
} ];