PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/migrations/classes/migration.php

https://bitbucket.org/sklyarov_ivan/trap
PHP | 284 lines | 143 code | 26 blank | 115 comment | 9 complexity | a22af2818e1ae98a617f067e2d890958 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Migration.
  4. *
  5. * @package Despark/timestamped-migrations
  6. * @author Ivan Kerin
  7. * @copyright (c) 2011-2012 Despark Ltd.
  8. * @license http://creativecommons.org/licenses/by-sa/3.0/legalcode
  9. */
  10. abstract class Migration
  11. {
  12. private $driver = null;
  13. private $config = null;
  14. private $dry_run = false;
  15. abstract public function up();
  16. abstract public function down();
  17. public function __construct($config = null)
  18. {
  19. $this->config = arr::merge(Kohana::$config->load('migrations')->as_array(), (array) $config);
  20. $this->driver(Migration_Driver::factory(Arr::get($this->config, 'database', 'default')));
  21. }
  22. /**
  23. * Get or set the current driver
  24. */
  25. public function driver(Migration_Driver $driver = NULL)
  26. {
  27. if ($driver !== NULL)
  28. {
  29. $this->driver = $driver;
  30. return $this;
  31. }
  32. return $this->driver;
  33. }
  34. public function log($message)
  35. {
  36. if ($this->config['log'])
  37. {
  38. call_user_func($this->config['log'], $message);
  39. }
  40. else
  41. {
  42. echo $message."\n";
  43. ob_flush();
  44. }
  45. }
  46. public function dry_run($dry_run = NULL)
  47. {
  48. if ($dry_run !== NULL)
  49. {
  50. $this->dry_run = $dry_run;
  51. return $this;
  52. }
  53. return $this->dry_run;
  54. }
  55. protected function run_driver($title, $method, $args, $will_return = FALSE)
  56. {
  57. if ($title)
  58. {
  59. $this->log("-- ".($this->dry_run ? "[dry-run]" : '')."$title");
  60. }
  61. $start = microtime(TRUE);
  62. $return = NULL;
  63. if ( ! $this->dry_run)
  64. {
  65. $return = call_user_func_array(array($this->driver, $method), $args);
  66. }
  67. $end = microtime(TRUE);
  68. if ($title)
  69. {
  70. $this->log(' --> '.number_format($end-$start, 4).'s'.($method == 'execute' ? ', affected rows: '.$this->driver->affected_rows() : ''));
  71. }
  72. return $will_return ? $return : $this;
  73. }
  74. public function execute($sql, $params = NULL, $display = NULL)
  75. {
  76. $args = func_get_args();
  77. if ($display === NULL)
  78. {
  79. $display = str_replace("\n", '↵', $sql);
  80. $display = preg_replace("/[\s\t]+/", " ", $display);
  81. $display = "execute( ".Text::limit_chars($display, 80)." )";
  82. }
  83. return $this->run_driver($display, __FUNCTION__, $args);
  84. }
  85. public function query($sql, $params = NULL)
  86. {
  87. $args = func_get_args();
  88. $display = str_replace("\n", '↵', $sql);
  89. $display = preg_replace("/[\s\t]+/", " ", $display);
  90. $display = Text::limit_chars($display, 60);
  91. return $this->run_driver("query( $display )", __FUNCTION__, $args, TRUE);
  92. }
  93. public function quote($value)
  94. {
  95. if (is_array($value))
  96. {
  97. return '('.join(', ', array_map(array($this->driver(), 'quote'), $value)).')';
  98. }
  99. else
  100. {
  101. return $this->driver()->quote($value);
  102. }
  103. }
  104. /**
  105. * Create Table
  106. *
  107. * Creates a new table
  108. *
  109. * $fields:
  110. *
  111. * Associative array containing the name of the field as a key and the
  112. * value could be either a string indicating the type of the field, or an
  113. * array containing the field type at the first position and any optional
  114. * arguments the field might require in the remaining positions.
  115. * Refer to the TYPES function for valid type arguments.
  116. * Refer to the FIELD_ARGUMENTS function for valid optional arguments for a
  117. * field.
  118. *
  119. * @code
  120. *
  121. * create_table (
  122. * 'blog',
  123. * array (
  124. * 'title' => array ( 'string[50]', default => "The blog's title." ),
  125. * 'date' => 'date',
  126. * 'content' => 'text'
  127. * ),
  128. * )
  129. * @endcode
  130. * @param string Name of the table to be created
  131. * @param array
  132. * @param array array of options - 'primary_key', false if not desired, not specified sets to 'id' column. Will be set to auto_increment, serial, etc. , 'if_not_exists' - bool, and all the others will be added as options to the end of the create table clause.
  133. * @param bool if_not_exists
  134. * @return boolean
  135. */
  136. public function create_table($table_name, $fields, $options = NULL)
  137. {
  138. $args = func_get_args();
  139. return $this->run_driver("create_table( $table_name, array(".join(", ", array_keys($fields)).") )", __FUNCTION__, $args);
  140. }
  141. /**
  142. * Drop a table
  143. *
  144. * @param string Name of the table
  145. * @return boolean
  146. */
  147. public function drop_table($table_name)
  148. {
  149. $args = func_get_args();
  150. return $this->run_driver("drop_table( $table_name )", __FUNCTION__, $args);
  151. }
  152. /**
  153. * Change table options (passed directly to alter table)
  154. *
  155. * @param string $table_name
  156. * @param array $options an array of options
  157. * @return boolean
  158. */
  159. public function change_table($table_name, $options)
  160. {
  161. $args = func_get_args();
  162. return $this->run_driver("change_table( $table_name , array(".join(", ", array_keys((array) $options))."))", __FUNCTION__, $args);
  163. }
  164. /**
  165. * Rename a table
  166. *
  167. * @param string Old table name
  168. * @param string New name
  169. * @return boolean
  170. */
  171. public function rename_table($old_name, $new_name)
  172. {
  173. $args = func_get_args();
  174. return $this->run_driver("rename_table( $old_name, $new_name )", __FUNCTION__, $args);
  175. }
  176. /**
  177. * Add a column to a table
  178. *
  179. * @code
  180. * add_column ( "the_table", "the_field", array('string', 'limit[25]', 'not_null') );
  181. * add_coumnn ( "the_table", "int_field", "integer" );
  182. * @endcode
  183. *
  184. * @param string Name of the table
  185. * @param string Name of the column
  186. * @param array Column arguments array
  187. * @return bool
  188. */
  189. public function add_column($table_name, $column_name, $params)
  190. {
  191. $args = func_get_args();
  192. return $this->run_driver("add_column( $table_name, $column_name )", __FUNCTION__, $args);
  193. }
  194. /**
  195. * Rename a column
  196. *
  197. * @param string Name of the table
  198. * @param string Name of the column
  199. * @param string New name
  200. * @return bool
  201. */
  202. public function rename_column($table_name, $column_name, $new_column_name)
  203. {
  204. $args = func_get_args();
  205. return $this->run_driver("rename_column( $table_name, $column_name, $new_column_name )", __FUNCTION__, $args);
  206. }
  207. /**
  208. * Alter a column
  209. *
  210. * @param string Table name
  211. * @param string Columnn ame
  212. * @param array Column arguments
  213. * @return bool
  214. */
  215. public function change_column($table_name, $column_name, $params)
  216. {
  217. $args = func_get_args();
  218. return $this->run_driver("change_column( $table_name, $column_name )", __FUNCTION__, $args);
  219. }
  220. /**
  221. * Remove a column from a table
  222. *
  223. * @param string Name of the table
  224. * @param string Name of the column
  225. * @return bool
  226. */
  227. public function remove_column($table_name, $column_name)
  228. {
  229. $args = func_get_args();
  230. return $this->run_driver("remove_column( $table_name, $column_name )", __FUNCTION__, $args);
  231. }
  232. /**
  233. * Add an index
  234. *
  235. * @param string Name of the table
  236. * @param string Name of the index
  237. * @param string|array Name(s) of the column(s)
  238. * @param string Type of the index (unique/normal/primary)
  239. * @return bool
  240. */
  241. public function add_index($table_name, $index_name, $columns, $index_type = 'normal')
  242. {
  243. $args = func_get_args();
  244. return $this->run_driver("add_index( $table_name, $index_name, array(".join(', ',(array) $columns)."), $index_type )", __FUNCTION__, $args);
  245. }
  246. /**
  247. * Remove an index
  248. *
  249. * @param string Name of the table
  250. * @param string Name of the index
  251. * @return bool
  252. */
  253. public function remove_index($table_name, $index_name)
  254. {
  255. $args = func_get_args();
  256. return $this->run_driver("remove_index( $table_name, $index_name )", __FUNCTION__, $args);
  257. }
  258. }