Skip to content

Commit c0384d3

Browse files
committed
Fix issue #3
Need to order versions by version number not created date when trying to find when uograde to run. Versions could have been created in random order if some older version where skipped and then run later (=> older version but more recent created date). Also fix status to SUCCESS
1 parent c19ba2a commit c0384d3

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

Console/Command/SqlMigrationShell.php

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
<?php
1+
<?php
22

33
/*
44
* Based on:
55
* http://bakery.cakephp.org/articles/erma/2010/01/05/cakephp-sql-shell-simple-and-powerful
66
*/
77

8-
App::uses('Folder', 'Utility');
8+
App::uses('Folder', 'Utility');
99

1010
App::uses('ConnectionManager', 'Model');
1111
App::uses('SchemaVersion', 'SqlMigration.Model');
1212

13-
class SqlMigrationShell extends Shell {
14-
13+
class SqlMigrationShell extends Shell {
14+
1515
/**
1616
* Connection used
1717
*
@@ -27,22 +27,22 @@ class SqlMigrationShell extends Shell {
2727
private $myName = 'SqlMigration';
2828

2929
/**
30-
* Table holding schema version
30+
* Table holding schema version
3131
*
3232
* @var string
3333
*/
3434
private $tableName = 'schema_versions';
3535

3636

3737
/**
38-
* Sucess status
39-
*
38+
* Sucess status
39+
*
4040
*/
41-
private $successStatus = 'STATUS';
41+
private $successStatus = 'SUCCESS';
4242

4343
/**
4444
* Skipped status
45-
*
45+
*
4646
*/
4747
private $skippedStatus = 'SKIPPED';
4848

@@ -57,10 +57,10 @@ class SqlMigrationShell extends Shell {
5757
* Overridding this method will prevent default welcome message
5858
*/
5959
public function _welcome() {
60-
60+
6161
$this->out('SQL Migration plugin');
6262
$this->schemaVersionModel = new SchemaVersion();
63-
63+
6464
} // End function _welcome()
6565

6666

@@ -78,9 +78,9 @@ public function main() {
7878
* Get latest version
7979
* @return int Latest version number
8080
*/
81-
private function getVersion() {
81+
private function getVersion() {
8282
$latest = $this->schemaVersionModel->find('first', array(
83-
'order' => array('created DESC'),
83+
'order' => array('version DESC'),
8484
'limit' => 1
8585
));
8686
if ( $latest && is_numeric($latest['SchemaVersion']['version']) ) {
@@ -90,19 +90,19 @@ private function getVersion() {
9090
$this->out('No version found. Assuming 0.');
9191
return 0;
9292
}
93-
} // End function getVersion()
94-
93+
} // End function getVersion()
94+
9595

9696
/**
9797
* Get all version history
9898
* @return int Latest version number
9999
*/
100-
private function getAllVersions() {
100+
private function getAllVersions() {
101101
$all = $this->schemaVersionModel->find('all', array(
102-
'order' => array('created ASC')
102+
'order' => array('version ASC')
103103
));
104104
return $all;
105-
} // End function getAllVersions()
105+
} // End function getAllVersions()
106106

107107

108108
/**
@@ -111,7 +111,7 @@ private function getAllVersions() {
111111
* @param int $version Version
112112
* @param String $status Status of upgrade to version
113113
*/
114-
private function setVersion($version, $status) {
114+
private function setVersion($version, $status) {
115115
$existingVersion = $this->schemaVersionModel->findByVersion($version);
116116
if ( $existingVersion ) {
117117
$this->schemaVersionModel->id = $existingVersion['SchemaVersion']['id'];
@@ -122,7 +122,7 @@ private function setVersion($version, $status) {
122122
'version' => $version,
123123
'status' => $status));
124124
$this->schemaVersionModel->create();
125-
$saved = $this->schemaVersionModel->save($data);
125+
$saved = $this->schemaVersionModel->save($data);
126126
if ( !$saved ) {
127127
$this->out('Unable to set version');
128128
$this->_stop();
@@ -136,37 +136,37 @@ private function setVersion($version, $status) {
136136
* @param int $verion Version number
137137
* @return String SQL to run
138138
*/
139-
private function getSql($version) {
140-
if (($text = file_get_contents($filename = APP.'Config/Sql/upgrade-'.$version.'.sql')) !== false) {
141-
return $text;
142-
} else {
143-
$this->out("Couldn't load contents of file {$filename}, unable to uograde/downgrade");
144-
$this->_stop();
145-
}
139+
private function getSql($version) {
140+
if (($text = file_get_contents($filename = APP.'Config/Sql/upgrade-'.$version.'.sql')) !== false) {
141+
return $text;
142+
} else {
143+
$this->out("Couldn't load contents of file {$filename}, unable to uograde/downgrade");
144+
$this->_stop();
145+
}
146146
} // End function getSql()
147147

148-
148+
149149
/**
150150
* Run the update.
151151
* This will try to run all upgrade SQL file in order of version.
152152
* It wil also try to run./re-run any version that might have been
153153
* skipped previously
154154
*/
155-
public function update() {
155+
public function update() {
156156
$sqlFolder = new Folder(APP.'Config/Sql');
157157
$updateErrors = array();
158158
list($dirs, $files) = $sqlFolder->read();
159159
$upgrades = array();
160-
foreach ($files as $i => $file) {
161-
if (preg_match( '/upgrade-(\d+)\.sql$/', $file, $matches)) {
160+
foreach ($files as $i => $file) {
161+
if (preg_match( '/upgrade-(\d+)\.sql$/', $file, $matches)) {
162162
$upgrades[(int)$matches[1]] = $file;
163-
}
164-
}
165-
ksort($upgrades);
163+
}
164+
}
165+
ksort($upgrades);
166166
$version = max(array_keys($upgrades));
167167
$this->out('Upgrading up to version : '.$version);
168168

169-
// Get all versions
169+
// Get all versions
170170
$allVersions = $this->getAllVersions();
171171

172172
// Try to run missing/skipped versions
@@ -184,8 +184,8 @@ public function update() {
184184
}
185185
}
186186
// Run upgrades up to the highest/latest verion of the upgrade files found
187-
for ($currentVersion = $this->getVersion(); $currentVersion < $version; $currentVersion++) {
188-
$this->out('Currently at Version '.$currentVersion);
187+
for ($currentVersion = $this->getVersion(); $currentVersion < $version; $currentVersion++) {
188+
$this->out('Currently at Version '.$currentVersion);
189189
$this->out('Updating to Version '.($currentVersion+1));
190190
if ( !isset($upgrades[$currentVersion+1]) ) {
191191
$this->out('No upgrade file for version '.($currentVersion+1).'. Skipping');
@@ -204,7 +204,7 @@ public function update() {
204204
if ($numErrors) {
205205
$this->error("There were " . $numErrors . " errors found while trying to upgrade your database. Please investigate.");
206206
}
207-
$this->out('Done with upgrades. Now at version '.$this->getVersion());
207+
$this->out('Done with upgrades. Now at version '.$this->getVersion());
208208
} // End function update
209209

210210
/**
@@ -213,17 +213,18 @@ public function update() {
213213
* @return boolean False if user choose to not run the SQL. 'Skip' will return true
214214
*/
215215
private function executeSql($version) {
216-
$this->out('Executing sql:');
217-
$this->hr();
218-
$this->out($sql = $this->getSql($version));
216+
$this->out('Executing sql:');
217+
$this->hr();
218+
$this->out($sql = $this->getSql($version));
219219
$this->hr();
220220
$a = $this->in('Execute SQL? [y/n/s]');
221-
if ( $a === 'n') {
222-
return false;
221+
if ( $a === 'n') {
222+
return false;
223223
}
224224
else if ( $a === 's') {
225+
$this->setVersion((int)($version), $this->skippedStatus);
225226
return true;
226-
} else {
227+
} else {
227228
$this->out('Launching MySQL to execute SQL');
228229
$database = ConnectionManager::getDataSource('default')->config;
229230
$sql_file = APP.'Config/Sql/upgrade-'.$version.'.sql';
@@ -234,8 +235,8 @@ private function executeSql($version) {
234235
$e = new Exception("An error occurred trying to execute " . $sql_file, $execReturn);
235236
throw $e;
236237
}
237-
$this->setVersion((int)($version), $this->successStatus);
238-
}
238+
$this->setVersion((int)($version), $this->successStatus);
239+
}
239240
return true;
240241
} // End function executeSql()
241242

@@ -269,6 +270,10 @@ private function updateMigrationSchema() {
269270
$command = 'schema update --quiet --plugin '.$this->myName.' --connection ' . $this->connection;
270271
// Dispatch to shell
271272
$this->dispatchShell($command);
273+
274+
// Update incorrect status
275+
// This is a typo in original code when 'SUCCESS' as set at 'STATUS'
276+
$this->schemaVersionModel->query("UPDATE schema_versions SET STATUS = '".$this->successStatus."' WHERE STATUS = 'STATUS'");
272277
$this->out('Updated');
273278

274279
} // End function updateMigrationchema()
@@ -287,5 +292,5 @@ private function createMigrationSchema() {
287292

288293
} // End function createMigrationchema()
289294

290-
}
291-
?>
295+
}
296+
?>

0 commit comments

Comments
 (0)