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 05fe4446bf
commit 51955504aa
21 changed files with 172 additions and 277 deletions

View File

@ -8,25 +8,20 @@
* 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\Builder;
class CreateAccessTokensTable extends AbstractMigration
{
public function up()
{
$this->schema->create('access_tokens', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('access_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->integer('user_id')->unsigned();
$table->timestamp('created_at');
$table->timestamp('expires_at');
});
}
},
public function down()
{
$this->schema->drop('access_tokens');
'down' => function (Builder $schema) {
$schema->drop('access_tokens');
}
}
];

View File

@ -8,22 +8,17 @@
* 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\Builder;
class CreateApiKeysTable extends AbstractMigration
{
public function up()
{
$this->schema->create('api_keys', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('api_keys', function (Blueprint $table) {
$table->string('id', 100)->primary();
});
}
},
public function down()
{
$this->schema->drop('api_keys');
'down' => function (Builder $schema) {
$schema->drop('api_keys');
}
}
];

View File

@ -8,23 +8,18 @@
* 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\Builder;
class CreateConfigTable extends AbstractMigration
{
public function up()
{
$this->schema->create('config', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('config', function (Blueprint $table) {
$table->string('key', 100)->primary();
$table->binary('value')->nullable();
});
}
},
public function down()
{
$this->schema->drop('config');
'down' => function (Builder $schema) {
$schema->drop('config');
}
}
];

View File

@ -8,16 +8,12 @@
* 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\Builder;
class CreateDiscussionsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('discussions', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('discussions', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 200);
$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_number')->unsigned()->nullable();
});
}
},
public function down()
{
$this->schema->drop('discussions');
'down' => function (Builder $schema) {
$schema->drop('discussions');
}
}
];

View File

@ -8,25 +8,20 @@
* 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\Builder;
class CreateEmailTokensTable extends AbstractMigration
{
public function up()
{
$this->schema->create('email_tokens', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('email_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('email', 150);
$table->integer('user_id')->unsigned();
$table->timestamp('created_at');
});
}
},
public function down()
{
$this->schema->drop('email_tokens');
'down' => function (Builder $schema) {
$schema->drop('email_tokens');
}
}
];

View File

@ -8,26 +8,21 @@
* 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\Builder;
class CreateGroupsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('groups', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name_singular', 100);
$table->string('name_plural', 100);
$table->string('color', 20)->nullable();
$table->string('icon', 100)->nullable();
});
}
},
public function down()
{
$this->schema->drop('groups');
'down' => function (Builder $schema) {
$schema->drop('groups');
}
}
];

View File

@ -8,16 +8,12 @@
* 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\Builder;
class CreateNotificationsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('notifications', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('sender_id')->unsigned()->nullable();
@ -29,10 +25,9 @@ class CreateNotificationsTable extends AbstractMigration
$table->boolean('is_read')->default(0);
$table->boolean('is_deleted')->default(0);
});
}
},
public function down()
{
$this->schema->drop('notifications');
'down' => function (Builder $schema) {
$schema->drop('notifications');
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class CreatePasswordTokensTable extends AbstractMigration
{
public function up()
{
$this->schema->create('password_tokens', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('password_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->integer('user_id')->unsigned();
$table->timestamp('created_at');
});
}
},
public function down()
{
$this->schema->drop('password_tokens');
'down' => function (Builder $schema) {
$schema->drop('password_tokens');
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class CreatePermissionsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('permissions', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('permissions', function (Blueprint $table) {
$table->integer('group_id')->unsigned();
$table->string('permission', 100);
$table->primary(['group_id', 'permission']);
});
}
},
public function down()
{
$this->schema->drop('permissions');
'down' => function (Builder $schema) {
$schema->drop('permissions');
}
}
];

View File

@ -8,16 +8,12 @@
* 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\Builder;
class CreatePostsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('posts', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('discussion_id')->unsigned();
$table->integer('number')->unsigned()->nullable();
@ -37,12 +33,11 @@ class CreatePostsTable extends AbstractMigration
$table->engine = 'MyISAM';
});
$prefix = $this->schema->getConnection()->getTablePrefix();
$this->schema->getConnection()->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');
}
$prefix = $schema->getConnection()->getTablePrefix();
$schema->getConnection()->statement('ALTER TABLE '.$prefix.'posts ADD FULLTEXT content (content)');
},
public function down()
{
$this->schema->drop('posts');
'down' => function (Builder $schema) {
$schema->drop('posts');
}
}
];

