srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Migration.php

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
352
353
354
355
356
357
358
359
360
361
362
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * A class to manage the migration of the code from one version to
 * another, upward or downward.
 *
 * You can directly use the migrate.php script.
 *
 * Simple example usage:
 *
 * <pre>
 * $m = new Pluf_Migration('MyApp');
 * $m->migrate();
 *
 * // Install the application MyApp
 * $m = new Pluf_Migration('MyApp');
 * $m->install();
 * // Uninstall the application MyApp
 * $m->unInstall();
 *
 * $m = new Pluf_Migration();
 * $m->migrate(); // migrate all the installed app to the newest version.
 *
 * $m = new Pluf_Migration();
 * $m->migrate(3); // migrate (upgrade or downgrade) to version 3
 * </pre>
 *
 */
class Pluf_Migration
{
    protected $app = ''; /**< Application beeing migrated. */
    public $apps = array(); /**< Applications which are going to be migrated. */
    public $to_version = null; /**< Target version for the migration. */
    public $dry_run = false; /**< Set to true to not act. */
    public $display = false; /**< Display on the console what is done. */
 
    /**
     * Create a new migration.
     *
     * @param mixed Application or array of applications to migrate.
     */
    public function __construct($app=null)
    {
        if (!is_null($app)) {
            if (is_array($app)) {
                $this->apps = $app;
            } else {
                $this->apps = array($app);
            }
        } else {
            $this->apps = Pluf::f('installed_apps');
        }
    }
 
 
    /**
     * Install the application.
     *
     * Basically run the base install function for each application
     * and then set the version to the latest migration.
     */
    public function install()
    {
        foreach ($this->apps as $app) {
            $this->installApp($app);
        }
        return true;
    }
 
    /**
     * Uninstall the application.
     */
    public function unInstall()
    {
        $apps = array_reverse($this->apps);
        foreach ($apps as $app) {
            $this->installApp($app, true);
        }
    }
 
    /**
     * Backup the application.
     *
     * @param string Path to the backup folder
     * @param string Backup name (null)
     */
    public function backup($path, $name=null)
    {
        foreach ($this->apps as $app) {
            $func = $app.'_Migrations_Backup_run';
            Pluf::loadFunction($func);
            if ($this->display) {
                echo($func."\n");
            }
            if (!$this->dry_run) {
                $ret = $func($path, $name);
            }
        }
        return true;
    }
     
    /**
     * Restore the application.
     *
     * @param string Path to the backup folder
     * @param string Backup name
     */
    public function restore($path, $name)
    {
        foreach ($this->apps as $app) {
            $func = $app.'_Migrations_Backup_restore';
            Pluf::loadFunction($func);
            if ($this->display) {
                echo($func."\n");
            }
            if (!$this->dry_run) {
                $ret = $func($path, $name);
            }
        }
        return true;
    }
     
    /**
     * Run the migration.
     *
     */
    public function migrate($to_version=null)
    {
        $this->to_version = $to_version;
        foreach ($this->apps as $app) {
            $this->app = $app;
            $migrations = $this->findMigrations();
            // The run will throw an exception in case of error.
            $this->runMigrations($migrations);
        }
        return true;
    }
 
    /**
     * 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';
        } else {
            $func = $app.'_Migrations_Install_setup';
        }
        $ret = true;
        Pluf::loadFunction($func);
        if ($this->display) {
            echo($func."\n");
        }
        if (!$this->dry_run) {
            $ret = $func(); // Run the install/uninstall
            if (!$uninstall) {
                //
                $this->app = $app;
                $migrations = $this->findMigrations();
                if (count($migrations) > 0) {
                    $to_version = max(array_keys($migrations));
                } else {
                    $to_version = 0;
                }
                $this->setAppVersion($app, $to_version);
            } else {
                if ($app != 'Pluf') {
                    // If Pluf we do not have the schema info table
                    // anymore
                    $this->delAppInfo($app);
                }
            }
        }
        return $ret;
    }
 
 
    /**
     * Find the migrations for the current app.
     *
     * @return array Migrations names indexed by order.
     */
    public function findMigrations()
    {
        $migrations = array();
        if (false !== ($mdir = Pluf::fileExists($this->app.'/Migrations'))) {
            $dir = new DirectoryIterator($mdir);
            foreach($dir as $file) {
                $matches = array();
                if (!$file->isDot() && !$file->isDir()
                    && preg_match('#^(\d+)#', $file->getFilename(), $matches)) {
                    $info = pathinfo($file->getFilename());
                    $migrations[(int)$matches[1]] = $info['filename'];
                }
            }
        }
        return $migrations;
    }
 
