1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-migrations.git synced 2024-06-26 03:40:54 +03:00

Merge pull request #26 from d4rky-pl/mod/db-migrate

Refactors the migrate:generate task to allow separate tasks to generate migrations with custom up/down code. Thanks Michał!
This commit is contained in:
Matt Button 2011-06-30 13:46:05 -07:00
commit 212b525b6b
2 changed files with 28 additions and 5 deletions

View file

@ -45,6 +45,20 @@ class Minion_Task_Db_Generate extends Minion_Task
* @param array Configuration
*/
public function execute(array $config)
{
try
{
$file = $this->generate($config);
Minion_CLI::write('Migration generated: '.$file);
}
catch(ErrorException $e)
{
Minion_CLI::write($e->getMessage());
}
}
public function generate($config, $up = null, $down = null)
{
$defaults = array(
'location' => APPPATH,
@ -59,9 +73,7 @@ class Minion_Task_Db_Generate extends Minion_Task
if ( ! $this->_valid_group($config['group']))
{
Minion_CLI::write('Please provide a valid --group');
Minion_CLI::write('See help for more info');
return;
throw new ErrorException("Please provide a valid --group\nSee help for more info");
}
$group = $config['group'].'/';
@ -77,6 +89,8 @@ class Minion_Task_Db_Generate extends Minion_Task
$data = Kohana::FILE_SECURITY.View::factory('minion/task/db/generate/template')
->set('class', $class)
->set('description', $description)
->set('up', $up)
->set('down', $down)
->render();
if ( ! is_dir(dirname($file)))
@ -86,8 +100,7 @@ class Minion_Task_Db_Generate extends Minion_Task
file_put_contents($file, $data);
Minion_CLI::write('Migration generated: '.$file);
return;
return $file;
}
/**

View file

@ -12,7 +12,12 @@ class <?php echo $class; ?> extends Minion_Migration_Base {
*/
public function up(Kohana_Database $db)
{
<?php if(!empty($up)): ?>
<?php echo $up; ?>
<?php else: ?>
// $db->query(NULL, 'CREATE TABLE ... ');
<?php endif; ?>
}
/**
@ -22,6 +27,11 @@ class <?php echo $class; ?> extends Minion_Migration_Base {
*/
public function down(Kohana_Database $db)
{
<?php if(!empty($down)): ?>
<?php echo $down; ?>
<?php else: ?>
// $db->query(NULL, 'DROP TABLE ... ');
<?php endif; ?>
}
}