1 | <?php |
||
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) |
||
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 = []) |
||
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 = '') |
||
267 | |||
268 | protected function executeInPerconaTool($query, $dbConfig): bool |
||
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: