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