summaryrefslogtreecommitdiff
path: root/src/Database/Migration/Migrate.php
blob: c8ee9456658e259020228062cbde6abe8f208acb (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
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
<?php

namespace Engelsystem\Database\Migration;

use Engelsystem\Application;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder as SchemaBuilder;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Migrate
{
    const UP = 'up';
    const DOWN = 'down';

    /** @var Application */
    protected $app;

    /** @var SchemaBuilder */
    protected $schema;

    /** @var callable */
    protected $output;

    /** @var string */
    protected $table = 'migrations';

    /**
     * Migrate constructor
     *
     * @param SchemaBuilder $schema
     * @param Application   $app
     */
    public function __construct(SchemaBuilder $schema, Application $app)
    {
        $this->app = $app;
        $this->schema = $schema;
        $this->output = function () {
        };
    }

    /**
     * Run a migration
     *
     * @param string $path
     * @param string $type (up|down)
     * @param bool   $oneStep
     */
    public function run($path, $type = self::UP, $oneStep = false)
    {
        $this->initMigration();
        $migrations = $this->getMigrations($path);
        $migrated = $this->getMigrated();

        if ($type == self::DOWN) {
            $migrations = array_reverse($migrations, true);
        }

        foreach ($migrations as $file => $migration) {
            if (
                ($type == self::UP && $migrated->contains('migration', $migration))
                || ($type == self::DOWN && !$migrated->contains('migration', $migration))
            ) {
                call_user_func($this->output, 'Skipping ' . $migration);
                continue;
            }

            call_user_func($this->output, 'Migrating ' . $migration . ' (' . $type . ')');

            $this->migrate($file, $migration, $type);
            $this->setMigrated($migration, $type);

            if ($oneStep) {
                return;
            }
        }
    }

    /**
     * Setup migration tables
     */
    public function initMigration()
    {
        if ($this->schema->hasTable($this->table)) {
            return;
        }

        $this->schema->create($this->table, function (Blueprint $table) {
            $table->increments('id');
            $table->string('migration');
        });
    }

    /**
     * Get all migrated migrations
     *
     * @return Collection
     */
    protected function getMigrated()
    {
        return $this->getTableQuery()->get();
    }

    /**
     * Migrate a migration
     *
     * @param string $file
     * @param string $migration
     * @param string $type (up|down)
     */
    protected function migrate($file, $migration, $type = self::UP)
    {
        require_once $file;

        $className = Str::studly(preg_replace('/\d+_/', '', $migration));
        /** @var Migration $class */
        $class = $this->app->make('Engelsystem\\Migrations\\' . $className);

        if (method_exists($class, $type)) {
            $class->{$type}();
        }
    }

    /**
     * Set a migration to migrated
     *
     * @param string $migration
     * @param string $type (up|down)
     */
    protected function setMigrated($migration, $type = self::UP)
    {
        $table = $this->getTableQuery();

        if ($type == self::DOWN) {
            $table->where(['migration' => $migration])->delete();
            return;
        }

        $table->insert(['migration' => $migration]);
    }

    /**
     * Get a list of migration files
     *
     * @param string $dir
     * @return array
     */
    protected function getMigrations($dir)
    {
        $files = $this->getMigrationFiles($dir);

        $migrations = [];
        foreach ($files as $dir) {
            $name = str_replace('.php', '', basename($dir));
            $migrations[$dir] = $name;
        }

        asort($migrations);
        return $migrations;
    }

    /**
     * List all migration files from the given directory
     *
     * @param string $dir
     * @return array
     */
    protected function getMigrationFiles($dir)
    {
        return glob($dir . '/*_*.php');
    }

    /**
     * Init a table query
     *
     * @return Builder
     */
    protected function getTableQuery()
    {
        return $this->schema->getConnection()->table($this->table);
    }

    /**
     * Set the output function
     *
     * @param callable $output
     */
    public function setOutput(callable $output)
    {
        $this->output = $output;
    }
}