View File

@ -8,26 +8,21 @@
* 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\Builder;
class CreateUsersDiscussionsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('users_discussions', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('users_discussions', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('discussion_id')->unsigned();
$table->dateTime('read_time')->nullable();
$table->integer('read_number')->unsigned()->nullable();
$table->primary(['user_id', 'discussion_id']);
});
}
},
public function down()
{
$this->schema->drop('users_discussions');
'down' => function (Builder $schema) {
$schema->drop('users_discussions');
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class CreateUsersGroupsTable extends AbstractMigration
{
public function up()
{
$this->schema->create('users_groups', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('users_groups', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->primary(['user_id', 'group_id']);
});
}
},
public function down()
{
$this->schema->drop('users_groups');
'down' => function (Builder $schema) {
$schema->drop('users_groups');
}
}
];

View File

@ -8,16 +8,12 @@
* 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\Builder;
class CreateUsersTable extends AbstractMigration
{
public function up()
{
$this->schema->create('users', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username', 100)->unique();
$table->string('email', 150)->unique();
@ -33,10 +29,9 @@ class CreateUsersTable extends AbstractMigration
$table->integer('discussions_count')->unsigned()->default(0);
$table->integer('comments_count')->unsigned()->default(0);
});
}
},
public function down()
{
$this->schema->drop('users');
'down' => function (Builder $schema) {
$schema->drop('users');
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class CreateAuthTokensTable extends AbstractMigration
{
public function up()
{
$this->schema->create('auth_tokens', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->create('auth_tokens', function (Blueprint $table) {
$table->string('id', 100)->primary();
$table->string('payload', 150);
$table->timestamp('created_at');
});
}
},
public function down()
{
$this->schema->drop('auth_tokens');
'down' => function (Builder $schema) {
$schema->drop('auth_tokens');
}
}
];

View File

@ -8,25 +8,20 @@
* 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\Builder;
class AddHideToDiscussions extends AbstractMigration
{
public function up()
{
$this->schema->table('discussions', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$table->dateTime('hide_time')->nullable();
$table->integer('hide_user_id')->unsigned()->nullable();
});
}
},
public function down()
{
$this->schema->table('discussions', function (Blueprint $table) {
'down' => function (Builder $schema) {
$schema->table('discussions', function (Blueprint $table) {
$table->dropColumn(['hide_time', 'hide_user_id']);
});
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class RenameNotificationReadTime extends AbstractMigration
{
public function up()
{
$this->schema->table('users', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->table('users', function (Blueprint $table) {
$table->renameColumn('notification_read_time', 'notifications_read_time');
});
}
},
public function down()
{
$this->schema->table('users', function (Blueprint $table) {
'down' => function (Builder $schema) {
$schema->table('users', function (Blueprint $table) {
$table->renameColumn('notifications_read_time', 'notification_read_time');
});
}
}
];

View File

@ -8,19 +8,14 @@
* 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
{
public function up()
{
$this->schema->rename('config', 'settings');
'down' => function (Builder $schema) {
$schema->rename('settings', 'config');
}
public function down()
{
$this->schema->rename('settings', 'config');
}
}
];

View File

@ -8,24 +8,19 @@
* 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\Builder;
class AddIpAddressToPosts extends AbstractMigration
{
public function up()
{
$this->schema->table('posts', function (Blueprint $table) {
return [
'up' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) {
$table->string('ip_address', 45)->nullable();
});
}
},
public function down()
{
$this->schema->table('posts', function (Blueprint $table) {
'down' => function (Builder $schema) {
$schema->table('posts', function (Blueprint $table) {
$table->dropColumn(['ip_address']);
});
}
}
];

View File

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

View File

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

View File

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