From 14042bedaf2475ea75ec66fa3aae3e2d70cd82f6 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 25 Feb 2016 00:50:03 +0900 Subject: [PATCH] Add some handy shortcuts for typical migration tasks This will make it much easier for extension developers (and also less error-prone) to create migrations for things like creating tables, renaming columns and so on... --- framework/core/composer.json | 2 +- framework/core/src/Database/Migration.php | 95 +++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 framework/core/src/Database/Migration.php diff --git a/framework/core/composer.json b/framework/core/composer.json index f70dc74c3..99cfbdb54 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -26,7 +26,7 @@ "illuminate/config": "5.1.*", "illuminate/container": "5.1.*", "illuminate/contracts": "5.1.*", - "illuminate/database": "5.1.*", + "illuminate/database": "^5.1.31", "illuminate/events": "5.1.*", "illuminate/filesystem": "5.1.*", "illuminate/hashing": "5.1.*", diff --git a/framework/core/src/Database/Migration.php b/framework/core/src/Database/Migration.php new file mode 100755 index 000000000..d7ca234ac --- /dev/null +++ b/framework/core/src/Database/Migration.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Database; + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Schema\Builder; + +/** + * Migration factory. + * + * Implements some handy shortcuts for creating typical migrations. + */ +abstract class Migration +{ + /** + * Create a table. + */ + public static function createTable($name, callable $definition) + { + return [ + 'up' => function (Builder $schema) use ($name, $definition) { + $schema->create($name, $definition); + }, + 'down' => function (Builder $schema) use ($name) { + $schema->drop($name); + } + ]; + } + + /** + * Rename a table. + */ + public static function renameTable($from, $to) + { + return [ + 'up' => function (Builder $schema) use ($from, $to) { + $schema->rename($from, $to); + }, + + 'down' => function (Builder $schema) use ($from, $to) { + $schema->rename($to, $from); + } + ]; + } + + /** + * Add columns to a table. + */ + public static function addColumns($tableName, array $columnDefinitions) + { + return [ + 'up' => function (Builder $schema) use ($tableName, $columnDefinitions) { + $schema->table($tableName, function (Blueprint $table) use ($columnDefinitions) { + foreach ($columnDefinitions as $columnName => $options) { + $type = array_shift($options); + $table->addColumn($type, $columnName, $options); + } + }); + }, + 'down' => function (Builder $schema) use ($tableName, $columnDefinitions) { + $schema->table($tableName, function (Blueprint $table) use ($columnDefinitions) { + $table->dropColumn(array_keys($columnDefinitions)); + }); + } + ]; + } + + /** + * Rename a column. + */ + public static function renameColumn($tableName, $from, $to) + { + return [ + 'up' => function (Builder $schema) use ($tableName, $from, $to) { + $schema->table($tableName, function (Blueprint $table) use ($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); + }); + } + ]; + } +}