|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConsoleTools\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Db\Adapter\Adapter; |
|
6
|
|
|
use Zend\Db\ResultSet\ResultSetInterface; |
|
7
|
|
|
use Zend\Db\Sql\Expression; |
|
8
|
|
|
use Zend\Db\Sql\Sql; |
|
9
|
|
|
use Zend\Db\Sql\Ddl; |
|
10
|
|
|
use Zend\Console\Prompt\Confirm; |
|
11
|
|
|
use Zend\ServiceManager\ServiceLocatorAwareTrait; |
|
12
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
13
|
|
|
use Zend\Console\ColorInterface as Color; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Generate class and methods for new migration |
|
17
|
|
|
* |
|
18
|
|
|
* @author V.Leontiev <[email protected]> |
|
19
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
20
|
|
|
* @since php 5.6 or higher |
|
21
|
|
|
* @see https://github.com/newage/console-tools |
|
22
|
|
|
*/ |
|
23
|
|
|
class Migration |
|
24
|
|
|
{ |
|
25
|
|
|
use ServiceLocatorAwareTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Current db adapter |
|
29
|
|
|
* |
|
30
|
|
|
* @var Adapter |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $adapter = null; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $percona = false; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $port = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Migration table name of database |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
const TABLE = 'migrations'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructor |
|
53
|
|
|
* Create migration table |
|
54
|
|
|
* Set current db adapter |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Zend\Db\Adapter\Adapter $adapter |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct($adapter = null, ServiceLocatorInterface $serviceLocator, $percona, $port) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$this->adapter = $adapter; |
|
61
|
|
|
$this->percona = $percona; |
|
62
|
|
|
$this->port = $port; |
|
63
|
|
|
$this->createTable(); |
|
64
|
|
|
$this->setServiceLocator($serviceLocator); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Create a migration table |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function createTable() |
|
73
|
|
|
{ |
|
74
|
|
|
$sql = new Sql($this->adapter); |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
$select = $sql->select(self::TABLE); |
|
78
|
|
|
$queryString = $sql->getSqlStringForSqlObject($select); |
|
|
|
|
|
|
79
|
|
|
$this->adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE); |
|
80
|
|
|
} catch (\Exception $err) { |
|
81
|
|
|
$table = new Ddl\CreateTable(self::TABLE); |
|
82
|
|
|
$table->addColumn(new Ddl\Column\Integer('id')); |
|
83
|
|
|
$table->addColumn(new Ddl\Column\Char('migration', 255)); |
|
84
|
|
|
$table->addColumn(new Ddl\Column\Text('up')); |
|
85
|
|
|
$table->addColumn(new Ddl\Column\Text('down')); |
|
86
|
|
|
$table->addColumn(new Ddl\Column\Integer('ignored', false, 0, array('length' => 1))); |
|
87
|
|
|
|
|
88
|
|
|
$table->addConstraint(new Ddl\Constraint\PrimaryKey('id')); |
|
89
|
|
|
$table->addConstraint(new Ddl\Constraint\UniqueKey(['migration'], 'unique_key')); |
|
90
|
|
|
|
|
91
|
|
|
$queryString = $sql->getSqlStringForSqlObject($table); |
|
|
|
|
|
|
92
|
|
|
$this->adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get a last migration |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function last() |
|
102
|
|
|
{ |
|
103
|
|
|
$sql = new Sql($this->adapter); |
|
104
|
|
|
$select = $sql->select(self::TABLE); |
|
105
|
|
|
$select->columns(array( |
|
106
|
|
|
'last' => new Expression('MAX(id)'), |
|
107
|
|
|
'up', |
|
108
|
|
|
'down', |
|
109
|
|
|
'ignored' |
|
110
|
|
|
)); |
|
111
|
|
|
|
|
112
|
|
|
$selectString = $sql->getSqlStringForSqlObject($select); |
|
|
|
|
|
|
113
|
|
|
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE); |
|
114
|
|
|
|
|
115
|
|
|
return $results->current(); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
/** |
|
118
|
|
|
* Get a last migration |
|
119
|
|
|
* |
|
120
|
|
|
* @param bool $isShow Show sql queries |
|
|
|
|
|
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function get(array $where = []) |
|
124
|
|
|
{ |
|
125
|
|
|
$sql = new Sql($this->adapter); |
|
126
|
|
|
$select = $sql->select(self::TABLE); |
|
127
|
|
|
$select->columns(array('*')); |
|
128
|
|
|
$select->where($where); |
|
129
|
|
|
|
|
130
|
|
|
$selectString = $sql->getSqlStringForSqlObject($select); |
|
|
|
|
|
|
131
|
|
|
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE); |
|
132
|
|
|
|
|
133
|
|
|
return $results->current(); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get applied a migrations name |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
public function applied() |
|
142
|
|
|
{ |
|
143
|
|
|
$migrationFiles = array(); |
|
144
|
|
|
$sql = new Sql($this->adapter); |
|
145
|
|
|
$select = $sql->select(); |
|
146
|
|
|
$select->from(self::TABLE); |
|
147
|
|
|
|
|
148
|
|
|
$selectString = $sql->getSqlStringForSqlObject($select); |
|
|
|
|
|
|
149
|
|
|
$results = $this->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE); |
|
150
|
|
|
|
|
151
|
|
|
if ($results->count() > 0) { |
|
|
|
|
|
|
152
|
|
|
foreach ($results as $migration) { |
|
|
|
|
|
|
153
|
|
|
$migrationFiles[] = $migration->migration; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $migrationFiles; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Apply up on the migration |
|
162
|
|
|
* And insert migration name to base |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $migrationName |
|
165
|
|
|
* @param array $migrationArray |
|
166
|
|
|
* @param bool $ignoreMigration |
|
167
|
|
|
* @param bool $doNotSaveAsExecuted |
|
168
|
|
|
* @throws \Exception |
|
169
|
|
|
* @internal param bool $ig |
|
170
|
|
|
*/ |
|
171
|
|
|
public function upgrade($migrationName, array $migrationArray, $ignoreMigration = false, $doNotSaveAsExecuted = false) |
|
172
|
|
|
{ |
|
173
|
|
|
$connection = $this->adapter->getDriver()->getConnection(); |
|
174
|
|
|
$connection->beginTransaction(); |
|
175
|
|
|
|
|
176
|
|
|
try { |
|
177
|
|
|
if (!$ignoreMigration) { |
|
178
|
|
|
$this->executeQueriesOneByOne($migrationArray['up']); |
|
179
|
|
|
} |
|
180
|
|
|
if (!$doNotSaveAsExecuted) { |
|
181
|
|
|
$sql = new Sql($this->adapter); |
|
182
|
|
|
$insert = $sql->insert(self::TABLE); |
|
183
|
|
|
$insert->values(array( |
|
184
|
|
|
'migration' => $migrationName, |
|
185
|
|
|
'up' => $migrationArray['up'], |
|
186
|
|
|
'down' => $migrationArray['down'], |
|
187
|
|
|
'ignore' => (int)$ignoreMigration, |
|
188
|
|
|
)); |
|
189
|
|
|
$queryString = $sql->getSqlStringForSqlObject($insert); |
|
|
|
|
|
|
190
|
|
|
$this->adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$connection->commit(); |
|
194
|
|
|
} catch (\Exception $exception) { |
|
195
|
|
|
$connection->rollback(); |
|
196
|
|
|
throw new \Exception($exception->getMessage()); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Apply down on the migration |
|
202
|
|
|
* And remove migration name from base |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $migrationName |
|
205
|
|
|
* @param array $migrationArray |
|
206
|
|
|
* @param bool $ignore |
|
207
|
|
|
* @throws \Exception |
|
208
|
|
|
*/ |
|
209
|
|
|
public function downgrade($migrationName, array $migrationArray, $ignore = false) |
|
210
|
|
|
{ |
|
211
|
|
|
$connection = $this->adapter->getDriver()->getConnection(); |
|
212
|
|
|
|
|
213
|
|
|
try { |
|
214
|
|
|
$connection->beginTransaction(); |
|
215
|
|
|
|
|
216
|
|
|
if (!$ignore) { |
|
217
|
|
|
$this->executeQueriesOneByOne($migrationArray['down']); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$sql = new Sql($this->adapter); |
|
221
|
|
|
$delete = $sql->delete(self::TABLE); |
|
222
|
|
|
$delete->where(array('migration' => $migrationName)); |
|
223
|
|
|
|
|
224
|
|
|
$queryString = $sql->getSqlStringForSqlObject($delete); |
|
|
|
|
|
|
225
|
|
|
$this->adapter->query($queryString, Adapter::QUERY_MODE_EXECUTE); |
|
226
|
|
|
|
|
227
|
|
|
$connection->commit(); |
|
228
|
|
|
} catch(\Exception $exception) { |
|
|
|
|
|
|
229
|
|
|
$connection->rollback(); |
|
230
|
|
|
throw new \Exception($exception->getMessage()); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string $migration |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function executeQueriesOneByOne($migration = '') |
|
238
|
|
|
{ |
|
239
|
|
|
$console = $this->getServiceLocator()->get('console'); |
|
240
|
|
|
$config = $this->getServiceLocator()->get('config'); |
|
241
|
|
|
if (!isset($config['db'])) { |
|
242
|
|
|
throw new \RuntimeException('Does nto exist `db` config!'); |
|
243
|
|
|
} |
|
244
|
|
|
$dbConfig = $config['db']; |
|
245
|
|
|
|
|
246
|
|
|
$queries = explode(';', $migration); |
|
247
|
|
|
foreach ($queries as $query) { |
|
248
|
|
|
$query = trim($query, " \t\n\r\0"); |
|
249
|
|
|
if (!empty($query)) { |
|
250
|
|
|
if (Confirm::prompt($query . PHP_EOL . 'Run this query? [y/n]', 'y', 'n')) { |
|
251
|
|
|
if ($this->executeInPerconaTool($query, $dbConfig) === false) { |
|
252
|
|
|
$request = $this->adapter->query($query, Adapter::QUERY_MODE_EXECUTE); |
|
253
|
|
|
|
|
254
|
|
|
if ($request instanceof ResultSetInterface) { |
|
255
|
|
|
$console->writeLine('Affected rows: ' . $request->count(), Color::BLUE); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
} elseif (Confirm::prompt('Break execution and ROLLBACK? [y/n]', 'y', 'n')) { |
|
259
|
|
|
$connection = $this->adapter->getDriver()->getConnection(); |
|
260
|
|
|
$connection->rollback(); |
|
261
|
|
|
exit; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
protected function executeInPerconaTool($query, $dbConfig): bool |
|
269
|
|
|
{ |
|
270
|
|
|
if (stristr($query, 'ALTER TABLE') && $this->percona) { |
|
271
|
|
|
$cleanQuery = str_replace(['`', '\''], '', $query); |
|
272
|
|
|
|
|
273
|
|
|
preg_match('~ALTER TABLE\s([\w\d\_]+)\s(.*);?~smi', $cleanQuery, $matches); |
|
274
|
|
|
if (!isset($matches[1]) || !isset($matches[2])) { |
|
275
|
|
|
return false; |
|
276
|
|
|
} |
|
277
|
|
|
$console = $this->getServiceLocator()->get('console'); |
|
278
|
|
|
|
|
279
|
|
|
$port = $dbConfig['port']; |
|
280
|
|
|
if (!empty($this->port)) { |
|
281
|
|
|
$port = $this->port; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$perconaString = sprintf( |
|
285
|
|
|
'pt-online-schema-change --execute --alter-foreign-keys-method=auto --password=%1$s --user=%2$s --alter "%6$s" D=%3$s,t=%5$s,h=%4$s,P=%7$s', |
|
286
|
|
|
$dbConfig['password'], |
|
287
|
|
|
$dbConfig['username'], |
|
288
|
|
|
$dbConfig['database'], |
|
289
|
|
|
$dbConfig['hostname'], |
|
290
|
|
|
$matches[1], |
|
291
|
|
|
$matches[2], |
|
292
|
|
|
$port |
|
293
|
|
|
); |
|
294
|
|
|
$result = shell_exec($perconaString); |
|
295
|
|
|
$console->writeLine('Percona response:', Color::BLUE); |
|
296
|
|
|
$console->writeLine($result, Color::WHITE); |
|
297
|
|
|
return true; |
|
298
|
|
|
} |
|
299
|
|
|
return false; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: