16
16
*/
17
17
class UpCommand extends Command
18
18
{
19
+ /**
20
+ * @var string
21
+ */
22
+ const DEFAULT_PROVISIONAL_VERSION_NAME = 'new ' ;
23
+
24
+ /**
25
+ * @var string
26
+ */
19
27
protected $ env ;
20
28
21
29
public function __construct ($ env )
@@ -46,6 +54,19 @@ protected function configure()
46
54
InputOption::VALUE_REQUIRED ,
47
55
'Optional custom path to database versions directory '
48
56
)
57
+ ->addOption (
58
+ 'install-provisional-version ' ,
59
+ null ,
60
+ InputOption::VALUE_NONE ,
61
+ 'Install a provisional version which may still be in development and is not final. '
62
+ )
63
+ ->addOption (
64
+ 'provisional-version ' ,
65
+ null ,
66
+ InputOption::VALUE_REQUIRED ,
67
+ 'The name of the provisional version ' ,
68
+ static ::DEFAULT_PROVISIONAL_VERSION_NAME
69
+ )
49
70
;
50
71
}
51
72
@@ -83,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
83
104
$ output ->writeln ('Installing version control... ' );
84
105
85
106
$ result = $ buildConf ->prepare (
86
- "CREATE TABLE `db_config`
107
+ "CREATE TABLE `db_config`
87
108
(
88
109
`key` VARCHAR(50) COLLATE 'utf8_general_ci' NOT NULL,
89
110
`value` TEXT,
@@ -138,17 +159,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
138
159
}
139
160
$ output ->writeln ('Available version: ' .$ availableVersion );
140
161
141
- if ($ currentVersion >= $ availableVersion ) {
142
- $ output ->writeln ('<info>Database version is already up to date.</info> ' );
143
- return 0 ;
144
- }
145
-
146
- $ noun = ($ availableVersion - $ currentVersion > 1 ) ? 'updates ' : 'update ' ;
147
- $ output ->writeln (
148
- "Installing database $ noun (Current version: $ currentVersion, Available version: $ availableVersion)... "
149
- );
150
-
151
- // go from current to latest version, building stack of SQL files
152
162
$ filesToLookFor = [];
153
163
if (!$ input ->getOption ('no-schema ' )) {
154
164
$ filesToLookFor [] = 'schema.sql ' ; // structural changes, alters, creates, drops
@@ -160,20 +170,55 @@ protected function execute(InputInterface $input, OutputInterface $output)
160
170
$ filesToLookFor [] = 'runme.php ' ; // custom php hook
161
171
162
172
$ stack = array ();
163
- for ($ i = $ currentVersion + 1 ; $ i <= $ availableVersion ; $ i ++) {
173
+ if ($ currentVersion < $ availableVersion ) {
174
+ for ($ i = $ currentVersion + 1 ; $ i <= $ availableVersion ; $ i ++) {
164
175
165
- $ path = $ versionsPath .DIRECTORY_SEPARATOR .$ i ;
166
- if (!is_dir ($ path ) || !is_readable ($ path )) {
167
- continue ;
176
+ $ path = $ versionsPath .DIRECTORY_SEPARATOR .$ i ;
177
+ if (!is_dir ($ path ) || !is_readable ($ path )) {
178
+ continue ;
179
+ }
180
+
181
+ foreach ($ filesToLookFor as $ file ) {
182
+ if (is_readable ($ path .DIRECTORY_SEPARATOR .$ file ) && is_file ($ path .DIRECTORY_SEPARATOR .$ file )) {
183
+ $ stack [$ i ][$ file ] = $ path .DIRECTORY_SEPARATOR .$ file ;
184
+ }
185
+ }
168
186
}
187
+ }
188
+
189
+ // Look for a provisional version?
190
+ $ provisionalVersion = null ;
191
+ if ($ input ->getOption ('install-provisional-version ' )) {
192
+
193
+ $ provisionalVersion = $ input ->getOption ('provisional-version ' );
194
+ $ output ->writeln ('Provisional version: ' .$ provisionalVersion );
195
+
196
+ $ path = $ versionsPath .DIRECTORY_SEPARATOR .$ provisionalVersion ;
197
+ if (is_readable ($ path ) && is_dir ($ path )) {
169
198
170
- foreach ($ filesToLookFor as $ file ) {
171
- if (is_readable ($ path .DIRECTORY_SEPARATOR .$ file )) {
172
- $ stack [$ i ][$ file ] = $ path .DIRECTORY_SEPARATOR .$ file ;
199
+ foreach ($ filesToLookFor as $ file ) {
200
+ if (is_readable ($ path .DIRECTORY_SEPARATOR .$ file ) && is_file ($ path .DIRECTORY_SEPARATOR .$ file )) {
201
+ $ stack [$ provisionalVersion ][$ file ] = $ path .DIRECTORY_SEPARATOR .$ file ;
202
+ }
173
203
}
174
204
}
175
205
}
176
206
207
+ $ updates = count ($ stack );
208
+ if (!$ updates ) {
209
+ $ output ->writeln ('<info>Database version is already up to date.</info> ' );
210
+ return 0 ;
211
+ }
212
+
213
+ $ noun = ($ updates > 1 ) ? 'updates ' : 'update ' ;
214
+ $ report = "Current version: $ currentVersion, Available version: $ availableVersion " ;
215
+ if (is_string ($ provisionalVersion ) && array_key_exists ($ provisionalVersion , $ stack )) {
216
+ $ report .= ", Provisional version: $ provisionalVersion " ;
217
+ }
218
+ $ output ->writeln (
219
+ "Installing database $ noun ( $ report)... "
220
+ );
221
+
177
222
$ s = '\\' == DIRECTORY_SEPARATOR ? "%s " : "'%s' " ; // Windows doesn't like quoted params
178
223
$ cmdMySQL = "$ mysqlbin -h $ s --user= $ s --password= $ s --database= $ s < %s " ;
179
224
@@ -232,7 +277,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
232
277
}
233
278
}
234
279
235
- if ($ result ) {
280
+ if ($ result && is_int ( $ version ) ) {
236
281
$ result = $ buildConf ->query (
237
282
"REPLACE INTO `db_config` (`key`, `value`, `updated_at`) VALUES ('version', $ version, now()) "
238
283
)->execute ();
0 commit comments