actions.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Oil;
  13. /**
  14. * Oil\Generate_Migration_Actions
  15. * Handles actions for generating migrations in Oil
  16. *
  17. * @package Fuel
  18. * @subpackage Oil
  19. * @category Core
  20. * @author Tom Arnfeld
  21. */
  22. class Generate_Migration_Actions
  23. {
  24. /**
  25. * Each migration action should return an array with two items, 0 being the up and 1 the being down.
  26. */
  27. // create_{tablename}
  28. public static function create($subjects, $fields)
  29. {
  30. $field_str = '';
  31. $defined_columns = array();
  32. $have_id = false;
  33. foreach($fields as $field)
  34. {
  35. $name = array_shift($field);
  36. $name === 'id' and $have_id = true;
  37. $field_opts = array();
  38. foreach($field as $option => $val)
  39. {
  40. if($val === true)
  41. {
  42. $field_opts[] = "'$option' => true";
  43. }
  44. else
  45. {
  46. if(is_int($val))
  47. {
  48. $field_opts[] = "'$option' => $val";
  49. }
  50. else
  51. {
  52. $field_opts[] = "'$option' => '$val'";
  53. }
  54. }
  55. }
  56. $field_opts = implode(', ', $field_opts);
  57. $field_str .= "\t\t\t'$name' => array({$field_opts}),".PHP_EOL;
  58. $defined_columns[$name] = true;
  59. }
  60. // ID Field
  61. $have_id or $field_str = "\t\t\t'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),".PHP_EOL . $field_str;
  62. $up = <<<UP
  63. \DBUtil::create_table('{$subjects[1]}', array(
  64. $field_str
  65. ), array('id'));
  66. UP;
  67. $down = <<<DOWN
  68. \DBUtil::drop_table('{$subjects[1]}');
  69. DOWN;
  70. return array($up, $down);
  71. }
  72. // add_{thing}_to_{tablename}
  73. public static function add($subjects, $fields, $reverse = false)
  74. {
  75. $field_up_str = '';
  76. foreach($fields as $field)
  77. {
  78. $name = array_shift($field);
  79. $field_opts = array();
  80. foreach($field as $option => $val)
  81. {
  82. if($val === true)
  83. {
  84. $field_opts[] = "'$option' => true";
  85. }
  86. else
  87. {
  88. if(is_int($val))
  89. {
  90. $field_opts[] = "'$option' => $val";
  91. }
  92. else
  93. {
  94. $field_opts[] = "'$option' => '$val'";
  95. }
  96. }
  97. }
  98. $field_opts = implode(', ', $field_opts);
  99. $field_up_str .= "\t\t\t'$name' => array({$field_opts}),".PHP_EOL;
  100. $field_down[] = "\t\t\t'$name'".PHP_EOL;
  101. }
  102. $field_down_str = implode(',', $field_down);
  103. $up = <<<UP
  104. \DBUtil::add_fields('{$subjects[1]}', array(
  105. $field_up_str
  106. ));
  107. UP;
  108. $down = <<<DOWN
  109. \DBUtil::drop_fields('{$subjects[1]}', array(
  110. $field_down_str
  111. ));
  112. DOWN;
  113. return $reverse ? array($down, $up) : array($up, $down);
  114. }
  115. // delete_{thing}_from_{tablename}
  116. public static function delete($subjects, $fields, $reverse = false)
  117. {
  118. return static::add($subjects, $fields, true);
  119. }
  120. // rename_field_{fieldname}_to_{newfieldname}_in_{table}
  121. public static function rename_field($subjects, $fields)
  122. {
  123. $column_list = \DB::list_columns($subjects[0], $subjects[1]);
  124. $column = $column_list[$subjects[1]];
  125. switch ($column['type'])
  126. {
  127. case 'float':
  128. $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
  129. break;
  130. case 'int':
  131. $constraint = $column['display'];
  132. break;
  133. case 'string':
  134. switch ($column['data_type'])
  135. {
  136. case 'binary':
  137. case 'varbinary':
  138. case 'char':
  139. case 'varchar':
  140. $constraint = $column['character_maximum_length'];
  141. break;
  142. case 'enum':
  143. case 'set':
  144. $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
  145. break;
  146. }
  147. break;
  148. }
  149. $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
  150. $up = <<<UP
  151. \DBUtil::modify_fields('{$subjects[0]}', array(
  152. \t\t\t'{$subjects[1]}' => array('name' => '{$subjects[2]}', 'type' => '{$column['data_type']}'$constraint_str)
  153. ));
  154. UP;
  155. $down = <<<DOWN
  156. \DBUtil::modify_fields('{$subjects[0]}', array(
  157. \t\t\t'{$subjects[2]}' => array('name' => '{$subjects[1]}', 'type' => '{$column['data_type']}'$constraint_str)
  158. ));
  159. DOWN;
  160. return array($up, $down);
  161. }
  162. // rename_table_{tablename}_to_{newtablename}
  163. public static function rename_table($subjects, $fields)
  164. {
  165. $up = <<<UP
  166. \DBUtil::rename_table('{$subjects[0]}', '{$subjects[1]}');
  167. UP;
  168. $down = <<<DOWN
  169. \DBUtil::rename_table('{$subjects[1]}', '{$subjects[0]}');
  170. DOWN;
  171. return array($up, $down);
  172. }
  173. // drop_{tablename}
  174. public static function drop($subjects, $fields)
  175. {
  176. $up = <<<UP
  177. \DBUtil::drop_table('{$subjects[1]}');
  178. UP;
  179. $field_str = '';
  180. $column_list = \DB::list_columns($subjects[1]);
  181. foreach ($column_list as $column)
  182. {
  183. switch ($column['type'])
  184. {
  185. case 'float':
  186. $constraint = '\''.$column['numeric_precision'].', '.$column['numeric_scale'].'\'';
  187. break;
  188. case 'int':
  189. $constraint = $column['display'];
  190. break;
  191. case 'string':
  192. switch ($column['data_type'])
  193. {
  194. case 'binary':
  195. case 'varbinary':
  196. case 'char':
  197. case 'varchar':
  198. $constraint = $column['character_maximum_length'];
  199. break;
  200. case 'enum':
  201. case 'set':
  202. $constraint = '"\''.implode('\',\'',$column['options']).'\'"';
  203. break;
  204. }
  205. break;
  206. }
  207. $constraint_str = isset($constraint) ? ", 'constraint' => $constraint" : '';
  208. $auto_increment = $column['extra'] == 'auto_increment' ? ", 'auto_increment' => true" : '';
  209. $default_str = $column['default'] != null ? ", 'default' => '{$column['default']}'" : ", 'null' => true";
  210. if ($column['key'] == 'PRI')
  211. {
  212. $primary_keys[] = "'{$column['name']}'";
  213. }
  214. else if ($column['key'] == 'MUL')
  215. {
  216. $indexes[] = $column['name'];
  217. }
  218. $field_str .= "\t\t\t'{$column['name']}' => array('type' => '{$column['data_type']}'{$default_str}{$constraint_str}{$auto_increment}),".PHP_EOL;
  219. unset($constraint);
  220. }
  221. $primary_keys = implode(',', $primary_keys);
  222. $down = <<<DOWN
  223. \DBUtil::create_table('{$subjects[1]}', array(
  224. $field_str
  225. ), array($primary_keys));
  226. DOWN;
  227. $down .= PHP_EOL;
  228. $active_db = \Config::get('db.active');
  229. $table_prefix = \Config::get('db.'.$active_db.'.table_prefix');
  230. if (isset($indexes))
  231. {
  232. foreach ($indexes as $field)
  233. {
  234. $down .= "\t\t\\DB::query(\"CREATE INDEX {$field}_idx ON {$table_prefix}{$subjects[1]} (`{$field}`)\")->execute();".PHP_EOL;
  235. }
  236. }
  237. return array($up, $down);
  238. }
  239. }