forked from phpbb/phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_generator.php
More file actions
347 lines (305 loc) · 8.12 KB
/
schema_generator.php
File metadata and controls
347 lines (305 loc) · 8.12 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration;
use Closure;
use LogicException;
use phpbb\config\config;
use phpbb\db\driver\driver_interface;
use phpbb\db\migrator;
use phpbb\db\tools\tools_interface;
use UnexpectedValueException;
use CHItA\TopologicalSort\TopologicalSort;
/**
* The schema generator generates the schema based on the existing migrations
*/
class schema_generator
{
use TopologicalSort;
/** @var config */
protected $config;
/** @var driver_interface */
protected $db;
/** @var tools_interface */
protected $db_tools;
/** @var array */
protected $class_names;
/** @var string */
protected $table_prefix;
/** @var string */
protected $phpbb_root_path;
/** @var string */
protected $php_ext;
/** @var array */
protected $tables;
/** @var array */
protected $table_names;
/**
* Constructor
* @param array $class_names
* @param config $config
* @param driver_interface $db
* @param tools_interface $db_tools
* @param string $phpbb_root_path
* @param string $php_ext
* @param string $table_prefix
* @param array $tables
*/
public function __construct(
array $class_names,
config $config,
driver_interface $db,
tools_interface $db_tools,
string $phpbb_root_path,
string $php_ext,
string $table_prefix,
array $tables)
{
$this->config = $config;
$this->db = $db;
$this->db_tools = $db_tools;
$this->class_names = $class_names;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
$this->table_prefix = $table_prefix;
$this->table_names = $tables;
}
/**
* Loads all migrations and their application state from the database.
*
* @return array An array describing the database schema.
*
* @throws UnexpectedValueException If a migration tries to use an undefined schema change.
* @throws UnexpectedValueException If a dependency can't be resolved or there are circular
* dependencies between migrations.
*/
public function get_schema() : array
{
if (!empty($this->tables))
{
return $this->tables;
}
$migrations = $this->class_names;
$filter = function($class_name) {
return !migrator::is_migration($class_name);
};
$edges = function($class_name) {
return $class_name::depends_on();
};
$apply_for_each = function($class_name) {
$this->apply_migration_to_schema($class_name);
};
try
{
$this->topologicalSort($migrations, $edges, true, $apply_for_each, $filter);
}
catch (LogicException $e)
{
throw new UnexpectedValueException(
"Migrations either have circular dependencies or unsatisfiable dependencies."
);
}
ksort($this->tables);
return $this->tables;
}
/**
* Apply the changes defined in the migration to the database schema.
*
* @param string $migration_class The name of the migration class.
*
* @throws UnexpectedValueException If a migration tries to use an undefined schema change.
*/
private function apply_migration_to_schema(string $migration_class)
{
$migration = new $migration_class(
$this->config,
$this->db,
$this->db_tools,
$this->phpbb_root_path,
$this->php_ext,
$this->table_prefix,
$this->table_names
);
$column_map = [
'add_tables' => null,
'drop_tables' => null,
'add_columns' => 'COLUMNS',
'drop_columns' => 'COLUMNS',
'change_columns' => 'COLUMNS',
'add_index' => 'KEYS',
'add_unique_index' => 'KEYS',
'drop_keys' => 'KEYS',
];
$schema_changes = $migration->update_schema();
foreach ($schema_changes as $change_type => $changes)
{
if (!array_key_exists($change_type, $column_map))
{
throw new UnexpectedValueException("$migration_class contains undefined schema changes: $change_type.");
}
$split_position = strpos($change_type, '_');
$schema_change_type = substr($change_type, 0, $split_position);
$schema_type = substr($change_type, $split_position + 1);
$action = null;
switch ($schema_change_type)
{
case 'add':
case 'change':
$action = function(&$value, $changes, $value_transform = null) {
self::set_all($value, $changes, $value_transform);
};
break;
case 'drop':
$action = function(&$value, $changes, $value_transform = null) {
self::unset_all($value, $changes);
};
break;
default:
throw new UnexpectedValueException("$migration_class contains undefined schema changes: $change_type.");
}
switch ($schema_type)
{
case 'tables':
$action($this->tables, $changes);
break;
default:
$this->for_each_table(
$changes,
$action,
$column_map[$change_type],
self::get_value_transform($schema_change_type, $schema_type)
);
}
}
}
/**
* Apply `$callback` to each table specified in `$data`.
*
* @param array $data Array describing the schema changes.
* @param callable $callback Callback function to be applied.
* @param string|null $column Column of the `$this->tables` array for the table on which
* the change will be made or null.
* @param callable|null $value_transform Value transformation callback function or null.
*/
private function for_each_table(array $data, callable $callback, $column = null, $value_transform = null)
{
foreach ($data as $table => $values)
{
$target = &$this->tables[$table];
if ($column !== null)
{
$target = &$target[$column];
}
$callback($target, $values, $value_transform);
}
}
/**
* Set an array of key-value pairs in the schema.
*
* @param mixed $schema Reference to the schema entry.
* @param mixed $data Array of values to be set.
* @param callable|null $value_transform Callback to transform the value being set.
*/
private static function set_all(&$schema, $data, ?callable $value_transform = null)
{
$data = (!is_array($data)) ? [$data] : $data;
foreach ($data as $key => $change)
{
if (is_callable($value_transform))
{
$value_transform($schema, $key, $change);
}
else
{
$schema[$key] = $change;
}
}
}
/**
* Remove an array of values from the schema
*
* @param mixed $schema Reference to the schema entry.
* @param mixed $data Array of values to be removed.
*/
private static function unset_all(&$schema, $data)
{
$data = (!is_array($data)) ? [$data] : $data;
foreach ($data as $key)
{
unset($schema[$key]);
}
}
/**
* Logic for adding a new column to a table.
*
* @param array $value The table column entry.
* @param string $key The column name to add.
* @param array $change The column data.
*/
private static function handle_add_column(array &$value, string $key, array $change)
{
if (!array_key_exists('after', $change))
{
$value[$key] = $change;
return;
}
$after = $change['after'];
unset($change['after']);
if ($after === null)
{
$value[$key] = array_values($change);
return;
}
$offset = array_search($after, array_keys($value));
if ($offset === false)
{
$value[$key] = array_values($change);
return;
}
$value = array_merge(
array_slice($value, 0, $offset + 1, true),
[$key => array_values($change)],
array_slice($value, $offset)
);
}
/**
* Returns the value transform for the change.
*
* @param string $change_type The type of the change.
* @param string $schema_type The schema type on which the change is to be performed.
*
* @return Closure|null The value transformation callback or null if it is not needed.
*/
private static function get_value_transform(string $change_type, string $schema_type) : ?Closure
{
if ($change_type !== 'add')
{
return null;
}
switch ($schema_type)
{
case 'index':
return function(&$value, $key, $change) {
$value[$key] = ['INDEX', $change];
};
case 'unique_index':
return function(&$value, $key, $change) {
$value[$key] = ['UNIQUE', $change];
};
case 'columns':
return function(&$value, $key, $change) {
self::handle_add_column($value, $key, $change);
};
}
return null;
}
}