forked from phalcon/phalcon-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrations.php
208 lines (177 loc) · 7.34 KB
/
Migrations.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
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Developer Tools |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Serghei Iakovlev <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon;
use Phalcon\Script\Color;
use Phalcon\Version\Item as VersionItem;
use Phalcon\Mvc\Model\Migration as ModelMigration;
use Phalcon\Mvc\Model\Exception as ModelException;
use Phalcon\Script\ScriptException;
use DirectoryIterator;
/**
* Migrations Class
*
* @package Phalcon
*/
class Migrations
{
/**
* Generate migrations
*
* @param array $options
*
* @throws \Exception
* @todo Refactor
*/
public static function generate(array $options)
{
$tableName = $options['tableName'];
$exportData = $options['exportData'];
$migrationsDir = $options['migrationsDir'];
$originalVersion = $options['originalVersion'];
$force = $options['force'];
$config = $options['config'];
if ($migrationsDir && !file_exists($migrationsDir)) {
mkdir($migrationsDir, 0777, true);
}
if ($originalVersion) {
if (!preg_match('/[a-z0-9](\.[a-z0-9]+)*/', $originalVersion, $matches)) {
throw new \Exception("Version {$originalVersion} is invalid");
}
$originalVersion = $matches[0];
$version = new VersionItem($originalVersion, 3);
if (file_exists($migrationsDir . DIRECTORY_SEPARATOR . $version) && !$force) {
throw new \Exception("Version {$version} is already generated");
}
} else {
$versions = ModelMigration::scanForVersions($migrationsDir);
if (!count($versions)) {
$version = new VersionItem('1.0.0');
} else {
$version = VersionItem::maximum($versions);
$version = $version->addMinor(1);
}
}
if (!file_exists($migrationsDir . DIRECTORY_SEPARATOR . $version)) {
mkdir($migrationsDir . DIRECTORY_SEPARATOR . $version);
}
if (!isset($config->database)) {
throw new \Exception("Cannot load database configuration");
}
ModelMigration::setup($config->database);
ModelMigration::setSkipAutoIncrement($options['no-ai']);
ModelMigration::setMigrationPath($migrationsDir);
if ($tableName == 'all') {
$migrations = ModelMigration::generateAll($version, $exportData);
foreach ($migrations as $tableName => $migration) {
file_put_contents($migrationsDir.'/'.$version.'/'.$tableName.'.php', '<?php '.PHP_EOL.PHP_EOL.$migration);
}
} else {
$migration = ModelMigration::generate($version, $tableName, $exportData);
file_put_contents($migrationsDir.'/'.$version.'/'.$tableName.'.php', '<?php '.PHP_EOL.PHP_EOL.$migration);
}
if (self::isConsole()) {
print Color::success('Version '.$version.' was successfully generated').PHP_EOL;
}
}
/**
* Check if the script is running on Console mode
*
* @return boolean
*/
public static function isConsole()
{
return PHP_SAPI == 'cli';
}
/**
* Run migrations
*
* @param array $options
*
* @throws Exception
* @throws ModelException
* @throws ScriptException
*/
public static function run(array $options)
{
$path = $options['directory'];
$migrationsDir = $options['migrationsDir'];
if (!file_exists($migrationsDir)) {
throw new ModelException('Migrations directory could not found.');
}
$config = $options['config'];
if (!$config instanceof Config) {
throw new ModelException('Internal error. Config should be instance of \Phalcon\Config');
}
$finalVersion = null;
if (isset($options['version']) && $options['version'] !== null) {
$finalVersion = new VersionItem($options['version']);
}
$tableName = 'all';
if (isset($options['tableName'])) {
$tableName = $options['tableName'];
}
$versions = ModelMigration::scanForVersions($migrationsDir);
if (!count($versions)) {
throw new ModelException("Migrations were not found at {$migrationsDir}");
}
// set default final version
if (!$finalVersion) {
$finalVersion = VersionItem::maximum($versions);
}
// read current version
if (is_file($path.'.phalcon')) {
unlink($path.'.phalcon');
mkdir($path.'.phalcon');
}
$migrationFid = $path.'.phalcon/migration-version';
$initialVersion = new VersionItem(file_exists($migrationFid) ? file_get_contents($migrationFid) : null);
if ($initialVersion->getStamp() == $finalVersion->getStamp()) {
return; // nothing to do
}
// init ModelMigration
if (!isset($config->database)) {
throw new ScriptException('Cannot load database configuration');
}
ModelMigration::setup($config->database);
ModelMigration::setMigrationPath($migrationsDir);
$direction = ModelMigration::DIRECTION_FORWARD;
if ($finalVersion->getStamp() < $initialVersion->getStamp()) {
$direction = ModelMigration::DIRECTION_BACK;
}
// run migration
$versionsBetween = VersionItem::between($initialVersion, $finalVersion, $versions);
foreach ($versionsBetween as $version) {
if ($tableName == 'all') {
$iterator = new DirectoryIterator($migrationsDir . DIRECTORY_SEPARATOR . $version);
foreach ($iterator as $fileinfo) {
if (!$fileinfo->isFile() || 0 !== strcasecmp($fileinfo->getExtension(), 'php')) {
continue;
}
ModelMigration::migrate($initialVersion, $version, $fileinfo->getBasename('.php'), $direction);
}
} else {
ModelMigration::migrate($initialVersion, $version, $tableName, $direction);
}
file_put_contents($migrationFid, (string)$version);
print Color::success('Version ' . $version . ' was successfully migrated');
$initialVersion = $version;
}
}
}