/
Migration.php
190 lines (169 loc) · 5.54 KB
/
Migration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Database;
use Flarum\Settings\DatabaseSettingsRepository;
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, function (Blueprint $table) use ($definition) {
$definition($table);
});
},
'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));
});
}
];
}
/**
* Drop columns from a table.
*/
public static function dropColumns($tableName, array $columnDefinitions)
{
$inverse = static::addColumns($tableName, $columnDefinitions);
return [
'up' => $inverse['down'],
'down' => $inverse['up']
];
}
/**
* Rename a column.
*/
public static function renameColumn($tableName, $from, $to)
{
return static::renameColumns($tableName, [$from => $to]);
}
/**
* Rename multiple columns.
*/
public static function renameColumns($tableName, array $columnNames)
{
return [
'up' => function (Builder $schema) use ($tableName, $columnNames) {
$schema->table($tableName, function (Blueprint $table) use ($columnNames) {
foreach ($columnNames as $from => $to) {
$table->renameColumn($from, $to);
}
});
},
'down' => function (Builder $schema) use ($tableName, $columnNames) {
$schema->table($tableName, function (Blueprint $table) use ($columnNames) {
foreach ($columnNames as $to => $from) {
$table->renameColumn($from, $to);
}
});
}
];
}
/**
* Add default values for config values.
*/
public static function addSettings(array $defaults)
{
return [
'up' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach ($defaults as $key => $value) {
$settings->set($key, $value);
}
},
'down' => function (Builder $schema) use ($defaults) {
$settings = new DatabaseSettingsRepository(
$schema->getConnection()
);
foreach (array_keys($defaults) as $key) {
$settings->delete($key);
}
}
];
}
/**
* Add default permissions.
*/
public static function addPermissions(array $permissions)
{
$rows = [];
foreach ($permissions as $permission => $groups) {
foreach ((array) $groups as $group) {
$rows[] = [
'group_id' => $group,
'permission' => $permission,
];
}
}
return [
'up' => function (Builder $schema) use ($rows) {
$db = $schema->getConnection();
foreach ($rows as $row) {
if ($db->table('group_permission')->where($row)->exists()) {
continue;
}
if ($db->table('groups')->where('id', $row['group_id'])->doesntExist()) {
continue;
}
$db->table('group_permission')->insert($row);
}
},
'down' => function (Builder $schema) use ($rows) {
$db = $schema->getConnection();
foreach ($rows as $row) {
$db->table('group_permission')->where($row)->delete();
}
}
];
}
}