PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tasks/migrate.php

https://github.com/studiofrenetic/core
PHP | 377 lines | 232 code | 45 blank | 100 comment | 20 complexity | 914ec7d79b484782e7c7ccc079c095c7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2012 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Tasks;
  13. /**
  14. * migrate task
  15. *
  16. * use this command line task to deploy and rollback changes
  17. */
  18. class Migrate
  19. {
  20. /**
  21. * @var boolean if true, migrate the app
  22. */
  23. protected static $default = true;
  24. /**
  25. * @var array list of modules to migrate
  26. */
  27. protected static $modules = array();
  28. /**
  29. * @var array list of packages to migrate
  30. */
  31. protected static $packages = array();
  32. /**
  33. * @var int number of modules migrated
  34. */
  35. protected static $module_count = 0;
  36. /**
  37. * @var int number of packages migrated
  38. */
  39. protected static $package_count = 0;
  40. /**
  41. * sets the properties by grabbing Cli options
  42. */
  43. public function __construct()
  44. {
  45. // load config
  46. \Config::load('migrations', true);
  47. // get Cli options
  48. $modules = \Cli::option('modules', \Cli::option('m'));
  49. $packages = \Cli::option('packages', \Cli::option('p'));
  50. $default = \Cli::option('default');
  51. $all = \Cli::option('all');
  52. if ($all)
  53. {
  54. $modules = true;
  55. $packages = true;
  56. $default = true;
  57. }
  58. // if modules option set
  59. if ( ! empty($modules))
  60. {
  61. // if true - get all modules
  62. if ($modules === true)
  63. {
  64. // loop through module paths
  65. foreach (\Config::get('module_paths') as $path)
  66. {
  67. // get all modules that have files in the migration folder
  68. foreach (glob($path . '*/') as $m)
  69. {
  70. if (count(glob($m.\Config::get('migrations.folder').'/*.php')))
  71. {
  72. static::$modules[] = basename($m);
  73. }
  74. }
  75. }
  76. }
  77. // else do selected modules
  78. else
  79. {
  80. static::$modules = explode(',', $modules);
  81. }
  82. }
  83. // if packages option set
  84. if ( ! empty($packages))
  85. {
  86. // if true - get all packages
  87. if ($packages === true)
  88. {
  89. // get all packages that have files in the migration folder
  90. foreach (\Config::get('package_paths', array(PKGPATH)) as $p)
  91. {
  92. foreach (glob($p . '*/') as $pp)
  93. {
  94. if (count(glob($pp.\Config::get('migrations.folder').'/*.php')))
  95. {
  96. static::$packages[] = basename($pp);
  97. }
  98. }
  99. }
  100. }
  101. // else do selected packages
  102. else
  103. {
  104. static::$packages = explode(',', $packages);
  105. }
  106. }
  107. // if packages or modules are specified, and the app isn't, disable app migrations
  108. if ( ( ! empty($packages) or ! empty($modules)) and empty($default))
  109. {
  110. static::$default = false;
  111. }
  112. // set the module and package count
  113. static::$module_count = count(static::$modules);
  114. static::$package_count = count(static::$packages);
  115. }
  116. /**
  117. * catches requested method call and runs as needed
  118. *
  119. * @param string name of the method to run
  120. * @param string any additional method arguments (not used here!)
  121. */
  122. public function __call($name, $args)
  123. {
  124. // set method name
  125. $name = '_'.$name;
  126. // make sure the called name exists
  127. if ( ! method_exists(get_called_class(), $name))
  128. {
  129. return static::help();
  130. }
  131. // run app (default) migrations if default is true
  132. if (static::$default)
  133. {
  134. static::$name('default', 'app');
  135. }
  136. // run migrations on all specified modules
  137. foreach (static::$modules as $module)
  138. {
  139. static::$name($module, 'module');
  140. }
  141. // run migrations on all specified packages
  142. foreach (static::$packages as $package)
  143. {
  144. static::$name($package, 'package');
  145. }
  146. }
  147. /**
  148. * migrates to the latest version unless -version is specified
  149. *
  150. * @param string name of the type (in case of app, it's 'default')
  151. * @param string type (app, module or package)
  152. * @param string direction of migration (up or down)
  153. */
  154. protected static function _run($name, $type)
  155. {
  156. // -v or --version
  157. $version = \Cli::option('v', \Cli::option('version', ''));
  158. // version is used as a flag, so show it
  159. if ($version === true)
  160. {
  161. \Cli::write('Currently installed migrations for '.$type.':'.$name.':', 'green');
  162. foreach (\Config::get('migrations.version.'.$type.'.'.$name, array()) as $version)
  163. {
  164. \Cli::write('- '.$version);
  165. }
  166. return;
  167. }
  168. // version contains a timestamp of sorts
  169. elseif ($version !== '')
  170. {
  171. // if version has a value, make sure only 1 item was passed
  172. if (static::$default + static::$module_count + static::$package_count > 1)
  173. {
  174. \Cli::write('Migration: version only excepts 1 item.');
  175. return;
  176. }
  177. $migrations = \Migrate::version($version, $name, $type);
  178. }
  179. // migrate to the latest version
  180. else
  181. {
  182. $migrations = \Migrate::latest($name, $type);
  183. }
  184. // any migrations executed?
  185. if ($migrations)
  186. {
  187. \Cli::write('Performed migrations for '.$type.':'.$name.':', 'green');
  188. foreach ($migrations as $migration)
  189. {
  190. \Cli::write($migration);
  191. }
  192. }
  193. else
  194. {
  195. if ($version !== '')
  196. {
  197. \Cli::write('No migrations were found for '.$type.':'.$name.'.');
  198. }
  199. else
  200. {
  201. \Cli::write('Already on the latest migration for '.$type.':'.$name.'.');
  202. }
  203. }
  204. }
  205. /**
  206. * migrates item to current config version
  207. *
  208. * @param string name of the type (in case of app, it's 'default')
  209. * @param string type (app, module or package)
  210. */
  211. protected static function _current($name, $type)
  212. {
  213. // -v or --version
  214. if (\Cli::option('v', \Cli::option('version', '')) !== '')
  215. {
  216. \Cli::write('You can not define a version when using the "current" command.', 'red');
  217. }
  218. $migrations = \Migrate::current($name, $type);
  219. if ($migrations)
  220. {
  221. \Cli::write('Newly installed migrations for '.$type.':'.$name.':', 'green');
  222. foreach ($migrations as $migration)
  223. {
  224. \Cli::write('- '.$migration);
  225. }
  226. }
  227. else
  228. {
  229. // migration is already on current version
  230. \Cli::write('Already on the current migration version for '.$type.':'.$name.'.');
  231. }
  232. }
  233. /**
  234. * migrates item up to the given version
  235. *
  236. * @param string
  237. * @param string
  238. */
  239. protected static function _up($name, $type)
  240. {
  241. // -v or --version
  242. $version = \Cli::option('v', \Cli::option('version', null));
  243. // if version has a value, make sure only 1 item was passed
  244. if ($version and (static::$default + static::$module_count + static::$package_count > 1))
  245. {
  246. \Cli::write('Migration: version only excepts 1 item.');
  247. return;
  248. }
  249. $migrations = \Migrate::up($version, $name, $type);
  250. if ($migrations)
  251. {
  252. \Cli::write('Newly installed migrations for '.$type.':'.$name.':', 'green');
  253. foreach ($migrations as $migration)
  254. {
  255. \Cli::write('- '.$migration);
  256. }
  257. }
  258. else
  259. {
  260. // there is no 'up'...
  261. \Cli::write('You are already on the latest migration version for '.$type.':'.$name.'.');
  262. }
  263. }
  264. /**
  265. * migrates item down to the given version
  266. *
  267. * @param string
  268. * @param string
  269. */
  270. protected static function _down($name, $type)
  271. {
  272. // -v or --version
  273. $version = \Cli::option('v', \Cli::option('version', null));
  274. // if version has a value, make sure only 1 item was passed
  275. if ($version and (static::$default + static::$module_count + static::$package_count > 1))
  276. {
  277. \Cli::write('Migration: version only excepts 1 item.');
  278. return;
  279. }
  280. $migrations = \Migrate::down($version, $name, $type);
  281. if ($migrations)
  282. {
  283. \Cli::write('Reverted migrations for '.$type.':'.$name.':', 'green');
  284. foreach ($migrations as $migration)
  285. {
  286. \Cli::write('- '.$migration);
  287. }
  288. }
  289. else
  290. {
  291. // there is no 'down'...
  292. \Cli::write('There are no migrations installed to revert for '.$type.':'.$name.'.');
  293. }
  294. }
  295. /**
  296. * Shows basic help instructions for using migrate in oil
  297. */
  298. public static function help()
  299. {
  300. echo <<<HELP
  301. Usage:
  302. php oil refine migrate[:command] [--version=X]
  303. Fuel commands:
  304. help shows this text
  305. current migrates to the version defined in the migration configuration file
  306. up migrate up to the next version
  307. down migrate down to the previous version
  308. run run all migrations (default)
  309. Fuel options:
  310. -v, [--version] # Migrate to a specific version ( only 1 item at a time)
  311. # The following disable default migrations unless you add --default to the command
  312. --default # re-enables default migration
  313. --modules -m # Migrates all modules
  314. --modules=item1,item2 -m=item1,item2 # Migrates specific modules
  315. --packages -p # Migrates all packages
  316. --packages=item1,item2 -p=item1,item2 # Migrates specific modules
  317. --all # shortcut for --modules --packages --default
  318. Description:
  319. The migrate task can run migrations. You can go up, down or by default go to the current migration marked in the config file.
  320. Examples:
  321. php oil r migrate
  322. php oil r migrate:current
  323. php oil r migrate:up -v=6
  324. php oil r migrate:down
  325. php oil r migrate --version=201203171206
  326. php oil r migrate --modules --packages --default
  327. php oil r migrate:up --modules=module1,module2 --packages=package1
  328. php oil r migrate --module=module1 -v=3
  329. php oil r migrate --all
  330. HELP;
  331. }
  332. }