1
- <?php
1
+ <?php
2
2
3
3
/*
4
4
* Based on:
5
5
* http://bakery.cakephp.org/articles/erma/2010/01/05/cakephp-sql-shell-simple-and-powerful
6
6
*/
7
7
8
- App::uses ('Folder ' , 'Utility ' );
8
+ App::uses ('Folder ' , 'Utility ' );
9
9
10
10
App::uses ('ConnectionManager ' , 'Model ' );
11
11
App::uses ('SchemaVersion ' , 'SqlMigration.Model ' );
12
12
13
- class SqlMigrationShell extends Shell {
14
-
13
+ class SqlMigrationShell extends Shell {
14
+
15
15
/**
16
16
* Connection used
17
17
*
@@ -27,22 +27,22 @@ class SqlMigrationShell extends Shell {
27
27
private $ myName = 'SqlMigration ' ;
28
28
29
29
/**
30
- * Table holding schema version
30
+ * Table holding schema version
31
31
*
32
32
* @var string
33
33
*/
34
34
private $ tableName = 'schema_versions ' ;
35
35
36
36
37
37
/**
38
- * Sucess status
39
- *
38
+ * Sucess status
39
+ *
40
40
*/
41
- private $ successStatus = 'STATUS ' ;
41
+ private $ successStatus = 'SUCCESS ' ;
42
42
43
43
/**
44
44
* Skipped status
45
- *
45
+ *
46
46
*/
47
47
private $ skippedStatus = 'SKIPPED ' ;
48
48
@@ -57,10 +57,10 @@ class SqlMigrationShell extends Shell {
57
57
* Overridding this method will prevent default welcome message
58
58
*/
59
59
public function _welcome () {
60
-
60
+
61
61
$ this ->out ('SQL Migration plugin ' );
62
62
$ this ->schemaVersionModel = new SchemaVersion ();
63
-
63
+
64
64
} // End function _welcome()
65
65
66
66
@@ -78,9 +78,9 @@ public function main() {
78
78
* Get latest version
79
79
* @return int Latest version number
80
80
*/
81
- private function getVersion () {
81
+ private function getVersion () {
82
82
$ latest = $ this ->schemaVersionModel ->find ('first ' , array (
83
- 'order ' => array ('created DESC ' ),
83
+ 'order ' => array ('version DESC ' ),
84
84
'limit ' => 1
85
85
));
86
86
if ( $ latest && is_numeric ($ latest ['SchemaVersion ' ]['version ' ]) ) {
@@ -90,19 +90,19 @@ private function getVersion() {
90
90
$ this ->out ('No version found. Assuming 0. ' );
91
91
return 0 ;
92
92
}
93
- } // End function getVersion()
94
-
93
+ } // End function getVersion()
94
+
95
95
96
96
/**
97
97
* Get all version history
98
98
* @return int Latest version number
99
99
*/
100
- private function getAllVersions () {
100
+ private function getAllVersions () {
101
101
$ all = $ this ->schemaVersionModel ->find ('all ' , array (
102
- 'order ' => array ('created ASC ' )
102
+ 'order ' => array ('version ASC ' )
103
103
));
104
104
return $ all ;
105
- } // End function getAllVersions()
105
+ } // End function getAllVersions()
106
106
107
107
108
108
/**
@@ -111,7 +111,7 @@ private function getAllVersions() {
111
111
* @param int $version Version
112
112
* @param String $status Status of upgrade to version
113
113
*/
114
- private function setVersion ($ version , $ status ) {
114
+ private function setVersion ($ version , $ status ) {
115
115
$ existingVersion = $ this ->schemaVersionModel ->findByVersion ($ version );
116
116
if ( $ existingVersion ) {
117
117
$ this ->schemaVersionModel ->id = $ existingVersion ['SchemaVersion ' ]['id ' ];
@@ -122,7 +122,7 @@ private function setVersion($version, $status) {
122
122
'version ' => $ version ,
123
123
'status ' => $ status ));
124
124
$ this ->schemaVersionModel ->create ();
125
- $ saved = $ this ->schemaVersionModel ->save ($ data );
125
+ $ saved = $ this ->schemaVersionModel ->save ($ data );
126
126
if ( !$ saved ) {
127
127
$ this ->out ('Unable to set version ' );
128
128
$ this ->_stop ();
@@ -136,37 +136,37 @@ private function setVersion($version, $status) {
136
136
* @param int $verion Version number
137
137
* @return String SQL to run
138
138
*/
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
+ }
146
146
} // End function getSql()
147
147
148
-
148
+
149
149
/**
150
150
* Run the update.
151
151
* This will try to run all upgrade SQL file in order of version.
152
152
* It wil also try to run./re-run any version that might have been
153
153
* skipped previously
154
154
*/
155
- public function update () {
155
+ public function update () {
156
156
$ sqlFolder = new Folder (APP .'Config/Sql ' );
157
157
$ updateErrors = array ();
158
158
list ($ dirs , $ files ) = $ sqlFolder ->read ();
159
159
$ 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 )) {
162
162
$ upgrades [(int )$ matches [1 ]] = $ file ;
163
- }
164
- }
165
- ksort ($ upgrades );
163
+ }
164
+ }
165
+ ksort ($ upgrades );
166
166
$ version = max (array_keys ($ upgrades ));
167
167
$ this ->out ('Upgrading up to version : ' .$ version );
168
168
169
- // Get all versions
169
+ // Get all versions
170
170
$ allVersions = $ this ->getAllVersions ();
171
171
172
172
// Try to run missing/skipped versions
@@ -184,8 +184,8 @@ public function update() {
184
184
}
185
185
}
186
186
// 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 );
189
189
$ this ->out ('Updating to Version ' .($ currentVersion +1 ));
190
190
if ( !isset ($ upgrades [$ currentVersion +1 ]) ) {
191
191
$ this ->out ('No upgrade file for version ' .($ currentVersion +1 ).'. Skipping ' );
@@ -204,7 +204,7 @@ public function update() {
204
204
if ($ numErrors ) {
205
205
$ this ->error ("There were " . $ numErrors . " errors found while trying to upgrade your database. Please investigate. " );
206
206
}
207
- $ this ->out ('Done with upgrades. Now at version ' .$ this ->getVersion ());
207
+ $ this ->out ('Done with upgrades. Now at version ' .$ this ->getVersion ());
208
208
} // End function update
209
209
210
210
/**
@@ -213,17 +213,18 @@ public function update() {
213
213
* @return boolean False if user choose to not run the SQL. 'Skip' will return true
214
214
*/
215
215
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 ));
219
219
$ this ->hr ();
220
220
$ a = $ this ->in ('Execute SQL? [y/n/s] ' );
221
- if ( $ a === 'n ' ) {
222
- return false ;
221
+ if ( $ a === 'n ' ) {
222
+ return false ;
223
223
}
224
224
else if ( $ a === 's ' ) {
225
+ $ this ->setVersion ((int )($ version ), $ this ->skippedStatus );
225
226
return true ;
226
- } else {
227
+ } else {
227
228
$ this ->out ('Launching MySQL to execute SQL ' );
228
229
$ database = ConnectionManager::getDataSource ('default ' )->config ;
229
230
$ sql_file = APP .'Config/Sql/upgrade- ' .$ version .'.sql ' ;
@@ -234,8 +235,8 @@ private function executeSql($version) {
234
235
$ e = new Exception ("An error occurred trying to execute " . $ sql_file , $ execReturn );
235
236
throw $ e ;
236
237
}
237
- $ this ->setVersion ((int )($ version ), $ this ->successStatus );
238
- }
238
+ $ this ->setVersion ((int )($ version ), $ this ->successStatus );
239
+ }
239
240
return true ;
240
241
} // End function executeSql()
241
242
@@ -269,6 +270,10 @@ private function updateMigrationSchema() {
269
270
$ command = 'schema update --quiet --plugin ' .$ this ->myName .' --connection ' . $ this ->connection ;
270
271
// Dispatch to shell
271
272
$ 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' " );
272
277
$ this ->out ('Updated ' );
273
278
274
279
} // End function updateMigrationchema()
@@ -287,5 +292,5 @@ private function createMigrationSchema() {
287
292
288
293
} // End function createMigrationchema()
289
294
290
- }
291
- ?>
295
+ }
296
+ ?>
0 commit comments