forked from cloudmanic/codeigniter-migrations
-
Notifications
You must be signed in to change notification settings - Fork 9
/
migrate.php
268 lines (220 loc) · 6.39 KB
/
migrate.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Migrations
*
* An open source utility for Code Igniter inspired by Ruby on Rails
*
* @package Migrations
* @author Mat’as Montes
*
* Rewritten into a lib By:
* Spicer Matthews <[email protected]>
* Cloudmanic Labs, LLC
* http://www.cloudmanic.com
*
*/
// ------------------------------------------------------------------------
/**
* Migrate Class
*
* Utility main controller.
*
* @package Migrations
* @author Mat’as Montes
*/
class Migrate
{
var $migrations_enabled = FALSE;
var $migrations_path = ".";
var $verbose = FALSE;
var $error = "";
function Migrate()
{
$this->CI =& get_instance();
$this->CI->config->load('migrate');
if(! $this->CI->config->item('migrations_enabled'))
show_404();
if(! $this->CI->config->item('migrations_path'))
show_404();
$this->migrations_path = $this->CI->config->item('migrations_path');
if($this->migrations_path != '' && substr($this->migrations_path, -1) != '/')
$this->migrations_path .= '/';
$this->CI->load->dbforge();
if(! $this->CI->db->table_exists('schema_version')) {
$cols = array(
'version' => array('type' => 'INT', 'constraint' => 3),
);
$this->CI->dbforge->add_field($cols);
$this->CI->dbforge->create_table('schema_version', TRUE);
$this->CI->db->insert('schema_version', array('version' => 0));
}
}
//
// This will set if there should be verbose output or not.
//
function setverbose($state)
{
$this->verbose = $state;
}
/**
* Installs the schema up to the last version
*
* @access public
* @return void Outputs a report of the installation
*/
function install()
{
$files = glob($this->migrations_path."*".EXT);
$file_count = count($files);
for($i=0; $i < $file_count; $i++)
{
// Mark wrongly formatted files as FALSE for later filtering
$name = basename($files[$i],EXT);
if(!preg_match('/^\d{3}_(\w+)$/',$name)) $files[$i] = FALSE;
}
$migrations = array_filter($files);
if(! empty($migrations)) {
sort($migrations);
$last_migration = basename(end($migrations));
// Calculate the last migration step from existing migration
// filenames and procceed to the standard version migration
$last_version = substr($last_migration,0,3);
return $this->version(intval($last_version,10));
} else {
$this->error = $this->CI->lang->line("no_migrations_found");
return 0;
}
}
// --------------------------------------------------------------------
/**
* Migrate to a schema version
*
* Calls each migration step required to get to the schema version of
* choice
*
* @access public
* @param $version integer Target schema version
* @return void Outputs a report of the migration
*/
function version($version = 0)
{
$schema_version = $this->_get_schema_version();
$start = $schema_version;
$stop = $version;
if($version > $schema_version) {
// Moving Up
$start++;
$stop++;
$step = 1;
} else {
// Moving Down
$step = -1;
}
$method = $step == 1 ? 'up' : 'down';
$migrations = array();
// We now prepare to actually DO the migrations
// But first let's make sure that everything is the way it should be
for($i=$start; $i != $stop; $i += $step)
{
$f = glob(sprintf($this->migrations_path . '%03d_*.php', $i));
// Only one migration per step is permitted
if(count($f) > 1) {
$this->error = sprintf($this->CI->lang->line("multiple_migrations_version"),$i);
return 0;
}
// Migration step not found
if(count($f) == 0) {
// If trying to migrate up to a version greater than the last
// existing one, migrate to the last one.
if ($step == 1)
break;
// If trying to migrate down but we're missing a step,
// something must definitely be wrong.
$this->error = sprintf($this->CI->lang->line("migration_not_found"),$i);
return 0;
}
$file = basename($f[0]);
$name = basename($f[0],EXT);
// Filename validations
if(preg_match('/^\d{3}_(\w+)$/', $name, $match)) {
$match[1] = strtolower($match[1]);
// Cannot repeat a migration at different steps
if(in_array($match[1], $migrations)) {
$this->error = sprintf($this->CI->lang->line("multiple_migrations_name"),$match[1]);
return 0;
}
include $f[0];
$class = ucfirst($match[1]);
if(! class_exists($class)) {
$this->error = sprintf($this->CI->lang->line("migration_class_doesnt_exist"),$class);
return 0;
}
if(! is_callable(array($class,"up")) || ! is_callable(array($class,"down"))) {
$this->error = sprintf($this->CI->lang->line('wrong_migration_interface'),$class);
return 0;
}
$migrations[] = $match[1];
} else {
$this->error = sprintf($this->CI->lang->line("invalid_migration_filename"),$file);
return 0;
}
}
$version = $i + ($step == 1 ? -1 : 0);
// If there is any migration to proccess
if(count($migrations)) {
if($this->verbose) {
echo "<p>Current schema version: ".$schema_version."<br/>";
echo "Moving ".$method." to version ".$version."</p>";
echo "<hr/>";
}
// Loop Through the migrations
foreach($migrations AS $m)
{
if($this->verbose) {
echo "$m:<br />";
echo "<blockquote>";
}
$class = ucfirst($m);
call_user_func(array($class, $method));
if($this->verbose) {
echo "</blockquote>";
echo "<hr/>";
}
$schema_version += $step;
$this->_update_schema_version($schema_version);
}
if($this->verbose)
echo "<p>All done. Schema is at version $schema_version.</p>";
} else
if($this->verbose)
echo "Nothing to do, bye!\n";
return 1;
}
// --------------------------------------------------------------------
/**
* Retrieves current schema version
*
* @access private
* @return integer Current Schema version
*/
function _get_schema_version()
{
$query = $this->CI->db->get('schema_version');
$row = $query->row();
return $row->version;
}
// --------------------------------------------------------------------
/**
* Stores the current schema version
*
* @access private
* @param $schema_version integer Schema version reached
* @return void Outputs a report of the migration
*/
function _update_schema_version($schema_version)
{
$data = array('version' => $schema_version);
$this->CI->db->update('schema_version', $data);
return 1;
}
}