Skip to content

Commit

Permalink
Add up and down option for migrate and seed commands.
Browse files Browse the repository at this point in the history
Add setup function to migration and seed file to prepare the data array.
Rollback the migration or the seed using down option.
Remove rollback command.
Remove version_by_name function.
  • Loading branch information
daif committed Apr 25, 2017
1 parent 9eb14b4 commit e32f237
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 224 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Change Log

## 1.3.0 - 2017-04-25
### Added
- Add `up` and `down` option for migrate and seed commands.
- Add `setup` function to migration and seed file to prepare the data array.

### Changed
- Rollback the migration or the seed using `down` option.

### Removed
- Remove rollback command.
- Remove version_by_name function.

## 1.2.0 - 2017-03-23
### Added
- Implement ErtikazOS store.
Expand Down Expand Up @@ -36,6 +48,6 @@ make_label function can used to print the input label.
- Remove unused $models variable from ER_Controller.
- Remove unneeded files.

## [1.0.0] - 2016-10-02
## 1.0.0 - 2016-10-02
### Added
- Initial Commit.
2 changes: 2 additions & 0 deletions application/controllers/Creator/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function index()
}
}
$this->_print("", "", "\n");
$this->_print("ErtikazOS: ", "", "");
$this->_print($this::ER_VERSION, "warning", "\n\n");
$this->_print("Usage:", "warning", "\n");

foreach ($this->commands as $key => $command) {
Expand Down
112 changes: 67 additions & 45 deletions application/controllers/Creator/commands/Migrate_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ function __construct()
$this->config->load('migration');
// auto-load Migration and Seeder
include_once(APPPATH . 'core/ER_Migration.php');
include_once(APPPATH . 'core/ER_Seeder.php');
// auto create file_path to track files
if(!$this->db->field_exists('file_path', config_item('migration_table')))
{
$fields = array('file_path' => array('type' => 'varchar', 'constraint' => 64));
$this->dbforge->add_column(config_item('migration_table'), $fields);
}
}

/**
Expand All @@ -37,11 +42,15 @@ public static function commands()
{
return [
'name' => 'migrate',
'desc' => 'Migrate the database to last migration version.',
'desc' => 'Migrate all file to database or passed $version or $name.',
'vars' => [
[
'name' => '$version',
'desc' => 'Migrate this $version number.',
'name' => '$version or $name',
'desc' => 'Migrating file by its [success]$version[/success] or [success]$name[/success].',
],
[
'name' => '$direction',
'desc' => 'Migrating direction [success]up[/success] or [success]down[/success].',
],
],
];
Expand All @@ -52,62 +61,75 @@ public static function commands()
* migrate command.
*
*/
public function migrate($target_version = FALSE)
public function migrate($target_version = FALSE, $direction = 'up')
{
$target_version = $this->version_by_name($target_version);
$current_version = $this->db->select('version')->get(config_item('migration_table'))->row();
if($target_version !== FALSE)
$this->_print('', '');
if($target_version === FALSE)
{
$migration_path = '*_*.php';
}
elseif(preg_match('/[0-9]{14}/', $target_version))
{
$migration_path = ''.$target_version.'_*.php';
} else
{
$migration_path = '*_'.strtolower($target_version).'.php';
}

// find migration files
foreach (glob(config_item('migration_path').$migration_path) as $key => $migration_file)
{
if($target_version > $current_version->version)
list($timestamp, $filename) = explode('_', basename($migration_file));
// search for the migration file in database
$migration_row = $this->db->select('file_path')->get_where(config_item('migration_table'), array('file_path' => basename($migration_file)))->row();

require_once(config_item('migration_path').''.basename($migration_file));
$migration = 'Migration_'.ucfirst(explode('.', $filename)[0]);

if(!class_exists($migration))
{
if ($this->migration->version($target_version) === FALSE)
$this->_print("migration class '$migration' is not found", 'error');
continue;
}
// new migration instance
$migration = new $migration;
if(strtolower($direction) == 'down')
{
if($migration_row)
{
show_error($this->migration->error_string());
$migration->down();

$this->db->delete(config_item('migration_table'), array('file_path' => basename($migration_file)));
$this->_print(basename($migration_file).' Un-Migrated.', 'success');
}
else
{
$this->_print("Migrate has been executed", 'success');
// migration file is not migrated yet
$this->_print(basename($migration_file).' is not migrated yet.', 'success');
continue;
}
}
else
{
$this->_print("Nothing to migrate", 'error');
return;
}
}
else
{
if ($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
else
{
$this->_print("Migrate has been executed", 'success');
}
}
}

/**
*
* Get target version by name.
*
*/
public function version_by_name($target_version)
{
if(!preg_match('/[0-9]{14}/', $target_version) && $target_version !== FALSE)
{
$version = glob(config_item('migration_path').'*_'.$target_version.'.php');
if(isset($version[0]))
{
$version = explode('_', basename($version[0]));
if(isset($version[0]))
if($migration_row)
{
return $version[0];
// migrated file is already migrated
$this->_print(basename($migration_file).' is already migrated.', 'warning');
continue;
}
else
{
$migration->up();
$this->db->insert(config_item('migration_table'), array('file_path' => basename($migration_file)));
$this->_print(basename($migration_file).' Migrated.', 'success');
}
}
}
return $target_version;
if(!isset($migration_file))
{
$this->_print("Migrating target '$target_version' is not found.", 'error');
}
$this->_print('', '');
}
}

Expand Down
92 changes: 0 additions & 92 deletions application/controllers/Creator/commands/Rollback_Command.php

This file was deleted.

Loading

0 comments on commit e32f237

Please sign in to comment.