1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 07:24:27 +02:00
Files
php-flarum/src/Database/Migration.php
Toby Zerner 1031826a3d Apply fixes from StyleCI
[ci skip] [skip ci]
2016-11-29 05:03:53 +00:00

115 lines
3.4 KiB
PHP
Executable File

<?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.
*/
namespace Flarum\Database;
use Flarum\Settings\SettingsRepositoryInterface;
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);
});
}
];
}
/**
* Add default values for config values.
*/
public static function addSettings($defaults)
{
return [
'up' => function (SettingsRepositoryInterface $settings) use ($defaults) {
foreach ($defaults as $key => $value) {
$settings->set($key, $value);
}
},
'down' => function (SettingsRepositoryInterface $settings) use ($defaults) {
foreach (array_keys($defaults) as $key) {
$settings->delete($key);
}
}
];
}
}