    /**
     * Run the migrations.
     *
     * From an array of possible migrations, it will first get the
     * current version of the app and then based on $this->to_version
     * will run the migrations in the right order or do nothing if
     * nothing to be done.
     *
     * @param array Possible migrations.
     */
    public function runMigrations($migrations)
    {
        if (empty($migrations)) {
            return;
        }
        $current = $this->getAppVersion($this->app);
        if ($this->to_version === null) {
            $to_version = max(array_keys($migrations));
        } else {
            $to_version = $this->to_version;
        }
        if ($to_version == $current) {
            return; // Nothing to do
        }
        $the_way = 'up'; // Tribute to Pat Metheny
        if ($to_version > $current) {
            // upgrade
            $min = $current + 1;
            $max = $to_version;
        } else {
            // downgrade
            $the_way = 'do';
            $max = $current;
            $min = $to_version + 1;
        }
        // Filter the migrations
        $to_run = array();
        foreach ($migrations as $order=>$name) {
            if ($order < $min or $order > $max) {
                continue;
            }
            if ($the_way == 'up') {
                $to_run[] = array($order, $name);
            } else {
                array_unshift($to_run, array($order, $name));
            }
        }
        asort($to_run);
        // Run the migrations
        foreach ($to_run as $migration) {
            $this->runMigration($migration, $the_way);
        }
    }
 
    /**
     * Run the given migration.
     */
    public function runMigration($migration, $the_way='up')
    {
        $target_version = ($the_way == 'up') ? $migration[0] : $migration[0]-1;
        if ($this->display) {
            echo($migration[0].' '.$migration[1].' '.$the_way."\n");
        }
        if (!$this->dry_run) {
            if ($the_way == 'up') {
                $func = $this->app.'_Migrations_'.$migration[1].'_up';
            } else {
                $func = $this->app.'_Migrations_'.$migration[1].'_down';
            }
            Pluf::loadFunction($func);
            $func(); // Real migration run
            $this->setAppVersion($this->app, $target_version);
        }
    }
 
    /**
     * Set the application version.
     *
     * @param string Application
     * @param int Version
     * @return true
     */
    public function setAppVersion($app, $version)
    {
        $gschema = new Pluf_DB_SchemaInfo();
        $sql = new Pluf_SQL('application=%s', $app);
        $appinfo = $gschema->getList(array('filter' => $sql->gen()));
        if ($appinfo->count() == 1) {
            $appinfo[0]->version = $version;
            $appinfo[0]->update();
        } else {
            $schema = new Pluf_DB_SchemaInfo();
            $schema->application = $app;
            $schema->version = $version;
            $schema->create();
        }
        return true;
    }
 
    /**
     * Remove the application information.
     *
     * @param string Application
     * @return true
     */
    public function delAppInfo($app)
    {
        $gschema = new Pluf_DB_SchemaInfo();
        $sql = new Pluf_SQL('application=%s', $app);
        $appinfo = $gschema->getList(array('filter' => $sql->gen()));
        if ($appinfo->count() == 1) {
            $appinfo[0]->delete();
        }
        return true;
    }
 
     
 
    /**
     * Get the current version of the app.
     *
     * @param string Application.
     * @return int Version.
     */
    public function getAppVersion($app)
    {
        try {
            $db =& Pluf::db();
            $res = $db->select('SELECT version FROM '.$db->pfx.'schema_info WHERE application='.$db->esc($app));
            return (int) $res[0]['version'];
        } catch (Exception $e) {
            // We should not be here, only in the case of nothing
            // installed. I am not sure if this is a good way to
            // handle this border case anyway. Maybe better to have an
            // 'install' method to run all the migrations in order.
            return 0;
        }
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.11528s using 11 queries.