From 4a5cc8bcb8059601c73e8c9beaf1bde9942b35a5 Mon Sep 17 00:00:00 2001 From: Matt Button Date: Fri, 31 Dec 2010 01:57:01 +0000 Subject: [PATCH] Adding task to generate migration files --- classes/minion/task/db/generate.php | 105 +++++++++++++++++++++ views/minion/task/db/generate/template.php | 27 ++++++ 2 files changed, 132 insertions(+) create mode 100644 classes/minion/task/db/generate.php create mode 100644 views/minion/task/db/generate/template.php diff --git a/classes/minion/task/db/generate.php b/classes/minion/task/db/generate.php new file mode 100644 index 0000000..f35b60f --- /dev/null +++ b/classes/minion/task/db/generate.php @@ -0,0 +1,105 @@ + + */ +class Minion_Task_Db_Generate extends Minion_Task +{ + /** + * A set of config options that this task accepts + * @var array + */ + protected $_config = array( + 'location', + 'description' + ); + + /** + * Execute the task + * + * @param array Configuration + */ + public function execute(array $config) + { + if(empty($config['location']) OR empty($config['description'])) + { + return 'Please provide --location and --description'.PHP_EOL. + 'See help for more info'.PHP_EOL; + } + + $location = rtrim(realpath($config['location']), '/').'/'; + $description = $config['description']; + + // {year}{month}{day}{hour}{minute}{second} + $time = date('YmdGis'); + $class = $this->_generate_classname($location, $time); + $file = $this->_generate_filename($location, $time, $description); + + + $data = Kohana::FILE_SECURITY.View::factory('minion/task/db/generate/template') + ->set('class', $class) + ->set('description', $description) + ->render(); + + file_put_contents($file, $data); + + return 'Migration generated in '.$file.PHP_EOL; + } + + /** + * Generate a class name from the location + * + * @param string location + * @param string Timestamp + * @return string Class name + */ + protected function _generate_classname($location, $time) + { + // Chop up everything up until the relative path + $location = substr($location, strrpos($location, 'migrations/') + 11); + + $class = ucwords(str_replace('/', ' ', $location)); + + return 'Migration_'.preg_replace('~[^a-zA-Z0-9]+~', '_', $class.'_'.$time); + } + + /** + * Generates a filename from the location, time and description + * + * @param string Location to store migration + * @param string Timestamp + * @param string Description + * @return string Filename + */ + public function _generate_filename($location, $time, $description) + { + $description = substr(strtolower($description), 0, 100); + + return $location.$time.'_'.preg_replace('~[^a-z]+~', '-', $description).EXT; + } + +} diff --git a/views/minion/task/db/generate/template.php b/views/minion/task/db/generate/template.php new file mode 100644 index 0000000..a97696a --- /dev/null +++ b/views/minion/task/db/generate/template.php @@ -0,0 +1,27 @@ + + +/** + * + */ +class extends Minion_Task { + + /** + * Run queries needed to apply this migration + * + * @param Kohana_Database Database connection + */ + public function up(Kohana_Database $db) + { + // $db->query(NULL, 'CREATE TABLE ... '); + } + + /** + * Run queries need to remove this migration + * + * @param Kohana_Database Database connection + */ + public function down(Kohana_Database $db) + { + // $db->query(NULL, 'DROP TABLE ... '); + } +}