aboutsummaryrefslogtreecommitdiff
path: root/cli/migrate.php
blob: 2b43b17b82e9d4d151407d32036cbcde77cc2a57 (plain)
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
#!/usr/bin/env php
<?php
# Lifter007: TODO
# Lifter003: TODO
/*
 * migrate.php - Migrations for Stud.IP
 *
 * Copyright (C) 2006 - Marcus Lunzenauer <mlunzena@uos.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

require_once __DIR__ . '/studip_cli_env.inc.php';

if (isset($_SERVER['argv'])) {
    # check for command line options
    $options = getopt('1:d:lm:t:v');
    if ($options === false) {
        exit(1);
    }

    # check for options
    $single = false;
    $domain = 'studip';
    $list = false;
    $path = $STUDIP_BASE_PATH . '/db/migrations';
    $verbose = false;
    $target = null;

    foreach ($options as $option => $value) {
        switch ($option) {
            case '1':
                $single = (string) $value;
                break;
            case 'd':
                $domain = (string) $value;
                break;
            case 'l':
                $list = true;
                break;
            case 'm':
                $path = $value;
                break;
            case 't':
                $target = (int) $value;
                break;
            case 'v':
                $verbose = true;
                break;
        }
    }

    $version = new DBSchemaVersion($domain);
    $migrator = new Migrator($path, $version, $verbose);

    if ($list) {
        $migrations = $migrator->relevantMigrations($target);

        foreach ($migrations as $number => $migration) {
            $description = $migration->description() ?: '(no description)';
            printf("%3d %s\n", $number, $description);
        }
    } elseif ($single) {
        $direction = 'up';
        if ($single[0] === '-') {
            $direction = 'down';
            $single = substr($single, 1);
        }
        $migrator->execute($single, $direction);
    } else {
        $migrator->migrateTo($target);
    }
}