]>
jfr.im git - z_archive/KronOS.git/blob - system/libraries/Migration.php
1 <?php
defined('BASEPATH') OR exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author EllisLab Dev Team
9 * @copyright Copyright (c) 2006 - 2012, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * All migrations should implement this, forces up() and down() and gives
22 * access to the CI super-global.
24 * @package CodeIgniter
25 * @subpackage Libraries
27 * @author Reactor Engineers
32 protected $_migration_enabled = FALSE;
33 protected $_migration_path = NULL;
34 protected $_migration_version = 0;
36 protected $_error_string = '';
38 public function __construct($config = array())
40 # Only run this constructor on main library load
41 if (get_parent_class($this) !== FALSE)
46 foreach ($config as $key => $val)
48 $this->{'_' . $key
} = $val;
51 log_message('debug', 'Migrations class initialized');
53 // Are they trying to use migrations while it is disabled?
54 if ($this->_migration_enabled
!== TRUE)
56 show_error('Migrations has been loaded but is disabled or set up incorrectly.');
60 $this->_migration_path
== '' AND $this->_migration_path
= APPPATH
. 'migrations/';
62 // Add trailing slash if not set
63 $this->_migration_path
= rtrim($this->_migration_path
, '/').'/';
65 // Load migration language
66 $this->lang
->load('migration');
68 // They'll probably be using dbforge
69 $this->load
->dbforge();
71 // If the migrations table is missing, make it
72 if ( ! $this->db
->table_exists('migrations'))
74 $this->dbforge
->add_field(array(
75 'version' => array('type' => 'INT', 'constraint' => 3),
78 $this->dbforge
->create_table('migrations', TRUE);
80 $this->db
->insert('migrations', array('version' => 0));
84 // --------------------------------------------------------------------
87 * Migrate to a schema version
89 * Calls each migration step required to get to the schema version of
92 * @param int Target schema version
93 * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
95 public function version($target_version)
97 $start = $current_version = $this->_get_version();
98 $stop = $target_version;
100 if ($target_version > $current_version)
113 $method = ($step === 1) ? 'up' : 'down';
114 $migrations = array();
116 // We now prepare to actually DO the migrations
117 // But first let's make sure that everything is the way it should be
118 for ($i = $start; $i != $stop; $i +
= $step)
120 $f = glob(sprintf($this->_migration_path
. '%03d_*.php', $i));
122 // Only one migration per step is permitted
125 $this->_error_string
= sprintf($this->lang
->line('migration_multiple_version'), $i);
129 // Migration step not found
132 // If trying to migrate up to a version greater than the last
133 // existing one, migrate to the last one.
139 // If trying to migrate down but we're missing a step,
140 // something must definitely be wrong.
141 $this->_error_string
= sprintf($this->lang
->line('migration_not_found'), $i);
145 $file = basename($f[0]);
146 $name = basename($f[0], '.php');
148 // Filename validations
149 if (preg_match('/^\d{3}_(\w+)$/', $name, $match))
151 $match[1] = strtolower($match[1]);
153 // Cannot repeat a migration at different steps
154 if (in_array($match[1], $migrations))
156 $this->_error_string
= sprintf($this->lang
->line('migration_multiple_version'), $match[1]);
161 $class = 'Migration_' . ucfirst($match[1]);
163 if ( ! class_exists($class))
165 $this->_error_string
= sprintf($this->lang
->line('migration_class_doesnt_exist'), $class);
169 if ( ! is_callable(array($class, $method)))
171 $this->_error_string
= sprintf($this->lang
->line('migration_missing_'.$method.'_method'), $class);
175 $migrations[] = $match[1];
179 $this->_error_string
= sprintf($this->lang
->line('migration_invalid_filename'), $file);
184 log_message('debug', 'Current migration: ' . $current_version);
186 $version = $i +
($step == 1 ? -1 : 0);
188 // If there is nothing to do so quit
189 if ($migrations === array())
194 log_message('debug', 'Migrating from ' . $method . ' to version ' . $version);
196 // Loop through the migrations
197 foreach ($migrations AS $migration)
199 // Run the migration class
200 $class = 'Migration_' . ucfirst(strtolower($migration));
201 call_user_func(array(new $class, $method));
203 $current_version +
= $step;
204 $this->_update_version($current_version);
207 log_message('debug', 'Finished migrating to '.$current_version);
209 return $current_version;
212 // --------------------------------------------------------------------
215 * Set's the schema to the latest migration
217 * @return mixed true if already latest, false if failed, int if upgraded
219 public function latest()
221 if ( ! $migrations = $this->find_migrations())
223 $this->_error_string
= $this->line
->lang('migration_none_found');
227 $last_migration = basename(end($migrations));
229 // Calculate the last migration step from existing migration
230 // filenames and procceed to the standard version migration
231 return $this->version((int) substr($last_migration, 0, 3));
234 // --------------------------------------------------------------------
237 * Set's the schema to the migration version set in config
239 * @return mixed true if already current, false if failed, int if upgraded
241 public function current()
243 return $this->version($this->_migration_version
);
246 // --------------------------------------------------------------------
251 * @return string Error message returned as a string
253 public function error_string()
255 return $this->_error_string
;
258 // --------------------------------------------------------------------
261 * Set's the schema to the latest migration
263 * @return mixed true if already latest, false if failed, int if upgraded
265 protected function find_migrations()
267 // Load all *_*.php files in the migrations path
268 $files = glob($this->_migration_path
. '*_*.php');
269 $file_count = count($files);
271 for ($i = 0; $i < $file_count; $i++
)
273 // Mark wrongly formatted files as false for later filtering
274 $name = basename($files[$i], '.php');
275 if ( ! preg_match('/^\d{3}_(\w+)$/', $name))
285 // --------------------------------------------------------------------
288 * Retrieves current schema version
290 * @return int Current Migration
292 protected function _get_version()
294 $row = $this->db
->get('migrations')->row();
295 return $row ? $row->version
: 0;
298 // --------------------------------------------------------------------
301 * Stores the current schema version
303 * @param int Migration reached
306 protected function _update_version($migrations)
308 return $this->db
->update('migrations', array(
309 'version' => $migrations
313 // --------------------------------------------------------------------
316 * Enable the use of CI super-global
321 public function __get($var)
323 return get_instance()->$var;
327 /* End of file Migration.php */
328 /* Location: ./system/libraries/Migration.php */