PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/system/libraries/Migration.php

https://gitlab.com/BeyondeLabs/tweet-daily
PHP | 329 lines | 163 code | 58 blank | 108 comment | 21 complexity | 621b816fcb5408a974c1b45d555e38af MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2006 - 2014, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Migration Class
  18. *
  19. * All migrations should implement this, forces up() and down() and gives
  20. * access to the CI super-global.
  21. *
  22. * @package CodeIgniter
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author Reactor Engineers
  26. * @link
  27. */
  28. class CI_Migration {
  29. protected $_migration_enabled = FALSE;
  30. protected $_migration_path = NULL;
  31. protected $_migration_version = 0;
  32. protected $_error_string = '';
  33. public function __construct($config = array())
  34. {
  35. # Only run this constructor on main library load
  36. if (get_parent_class($this) !== FALSE)
  37. {
  38. return;
  39. }
  40. foreach ($config as $key => $val)
  41. {
  42. $this->{'_' . $key} = $val;
  43. }
  44. log_message('debug', 'Migrations class initialized');
  45. // Are they trying to use migrations while it is disabled?
  46. if ($this->_migration_enabled !== TRUE)
  47. {
  48. show_error('Migrations has been loaded but is disabled or set up incorrectly.');
  49. }
  50. // If not set, set it
  51. $this->_migration_path == '' AND $this->_migration_path = APPPATH . 'migrations/';
  52. // Add trailing slash if not set
  53. $this->_migration_path = rtrim($this->_migration_path, '/').'/';
  54. // Load migration language
  55. $this->lang->load('migration');
  56. // They'll probably be using dbforge
  57. $this->load->dbforge();
  58. // If the migrations table is missing, make it
  59. if ( ! $this->db->table_exists('migrations'))
  60. {
  61. $this->dbforge->add_field(array(
  62. 'version' => array('type' => 'INT', 'constraint' => 3),
  63. ));
  64. $this->dbforge->create_table('migrations', TRUE);
  65. $this->db->insert('migrations', array('version' => 0));
  66. }
  67. }
  68. // --------------------------------------------------------------------
  69. /**
  70. * Migrate to a schema version
  71. *
  72. * Calls each migration step required to get to the schema version of
  73. * choice
  74. *
  75. * @param int Target schema version
  76. * @return mixed TRUE if already latest, FALSE if failed, int if upgraded
  77. */
  78. public function version($target_version)
  79. {
  80. $start = $current_version = $this->_get_version();
  81. $stop = $target_version;
  82. if ($target_version > $current_version)
  83. {
  84. // Moving Up
  85. ++$start;
  86. ++$stop;
  87. $step = 1;
  88. }
  89. else
  90. {
  91. // Moving Down
  92. $step = -1;
  93. }
  94. $method = ($step === 1) ? 'up' : 'down';
  95. $migrations = array();
  96. // We now prepare to actually DO the migrations
  97. // But first let's make sure that everything is the way it should be
  98. for ($i = $start; $i != $stop; $i += $step)
  99. {
  100. $f = glob(sprintf($this->_migration_path . '%03d_*.php', $i));
  101. // Only one migration per step is permitted
  102. if (count($f) > 1)
  103. {
  104. $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $i);
  105. return FALSE;
  106. }
  107. // Migration step not found
  108. if (count($f) == 0)
  109. {
  110. // If trying to migrate up to a version greater than the last
  111. // existing one, migrate to the last one.
  112. if ($step == 1)
  113. {
  114. break;
  115. }
  116. // If trying to migrate down but we're missing a step,
  117. // something must definitely be wrong.
  118. $this->_error_string = sprintf($this->lang->line('migration_not_found'), $i);
  119. return FALSE;
  120. }
  121. $file = basename($f[0]);
  122. $name = basename($f[0], '.php');
  123. // Filename validations
  124. if (preg_match('/^\d{3}_(\w+)$/', $name, $match))
  125. {
  126. $match[1] = strtolower($match[1]);
  127. // Cannot repeat a migration at different steps
  128. if (in_array($match[1], $migrations))
  129. {
  130. $this->_error_string = sprintf($this->lang->line('migration_multiple_version'), $match[1]);
  131. return FALSE;
  132. }
  133. include $f[0];
  134. $class = 'Migration_' . ucfirst($match[1]);
  135. if ( ! class_exists($class))
  136. {
  137. $this->_error_string = sprintf($this->lang->line('migration_class_doesnt_exist'), $class);
  138. return FALSE;
  139. }
  140. if ( ! is_callable(array($class, $method)))
  141. {
  142. $this->_error_string = sprintf($this->lang->line('migration_missing_'.$method.'_method'), $class);
  143. return FALSE;
  144. }
  145. $migrations[] = $match[1];
  146. }
  147. else
  148. {
  149. $this->_error_string = sprintf($this->lang->line('migration_invalid_filename'), $file);
  150. return FALSE;
  151. }
  152. }
  153. log_message('debug', 'Current migration: ' . $current_version);
  154. $version = $i + ($step == 1 ? -1 : 0);
  155. // If there is nothing to do so quit
  156. if ($migrations === array())
  157. {
  158. return TRUE;
  159. }
  160. log_message('debug', 'Migrating from ' . $method . ' to version ' . $version);
  161. // Loop through the migrations
  162. foreach ($migrations AS $migration)
  163. {
  164. // Run the migration class
  165. $class = 'Migration_' . ucfirst(strtolower($migration));
  166. call_user_func(array(new $class, $method));
  167. $current_version += $step;
  168. $this->_update_version($current_version);
  169. }
  170. log_message('debug', 'Finished migrating to '.$current_version);
  171. return $current_version;
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Set's the schema to the latest migration
  176. *
  177. * @return mixed true if already latest, false if failed, int if upgraded
  178. */
  179. public function latest()
  180. {
  181. if ( ! $migrations = $this->find_migrations())
  182. {
  183. $this->_error_string = $this->lang->line('migration_none_found');
  184. return false;
  185. }
  186. $last_migration = basename(end($migrations));
  187. // Calculate the last migration step from existing migration
  188. // filenames and procceed to the standard version migration
  189. return $this->version((int) substr($last_migration, 0, 3));
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Set's the schema to the migration version set in config
  194. *
  195. * @return mixed true if already current, false if failed, int if upgraded
  196. */
  197. public function current()
  198. {
  199. return $this->version($this->_migration_version);
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Error string
  204. *
  205. * @return string Error message returned as a string
  206. */
  207. public function error_string()
  208. {
  209. return $this->_error_string;
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Set's the schema to the latest migration
  214. *
  215. * @return mixed true if already latest, false if failed, int if upgraded
  216. */
  217. protected function find_migrations()
  218. {
  219. // Load all *_*.php files in the migrations path
  220. $files = glob($this->_migration_path . '*_*.php');
  221. $file_count = count($files);
  222. for ($i = 0; $i < $file_count; $i++)
  223. {
  224. // Mark wrongly formatted files as false for later filtering
  225. $name = basename($files[$i], '.php');
  226. if ( ! preg_match('/^\d{3}_(\w+)$/', $name))
  227. {
  228. $files[$i] = FALSE;
  229. }
  230. }
  231. sort($files);
  232. return $files;
  233. }
  234. // --------------------------------------------------------------------
  235. /**
  236. * Retrieves current schema version
  237. *
  238. * @return int Current Migration
  239. */
  240. protected function _get_version()
  241. {
  242. $row = $this->db->get('migrations')->row();
  243. return $row ? $row->version : 0;
  244. }
  245. // --------------------------------------------------------------------
  246. /**
  247. * Stores the current schema version
  248. *
  249. * @param int Migration reached
  250. * @return bool
  251. */
  252. protected function _update_version($migrations)
  253. {
  254. return $this->db->update('migrations', array(
  255. 'version' => $migrations
  256. ));
  257. }
  258. // --------------------------------------------------------------------
  259. /**
  260. * Enable the use of CI super-global
  261. *
  262. * @param mixed $var
  263. * @return mixed
  264. */
  265. public function __get($var)
  266. {
  267. return get_instance()->$var;
  268. }
  269. }
  270. /* End of file Migration.php */
  271. /* Location: ./system/libraries/Migration.php */