Skip to content

Commit

Permalink
Refactor Migrations to track history on a per-db group basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Apr 25, 2016
1 parent bf1b2eb commit 13eec36
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
20 changes: 20 additions & 0 deletions application/Database/Migrations/20150101101500_First_migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Migration_First_migration extends \CodeIgniter\Database\Migration
{
public function up()
{

}

//--------------------------------------------------------------------

public function down()
{

}

//--------------------------------------------------------------------


}
20 changes: 16 additions & 4 deletions system/Commands/MigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function latest()
}
catch (\Exception $e)
{
CLI::error($e->getMessage());
$this->showError($e);
}

CLI::write('Done');
Expand Down Expand Up @@ -59,7 +59,7 @@ public function version(int $version = null)
}
catch (\Exception $e)
{
CLI::error($e->getMessage());
$this->showError($e);
}

CLI::write('Done');
Expand All @@ -80,7 +80,7 @@ public function current()
}
catch (\Exception $e)
{
CLI::error($e->getMessage());
$this->showError($e);
}

CLI::write('Done');
Expand All @@ -101,7 +101,7 @@ public function rollback()
}
catch (\Exception $e)
{
CLI::error($e->getMessage());
$this->showError($e);
}

CLI::write('Done');
Expand Down Expand Up @@ -175,5 +175,17 @@ public function seed(string $seedName)

//--------------------------------------------------------------------

/**
* Displays a caught exception.
*
* @param \Exception $e
*/
protected function showError(\Exception $e)
{
CLI::error($e->getMessage());
CLI::write($e->getFile().' - '.$e->getLine(), 'white');
}

//--------------------------------------------------------------------

}
3 changes: 3 additions & 0 deletions system/Config/AutoloadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public function __construct()
'CodeIgniter\Database\Query' => BASEPATH.'Database/Query.php',
'CodeIgniter\Database\QueryInterface' => BASEPATH.'Database/QueryInterface.php',
'CodeIgniter\Database\ResultInterface' => BASEPATH.'Database/ResultInterface.php',
'CodeIgniter\Database\Migration' => BASEPATH.'Database/Migration.php',
'CodeIgniter\Database\MigrationRunner' => BASEPATH.'Database/MigrationRunner.php',
'CodeIgniter\Debug\Exceptions' => BASEPATH.'Debug/Exceptions.php',
'CodeIgniter\Debug\Timer' => BASEPATH.'Debug/Timer.php',
'CodeIgniter\Debug\Iterator' => BASEPATH.'Debug/Iterator.php',
Expand Down Expand Up @@ -154,6 +156,7 @@ public function __construct()
'CodeIgniter\View\View' => BASEPATH.'View/View.php',
'Zend\Escaper\Escaper' => BASEPATH.'View/Escaper.php',
'CodeIgniter\Log\TestLogger' => BASEPATH.'../tests/_support/Log/TestLogger.php',
'CIDatabaseTestCase' => BASEPATH.'../tests/_support/CIDatabaseTestCase.php'
];
}

Expand Down
19 changes: 16 additions & 3 deletions system/Database/Migration.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace CodeIgniter\Database;

use Config\Database;
use \Config\Database;

abstract class Migration
{
Expand Down Expand Up @@ -30,11 +30,24 @@ public function __construct(Forge $forge = null)
? $forge
: Database::forge($this->DBGroup);

$this->db =& $this->forge->getConnection();
$this->db = $this->forge->getConnection();
}

//--------------------------------------------------------------------


/**
* Returns the database group name this migration uses.
*
* @return string
*/
public function getDBGroup()
{
return $this->DBGroup;
}

//--------------------------------------------------------------------


abstract public function up();

//--------------------------------------------------------------------
Expand Down
29 changes: 20 additions & 9 deletions system/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public function version($targetVersion)
call_user_func([$instance, $method]);

$currentVersion = $number;
if ($method === 'up') $this->addHistory($currentVersion);
elseif ($method === 'down') $this->removeHistory($currentVersion);
if ($method === 'up') $this->addHistory($currentVersion, $instance->getDBGroup());
elseif ($method === 'down') $this->removeHistory($currentVersion, $instance->getDBGroup());
}
}

Expand Down Expand Up @@ -288,9 +288,10 @@ public function setPath(string $path)
*
* @return mixed
*/
public function getHistory()
public function getHistory($group = 'default')
{
$query = $this->db->table($this->table)
->where('group', $group)
->get();

if (! $query) return [];
Expand Down Expand Up @@ -338,10 +339,11 @@ protected function getMigrationName($migration)
*
* @return string Current migration version
*/
protected function getVersion()
protected function getVersion($group = 'default')
{
$row = $this->db->table($this->table)
->select('version')
->where('group', $group)
->get()
->getRow();

Expand All @@ -353,17 +355,19 @@ protected function getVersion()
/**
* Stores the current schema version.
*
* @param $version
* @param string $version
* @param string $group The database group
*
* @internal param string $migration Migration reached
*
*/
protected function addHistory($version)
protected function addHistory($version, $group = 'default')
{
$this->db->table($this->table)
->insert([
'version' => $version,
'time' => date('Y-m-d H:i:s')
'group' => $group,
'time' => date('Y-m-d H:i:s')
]);
}

Expand All @@ -372,12 +376,14 @@ protected function addHistory($version)
/**
* Removes a single history
*
* @param $version
* @param string $version
* @param string $group The database group
*/
protected function removeHistory($version)
protected function removeHistory($version, $group = 'default')
{
$this->db->table($this->table)
->where('version', $version)
->where('group', $group)
->delete();
}

Expand All @@ -402,6 +408,11 @@ protected function ensureTable()
'constraint' => 20,
'null' => false
],
'group' => [
'type' => 'varchar',
'constraint' => 255,
'null' => false
],
'time' => [
'type' => 'datetime',
'null' => false
Expand Down

0 comments on commit 13eec36

Please sign in to comment.