1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Migrations class. |
5
|
|
|
*/ |
6
|
|
|
class Migrations |
7
|
|
|
{ |
8
|
|
|
protected $config; |
9
|
|
|
protected $driver; |
10
|
|
|
protected $migrations; |
11
|
|
|
public $output = NULL; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Intialize migration library |
15
|
|
|
* |
16
|
|
|
* @param bool Do we want output of migration steps? |
17
|
|
|
* @param string Database group |
18
|
|
|
*/ |
19
|
|
|
public function __construct($config = NULL) |
20
|
|
|
{ |
21
|
|
|
$this->config = arr::merge(Kohana::$config->load('migrations')->as_array(), (array) $config); |
22
|
|
|
|
23
|
|
|
$database = Kohana::$config->load('database.'.Arr::get(Kohana::$config->load('migrations'), 'database', 'default')); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// Set the driver class name |
26
|
|
|
$driver_name = in_array($database['type'], array('PDO', 'MySQL')) ? 'Mysql' : ucfirst($database['type']); |
27
|
|
|
$driver = 'Migration_Driver_'.$driver_name; |
28
|
|
|
|
29
|
|
|
// Create the database connection instance |
30
|
|
|
$this->driver = new $driver(Arr::get(Kohana::$config->load('migrations'), 'database', 'default')); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$this->driver->versions()->init(); |
33
|
|
|
|
34
|
|
|
if( ! is_dir($this->config['path'])) |
35
|
|
|
{ |
36
|
|
|
mkdir($this->config['path'], 0777, TRUE); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function set_executed($version) |
41
|
|
|
{ |
42
|
|
|
$this->driver->versions()->set($version); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function set_unexecuted($version) |
46
|
|
|
{ |
47
|
|
|
$this->driver->versions()->clear($version); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function generate_new_migration_file($name, $actions_template = NULL) |
51
|
|
|
{ |
52
|
|
|
$actions = new Migration_Actions($this->driver); |
53
|
|
|
|
54
|
|
|
if ($actions_template) |
55
|
|
|
{ |
56
|
|
|
$actions->template(getcwd().DIRECTORY_SEPARATOR.$actions_template); |
57
|
|
|
} |
58
|
|
|
else |
59
|
|
|
{ |
60
|
|
|
$actions->parse($name); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$template = file_get_contents(Kohana::find_file('templates', 'migration', 'tpl')); |
64
|
|
|
$class_name = str_replace(' ', '_', ucwords(str_replace('_',' ',$name))); |
65
|
|
|
$filename = sprintf("%d_$name.php", time()); |
66
|
|
|
|
67
|
|
|
file_put_contents( |
68
|
|
|
$this->config['path'].DIRECTORY_SEPARATOR.$filename, |
69
|
|
|
strtr($template, array( |
70
|
|
|
'{up}' => join("\n", array_map('Migrations::indent', $actions->up)), |
71
|
|
|
'{down}' => join("\n", array_map('Migrations::indent', $actions->down)), |
72
|
|
|
'{class_name}' => $class_name |
73
|
|
|
)) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return $filename; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
static function indent($action) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return "\t\t$action"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Loads a migration |
86
|
|
|
* |
87
|
|
|
* @param integer Migration version number |
88
|
|
|
* @return Migration_Core Class object |
89
|
|
|
*/ |
90
|
|
|
public function load_migration($version) |
91
|
|
|
{ |
92
|
|
|
$f = glob(sprintf($this->config['path'] . DIRECTORY_SEPARATOR . '%d_*.php', $version)); |
93
|
|
|
|
94
|
|
|
if (count($f) > 1) |
95
|
|
|
throw new Migration_Exception('Only one migration per step is permitted, there are :count of version :version', array(':count' => count($f), ':version' => $version)); |
96
|
|
|
|
97
|
|
|
if (count($f) == 0) |
98
|
|
|
throw new Migration_Exception('Migration step not found with version :version', array(":version" => $version)); |
99
|
|
|
|
100
|
|
|
$file = basename($f[0]); |
101
|
|
|
$name = basename($f[0], EXT); |
102
|
|
|
|
103
|
|
|
// Filename validations |
104
|
|
|
if ( ! preg_match('/^\d+_(\w+)$/', $name, $match)) |
105
|
|
|
throw new Migration_Exception('Invalid filename :file', array(':file' => $file)); |
106
|
|
|
|
107
|
|
|
$match[1] = strtolower($match[1]); |
108
|
|
|
|
109
|
|
|
include_once $f[0]; |
110
|
|
|
$class = ucfirst($match[1]); |
111
|
|
|
|
112
|
|
|
if ( ! class_exists($class)) |
113
|
|
|
throw new Migration_Exception('Migration class :class does not exist', array( ':class' => $class)); |
114
|
|
|
|
115
|
|
|
return new $class($this->config); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retrieves all the timestamps of the migration files |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function get_migrations() |
124
|
|
|
{ |
125
|
|
|
if ( ! $this->migrations) |
126
|
|
|
{ |
127
|
|
|
$migrations = glob($this->config['path'] . DIRECTORY_SEPARATOR . '*' . EXT); |
128
|
|
|
$ids = array(); |
129
|
|
|
foreach ((array) $migrations as $file) |
130
|
|
|
{ |
131
|
|
|
$name = basename($file, EXT); |
132
|
|
|
$matches = array(); |
133
|
|
|
if ( preg_match('/^(\d+)_(\w+)$/', $name, $matches)) |
134
|
|
|
{ |
135
|
|
|
$ids[] = intval($matches[1]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
$this->migrations = $ids; |
139
|
|
|
} |
140
|
|
|
return $this->migrations; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function clear_all() |
144
|
|
|
{ |
145
|
|
|
$this->driver->clear_all(); |
146
|
|
|
$this->driver->versions()->clear_all(); |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function get_executed_migrations() |
151
|
|
|
{ |
152
|
|
|
return $this->driver->versions()->get(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function get_unexecuted_migrations() |
156
|
|
|
{ |
157
|
|
|
return array_diff($this->get_migrations(), $this->get_executed_migrations()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
protected function execute($version, $direction, $dry_run) |
161
|
|
|
{ |
162
|
|
|
$migration = $this->load_migration($version)->dry_run($dry_run); |
163
|
|
|
|
164
|
|
|
$this->log($version.' '.get_class($migration).' : migrating '.$direction.($dry_run ? " -- Dry Run" : '')); |
165
|
|
|
$start = microtime(TRUE); |
166
|
|
|
|
167
|
|
|
switch ($direction) |
168
|
|
|
{ |
169
|
|
|
case 'down': |
170
|
|
|
$migration->down(); |
171
|
|
|
if ( ! $dry_run) |
172
|
|
|
{ |
173
|
|
|
$this->set_unexecuted($version); |
174
|
|
|
} |
175
|
|
|
break; |
176
|
|
|
|
177
|
|
|
case 'up': |
178
|
|
|
$migration->up(); |
179
|
|
|
if ( ! $dry_run) |
180
|
|
|
{ |
181
|
|
|
$this->set_executed($version); |
182
|
|
|
} |
183
|
|
|
break; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$end = microtime(TRUE); |
187
|
|
|
$this->log($version.' '.get_class($migration).' : migrated ('.number_format($end - $start, 4).'s)'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function execute_all($up = array(), $down = array(), $dry_run = FALSE) |
191
|
|
|
{ |
192
|
|
|
if ( ! count($down) AND ! count($up)) |
193
|
|
|
{ |
194
|
|
|
$this->log("Nothing to do"); |
195
|
|
|
} |
196
|
|
|
else |
197
|
|
|
{ |
198
|
|
|
foreach ($down as $version) |
199
|
|
|
{ |
200
|
|
|
$this->execute($version, 'down', $dry_run); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
foreach ($up as $version) |
204
|
|
|
{ |
205
|
|
|
$this->execute($version, 'up', $dry_run); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function log($message) |
211
|
|
|
{ |
212
|
|
|
if ($this->config['log']) |
213
|
|
|
{ |
214
|
|
|
call_user_func($this->config['log'], $message); |
215
|
|
|
} |
216
|
|
|
else |
217
|
|
|
{ |
218
|
|
|
echo $message."\n"; |
219
|
|
|
ob_flush(); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: