File: horde-db-migrate-component

package info (click to toggle)
php-horde-db 2.4.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: php: 9,205; xml: 1,007; sql: 22; sh: 16; makefile: 2
file content (126 lines) | stat: -rwxr-xr-x 3,487 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env php
<?php
/**
 * Database migration script.
 *
 * Usage: horde-db-migrate-component
 *        --adapter=pdo_mysql
 *        [--host=db.example.com]
 *        [--username=user]
 *        [--password=secret]
 *        [--database=db]
 *        [...]
 *        directory module [(up|down|status|<version>) [debug]]
 *
 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (BSD). If you did
 * not receive this file, see http://www.horde.org/licenses/bsd
 *
 * @author Chuck Hagenbuch <chuck@horde.org>
 * @author Jan Schneider <jan@horde.org>
 */

require_once 'Horde/Autoloader/Default.php';

// Parse command line arguments
array_shift($_SERVER['argv']);
$args = $params = array();
foreach ($_SERVER['argv'] as $arg) {
    if (substr($arg, 0, 2) == '--') {
        $param = substr($arg, 2);
        $param = explode('=', $param, 2);
        if (count($param) != 2) {
            die("The --${param[0]} parameter must have a value.\n");
        }
        $params[$param[0]] = $param[1];
    } else {
        $args[] = $arg;
    }
}
if (empty($args[1])) {
    die("horde-db-migrate-component [--parameter=value ...] directory module [(up|down|status|<version>) [debug]]\n");
}
if (!isset($params['adapter'])) {
    die("The --adapter parameter is required. Other parameters may be required depending on the adapter.\n");
}

$dir = $args[0];
$module = Horde_String::lower($args[1]);
if (!is_dir($dir)) {
    die("$dir is not a migration directory");
}

$action = 'up';
if (!empty($args[2])) {
    switch ($args[2]) {
    case 'up':
    case 'down':
        $action = $args[2];
        break;

    case 'status':
        $action = $args[2];
        break;

    default:
        $action = 'migrate';
        $targetVersion = $args[2];
        break;
    }
}

// Build Horde_Db adapter.
$class = 'Horde_Db_Adapter_' . str_replace(' ', '_', Horde_String::ucwords(str_replace('_', ' ', basename($params['adapter']))));
if (!class_exists($class)) {
    die($params['adapter'] . " is not a valid adapter name.\n");
}
unset($params['adapter']);
try {
    $db = new $class($params);
} catch (Exception $e) {
    die($e->getMessage() . "\n");
}
if (!empty($args[3]) && strpos($args[3], 'debug') !== false) {
    $logger = new Horde_Log_Logger(new Horde_Log_Handler_Stream(STDOUT));
    $db->setLogger($logger);
}
$logger = new Horde_Log_Logger(
    new Horde_Log_Handler_Stream(
        STDOUT, null, new Horde_Log_Formatter_Simple('%message%' . PHP_EOL)));
$migrator = new Horde_Db_Migration_Migrator(
    $db, $logger,
    array('migrationsPath' => $dir,
          'schemaTableName' => $module . '_schema_info'));

echo 'Current schema version: ' . $migrator->getCurrentVersion() . "\n";

try {
    switch ($action) {
    case 'up':
        echo "Migrating DB up.\n";
        $migrator->up();
        break;

    case 'down':
        echo "Migrating DB down.\n";
        $migrator->down();
        break;

    case 'status':
        echo 'Target schema version: ' . $migrator->getTargetVersion() . "\n";
        exit(
            $migrator->getCurrentVersion() == $migrator->getTargetVersion() ? 0 : 1
        );
        break;

    case 'migrate':
        echo 'Migrating DB to schema version ' . $targetVersion . ".\n";
        $migrator->migrate($targetVersion);
        break;
    }
} catch (Exception $e) {
    die($e->getMessage() . "\n");
}

echo 'Ending schema version: ' . $migrator->getCurrentVersion() . "\n";