summaryrefslogtreecommitdiffstats
path: root/application/clicommands/MigrationCommand.php
blob: 6a4d0027420d8a3edc3c28b15f13ee1ffa96daa8 (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
<?php

namespace Icinga\Module\Director\Clicommands;

use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\Db\Migrations;

/**
 * Handle DB migrations
 *
 * This command retrieves information about unapplied database migration and
 * helps applying them.
 */
class MigrationCommand extends Command
{
    /**
     * Check whether there are pending migrations
     *
     * This is mostly for automation, so one could create a Puppet manifest
     * as follows:
     *
     *     exec { 'Icinga Director DB migration':
     *       command => 'icingacli director migration run',
     *       onlyif  => 'icingacli director migration pending',
     *     }
     *
     * Exit code 0 means that there are pending migrations, code 1 that there
     * are no such. Use --verbose for human readable output
     */
    public function pendingAction()
    {
        if ($count = $this->migrations()->countPendingMigrations()) {
            if ($this->isVerbose) {
                if ($count === 1) {
                    echo "There is 1 pending migration\n";
                } else {
                    printf("There are %d pending migrations\n", $count);
                }
            }

            exit(0);
        } else {
            if ($this->isVerbose) {
                echo "There are no pending migrations\n";
            }

            exit(1);
        }
    }

    /**
     * Run any pending migrations
     *
     * All pending migrations will be silently applied
     */
    public function runAction()
    {
        $this->migrations()->applyPendingMigrations();
        exit(0);
    }

    protected function migrations()
    {
        return new Migrations($this->db());
    }
}