Pluf Framework

Review 2: extending migrate.php of restore/backup func

Created:2009-07-23 16:59:12 (3 years 10 months ago)
Updated:3 years 10 months ago
Author:Jakub Vitak
Commit:dddca5070e52d50d6b00dca6d0a75d418f908ae6
Description:extending migrate.php of restore/backup func

added new options to migrate.php now it is able to backup/restore data should be able to do it with upgrade but there are some issues which need more knowledge
Reviewers:No reviewers at the moment.
Files: b/src/Pluf/Migration.php (6 diffs)
b/src/backup.php (1 diff)
b/src/migrate.php (10 diffs)
 Archive Download the corresponding diff file
b/src/Pluf/Migration.php
OldNew
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
* // Uninstall the application MyApp
* $m->unInstall();
*
* $m = new Pluf_Migration();
* $m->migrate(); // migrate all the installed app to the newest version.
*
/**
* Un/Install the given application.
*
* @param string Application to install.
* @param bool Uninstall (false)
*/
public function installApp($app, $uninstall=false)
{
if ($uninstall) {
$func = $app.'_Migrations_Install_teardown';
}
if (!$this->dry_run) {
$ret = $func(); // Run the install/uninstall
if (!$uninstall) {
//
$this->app = $app;
} else {
$to_version = 0;
}
$this->setAppVersion($app, $to_version);
} else {
if ($app != 'Pluf') {
return $ret;
}
/**
* Find the migrations for the current app.
*
return 0;
}
}
}
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
* // Uninstall the application MyApp
* $m->unInstall();
*
* // Install the application MyApp to specified version
* $m = new Pluf_Migration('MyApp'); // or Pluf_Migration()
* $m->installApp($app, false, $ver);
*
* need for restore backup func ^^^
*
* $m = new Pluf_Migration();
* $m->migrate(); // migrate all the installed app to the newest version.
*
/**
* Un/Install the given application.
*
* extended: backup/restore need to install only specified version
*
* @param string Application to install.
* @param bool Uninstall (false)
*/
public function installApp($app, $uninstall=false, $ver=null)
{
if ($uninstall) {
$func = $app.'_Migrations_Install_teardown';
}
if (!$this->dry_run) {
$ret = $func(); // Run the install/uninstall
if (!$uninstall) {
//
$this->app = $app;
} else {
$to_version = 0;
}
if ((!is_null($ver)) && ($ver > $to_version)) {
/*
* check if we want to restore
* something, which doesn't exist
*/
throw new Exception("requiring higher version ($ver) than exist ($to_version)");
} else {
// everything ok
$to_version = $ver; // we know what we want ;-)
}
$this->setAppVersion($app, $to_version);
} else {
if ($app != 'Pluf') {
return $ret;
}
/**
* Find the migrations for the current app.
*
return 0;
}
}
}
b/src/backup.php
OldNew
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/*
* USAGE EXAMPLES:
*
* backup(); // backup to current dir
* backup("/tmp/"); // backup to different dir
*
* // backup data to this directory in my format
* backup($cfg['tmp_dir'].'/'.date("myApp_d-m-Y"), true);
*
* $dir = '/tmp/2009-07-21_01-40-52'; // full path to restore data
* restore($dir);
*
* TODO
*
* 1) merge into migrate.php [ok]
* -> call backup/restore from migrate and use what['xxx']
* 2) add zip/tar.bz2 support [-]
* 3) allow partial/full backup [1/2] (try{}catch(){})
*
* add name of backuped app to directory as prefix
* (for sure when making archive)
*
* NEED HELP:
* - save ALL and _restore_ data in right order (!!)
* - review code and maybe unit testing
*
* KNOWN ISSUES:
* - when restoring, dates are throwed away
* and rewriten with actual (restore) date&time
* - there are some exceptions when restoring some objects
* (from pluf/src/Pluf/Test/Fixture.php:56)
*
* => maybe need something which saves raw model
* and don't calls postSave() and so on
*
* - and this one:
* Fatal error: Call to a member function _toIndex() on a non-object in /srv/http/indefero/src/IDF/WikiPage.php on line 133
* (but maybe this is caused by my broken InDefero instance)
*
*/
//===========================
function pex($info, $e) {
$tmps = $e->getFile().":".$e->getLine()
." [$info] Caught exception: ".var_dump($e->getMessage())."\n";
echo $tmps;
//fprintf($STDERR, "%s", $tmps);
}
/*
* if included
* not define debug
* not require pluf
* not call pluf::start
*/
$run = false;
if (__FILE__ == realpath($argv[0])) {
$run = true;
}
if ($run) {
global $debug;
$debug = false;
//===========================
//$conf='/srv/http/indefero/src/IDF/conf/idf.php.sqlite'; // from
$conf='/srv/http/indefero/src/IDF/conf/idf.php'; // to
$app_path = '/srv/http/indefero/src';
//$path_to_Pluf = '/srv/http/pluf/src'; // stable
$path_to_Pluf = '/mnt/data/third-party/WEB/pluf/src'; // dev
set_include_path(get_include_path()
.PATH_SEPARATOR.$path_to_Pluf
.PATH_SEPARATOR.$app_path
);
//===========================
function debug($what)
{
global $debug;
if ($debug) {
echo($what."\n");
//fprintf($STDERR, "%s", $what);
}
}
//===========================
global $what;
$what = array(
'dry_run' => false,
);
require 'Pluf.php';
Pluf::start($conf);
}
/*
* [CONCEPT] - how it works
*
* backup procedure
*
* 1. backup application will parse big application conf,
* get all versions of migrations of all apps
* AND pick to array all names of used models
*
* how:
*
* a) by parsing relations.php - done (loaded in _PX_MODELS)
* b) by parsing migrations - should be better to find standalone models (?)
* c) combination of two methods above or something else
*
* 2. using ...
* > $json = Pluf_Test_Fixture::dump('YourApp_Model'); // the full table
* ... we get data (and only data; we dont care about sql, because migrations
* could recreate correctly for specific database on the fly)
*
* 3. we save all of that
*
*
* restore procedure
*
* 1) read data (reverse 3)
* 2) get versions of apps and migrate on specific databese
* to correct version to match data (mostly reverse 1 stage)
* 3) restore thanx to Pluf_Test_Fixture::load
* 4) migrate to last versions
*
* and enjoy :D
*
* i think this _could_ work also as migration from one databese to second
* (MySQL <--> PostgreSQL) or not?
*
*
* HINT:
*
* this tool will not repair or work on broken databse !!!
* (realy bad idea is doing migration on SQLite)
*
* it could try dump as most data as it can
* but still your backup and restored DB will be broken
*
* (should mark broken dumps)
*
*/
//=========================================================
function get_apps()
{
global $debug;
global $what;
$apps = Pluf::f('installed_apps');
$versions = array();
foreach ($apps as $app)
{
$m = new Pluf_Migration($app);
$m->display = $debug;
$m->dry_run = $what['dry_run'];
$ver = $m->getAppVersion($app);
$versions[$app] = $ver;
debug("|| {$app}: {$ver}");
}
return( $versions );
} //end get_apps()
function get_models()
{
global $debug;
global $what;
$a = $GLOBALS['_PX_models'];
$modelsA = array();
$modelsB = array();
while (list($mod, $rel) = each($a)) {
array_push( $modelsB, $mod );
$mm = array_pop( $rel ); // contains relate_to_*
foreach ($mm as $m) {
array_push( $modelsA, $m );
}
}
unset( $a );
/*
*
* should this help to save (and reload)
* in right order (???)
*
*/
$modelsA = array_unique( $modelsA ); // independent
$modelsB = array_unique( $modelsB ); // related
$models = array_merge($modelsA, $modelsB);
debug(print_r($models, true));
return($models);
} //end get_apps()
//=========================================================
function backup($dir=null, $full_path=false)
{
global $debug;
global $what;
if (!is_null($dir)) {
if ($full_path == false) {
$dir .= "/".date("Y-m-d_H-i-s");
}
} else {
$dir = date("Y-m-d_H-i-s");
}
debug("# backuped to '{$dir}'");
$versions = get_apps();
$models = get_models();
$versions_js = json_encode($versions);
$models_js = json_encode($models);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
file_put_contents($dir."/versions.json", $versions_js);
file_put_contents($dir."/models.json", $models_js);
$i=0;
$failed = 0;
while (list($k, $mod) = each($models))
{
$i+=1;
$fname = "{$dir}/{$mod}.json";
try {
$string = Pluf_Test_Fixture::dump($mod);
file_put_contents($fname, $string);
debug($mod);
}
catch (Exception $e) {
$failed+=1;
pex("dumping '{$mod}'", $e);
}
}
debug("# status: failed dump {$failed} from {$i}\n");
} // end backup()
function check_file($f) {
$ret = true;
if (! Pluf::fileExists($f)) {
$ret = false;
echo "!! file '{$f}' doesn't exist\n";
}
return $ret;
} // end check_file()
function restore($dir)
{
global $debug;
global $what;
if (is_null($dir)) {
echo "!! don't know what to load\n";
exit( 2 );
}
if (check_file($dir)==false) {
exit( 3 );
}
if (!check_file($dir."/versions.json")
|| !check_file($dir."/models.json")) {
throw new Exception("Version/Models file doesn't exist.");
}
$versions_js = file_get_contents($dir."/versions.json");
$models_js = file_get_contents($dir."/models.json");
$versions = json_decode($versions_js, true);
$models = json_decode($models_js, true);
/* migrate all apps to right versions */
$i=0;
$failed=0;
while (list($app,$ver) = each( $versions ))
{
$i+=1;
try {
$m = new Pluf_Migration($app);
$m->display = $debug;
$m->dry_run = $what['dry_run'];
$m->installApp($app, false, $ver); // should be ok
}
catch (Exception $e) {
$failed+=1;
pex("recreate", $e);
}
}
debug("# status: failed recreate {$failed} from {$i}\n");
/* restore data */
$i=0;
$failed=0;
while (list($k, $mod) = each($models))
{
$i+=1;
try
{
$fname = "{$dir}/{$mod}.json";
if (check_file($fname) == false) {
continue;
}
$content = file_get_contents($fname);
$created = Pluf_Test_Fixture::load($content);
debug($mod);
}
catch (Exception $e) {
$failed+=1;
pex("loading '{$mod}'", $e);
}
}
debug("# status: failed load {$failed} from {$i}\n");
/* migrate to latest versions */
$m = new Pluf_Migration();
$m->display = $debug;
$m->dry_run = $what['dry_run'];
$m->migrate();
} // end restore()
if ($run) {
/*
$dir = date("Y-m-d_H-i-s");
echo $dir."\n";
backup($dir, true);
restore('2009-07-22_22-32-34');
*/
}
// END
b/src/migrate.php
OldNew
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
$search_path = null;
$cg = new Console_Getopt();
$shortoptions = 'aixuc:v:d';
$longoptions = array('app=', 'version=', 'conf=', 'search-path=', 'include-path=');
$args = $cg->readPHPArgv();
function debug($what)
{
global $debug;
if ($debug) {
echo($what."\n");
}
}
.' Upgrade all: migrate.php --conf=path/to/config.php -a'."\n"
.' All to version 3: migrate.php --conf=path/to/config.php -a -v3'."\n"
.' Upgrade MyApp: migrate.php --conf=path/to/config.php --app=MyApp'."\n"
.''."\n"
.'Options:'."\n"
.' c, --conf: Path to the configuration file.'."\n"
.' a: Upgrade all the installed applications.'."\n"
.' v, --version: Upgrade/Downgrade to the given version.'."\n"
.' --app: Application to upgrade/downgrade.'."\n"
}
// Parse the options.
$what = array(
'all' => false,
'app' => '',
'dry_run' => false,
'un-install' => false,
'install' => false,
);
$opts = $ret[0];
case 'a':
$what['all'] = true;
break;
case 'v':
case '--version':
$what['version'] = $o[1];
case 'i':
$what['install'] = true;
break;
}
}
} else {
die();
}
// control the arguments.
if (('' == $what['conf'] or !file_exists($what['conf']))
or ($what['all'] == false and $what['app'] == '')) {
echo 'Error: Missing what to do or config file.'."\n";
usage();
die();
usage();
die();
}
Pluf::start($what['conf']);
if (PHP_SAPI != 'cli' and Pluf::f('migrate_allow_web', false)) {
echo('Error: This script can only be run from the command line.'."\n");
}
$m->dry_run = $what['dry_run'];
if ($what['install']) {
debug('Install '.$app_disp);
$m->install();
debug('Uninstall '.$app_disp);
$m->unInstall();
} else {
debug('Migrate '.$app.' to version '.$what['version']);
$m->migrate($what['version']);
}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
$search_path = null;
$cg = new Console_Getopt();
$shortoptions = 'aixuc:v:db';
$longoptions = array('app=', 'version=', 'conf=', 'search-path=', 'include-path=',
'backup-dir=', 'backup', 'restore');
$args = $cg->readPHPArgv();
function debug($what)
{
global $debug;
if ($debug) {
echo($what."\n");
//fprintf($STDERR, "%s", $what);
}
}
.' Upgrade all: migrate.php --conf=path/to/config.php -a'."\n"
.' All to version 3: migrate.php --conf=path/to/config.php -a -v3'."\n"
.' Upgrade MyApp: migrate.php --conf=path/to/config.php --app=MyApp'."\n"
.' Upgrade with backup: migrate.php --conf=path/to/config.php -b ^^^'."\n"
.' Standalone backup: migrate.php --conf=path/to/config.php --backup '."\n"
.' Restore backup: migrate.php --conf=path/to/config.php --restore --backup-dir=ABC/XYZ'."\n"
.''."\n"
.'Options:'."\n"
.' c, --conf: Path to the configuration file.'."\n"
.' b, --backup: Backup data.'."\n"
.' --backup-dir: Where place backups.'."\n"
.' a: Upgrade all the installed applications.'."\n"
.' v, --version: Upgrade/Downgrade to the given version.'."\n"
.' --app: Application to upgrade/downgrade.'."\n"
}
// Parse the options.
global $what; // need global for backup funcs
$what = array(
'all' => false,
'app' => '',
'dry_run' => false,
'un-install' => false,
'install' => false,
'backup' => false,
'restore' => false,
'backup-dir' => null
);
$opts = $ret[0];
case 'a':
$what['all'] = true;
break;
case 'b':
case '--backup':
$what['backup'] = true;
break;
case '--backup-dir':
$what['backup-dir'] = $o[1];
break;
case 'v':
case '--version':
$what['version'] = $o[1];
case 'i':
$what['install'] = true;
break;
case '--restore':
$what['restore'] = true;
break;
}
}
} else {
die();
}
//var_dump($what); // DEBUG
// control the arguments.
if (('' == $what['conf'] or !file_exists($what['conf'])) // need conf
and ((($what['backup'] == false) or ($what['restore'] == false)) // backup action
and (($what['all'] == false) and ($what['app'] == '') // need -a or --app=
))) {
echo 'Error: Missing what to do or config file.'."\n";
usage();
die();
usage();
die();
}
if ($what['backup'] and $what['restore']) {
echo 'Error: -b, --backup and --restore cannot be used together.'."\n";
usage();
die();
}
if (($what['restore']==true) and is_null($what['restore'])) {
echo 'Error: --restore needs --backup-dir=ABC/XYZ'."\n";
usage();
die();
}
Pluf::start($what['conf']);
if (PHP_SAPI != 'cli' and Pluf::f('migrate_allow_web', false)) {
echo('Error: This script can only be run from the command line.'."\n");
}
$m->dry_run = $what['dry_run'];
if ($what['backup'] || $what['restore']) {
require 'backup.php';
if ($what['backup'] == true) {
if (!is_null($what['backup-dir'])) {
backup($what['backup-dir']);
} else {
backup();
}
}
if ($what['restore'] == true) {
debug("# restore from '".$what['backup-dir']."'");
restore($what['backup-dir']);
exit(0);
}
}
if ($what['install']) {
debug('Install '.$app_disp);
$m->install();
debug('Uninstall '.$app_disp);
$m->unInstall();
} else {
debug("Migrate '{$app}' to version '".$what['version']."'.");
$m->migrate($what['version']);
}