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
|
<?php
/**
*
* @package db
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* The migrator is responsible for applying new migrations in the correct order.
*
* @package db
*/
class phpbb_db_migrator
{
var $db;
var $db_tools;
var $migrations_table;
var $migration_state;
var $migrations;
/**
* Constructor of the database migrator
*
* @param dbal $db Connected database abstraction instance
* @param phpbb_db_tools $db_tools Instance of db schema manipulation tools
* @param string $migrations_table The name of the db table storing
* information on applied migrations
*/
function phpbb_db_migrator(&$db, &$db_tools, $migrations_table)
{
$this->db = &$db;
$this->db_tools = &$db_tools;
$this->migrations_table = $migrations_table;
$this->migrations = array();
$this->load_migration_state();
}
/**
* Loads all migrations and their application state from the database.
*
* @return null
*/
function load_migration_state()
{
$sql = "SELECT *
FROM " . $this->migrations_table;
$result = $this->db->sql_query($sql);
$this->migration_state = array();
while ($migration = $this->db->sql_fetchrow($result))
{
$this->migration_state[$migration['migration_name']] = $migration;
}
$this->db->sql_freeresult($result);
}
/**
* Sets the list of available migration class names to the given array.
*
* @param array $class_names An array of migration class names
* @return null
*/
function set_migrations($class_names)
{
$this->migrations = $class_names;
}
/**
* Runs a single update step from the next migration to be applied.
*
* The update step can either be a schema or a (partial) data update. To
* check if update() needs to be called again use the finished() method.
*
* @return null
*/
function update()
{
foreach ($this->migrations as $name)
{
if (!isset($this->migration_state[$name]) ||
!$this->migration_state[$name]['migration_schema_done'] ||
!$this->migration_state[$name]['migration_data_done'])
{
if (!$this->try_apply($name))
{
continue;
}
else
{
return;
}
}
}
}
/**
* Attempts to apply a step of the given migration or one of its dependencies
*
* @param string The class name of the migration
* @return bool Whether any update step was successfully run
*/
function try_apply($name)
{
if (!class_exists($name))
{
return false;
}
$migration =& new $name($this->db, $this->db_tools);
$state = (isset($this->migration_state[$name])) ?
$this->migration_state[$name] :
array(
'migration_schema_done' => false,
'migration_data_done' => false,
'migration_data_state' => '',
'migration_start_time' => 0,
'migration_end_time' => 0,
);
$depends = $migration->depends_on();
foreach ($depends as $depend)
{
if (!isset($this->migration_state[$depend]) ||
!$this->migration_state[$depend]['migration_schema_done'] ||
!$this->migration_state[$depend]['migration_data_done'])
{
return $this->try_apply($depend);
}
}
if (!isset($this->migration_state[$name]))
{
$state['migration_start_time'] = time();
$this->insert_migration($name, $state);
}
if (!$state['migration_schema_done'])
{
$migration->update_schema();
$state['migration_schema_done'] = true;
}
else
{
$migration->update_data();
$state['migration_data_done'] = true;
$state['migration_end_time'] = time();
}
$sql = 'UPDATE ' . $this->migrations_table . '
SET ' . $this->db->sql_build_array('UPDATE', $state) . "
WHERE migration_name = '" . $this->db->sql_escape($name) . "'";
$this->db->sql_query($sql);
$this->migration_state[$name] = $state;
return true;
}
function insert_migration($name, $state)
{
$migration_row = $state;
$migration_row['migration_name'] = $name;
$sql = 'INSERT INTO ' . $this->migrations_table . '
' . $this->db->sql_build_array('INSERT', $migration_row);
$this->db->sql_query($sql);
$this->migration_state[$name] = $state;
}
/**
* Checks if a migration's dependencies can even theoretically be satisfied.
*
* @param string $name The class name of the migration
* @return bool Whether the migration cannot be fulfilled
*/
function unfulfillable($name)
{
if (isset($this->migration_state[$name]))
{
return false;
}
if (!class_exists($name))
{
return true;
}
$migration =& new $name($this->db, $this->db_tools);
$depends = $migration->depends_on();
foreach ($depends as $depend)
{
if ($this->unfulfillable($depend))
{
return true;
}
}
return false;
}
/**
* Checks whether all available, fulfillable migrations have been applied.
*
* @return bool Whether the migrations have been applied
*/
function finished()
{
foreach ($this->migrations as $name)
{
if (!isset($this->migration_state[$name]))
{
// skip unfulfillable migrations, but fulfillables mean we
// are not finished yet
if ($this->unfulfillable($name))
{
continue;
}
return false;
}
$migration = $this->migration_state[$name];
if (!$migration['migration_schema_done'] || !$migration['migration_data_done'])
{
return false;
}
}
return true;
}
}
|