mirror of
https://github.com/Oreolek/kohana-migrations.git
synced 2024-06-16 15:01:08 +03:00
Changing timestamps to be {year}{month}{day}{hour}{minute}{second}
Also added get_migration method to model and some more tests
This commit is contained in:
parent
d8f033f22a
commit
41a7392f15
|
@ -55,6 +55,31 @@ class Model_Minion_Migration extends Model
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a migration by its id
|
||||
*
|
||||
* @param string Migration ID
|
||||
* @return array Migration info
|
||||
*/
|
||||
public function get_migration($location, $timestamp = NULL)
|
||||
{
|
||||
if($timestamp === NULL)
|
||||
{
|
||||
if(empty($location) OR strpos(':', $location) === FALSE)
|
||||
{
|
||||
throw new Kohana_Exception('Invalid migration id :id', array(':id' => $location));
|
||||
}
|
||||
|
||||
list($location, $timestamp) = explode(':', $location);
|
||||
}
|
||||
|
||||
return $this->_select()
|
||||
->where('timestamp', '=', (string) $timestamp)
|
||||
->where('location', '=', (string) $location)
|
||||
->execute($this->_db)
|
||||
->current();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a migration from the database
|
||||
*
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `migrations` (
|
||||
`timestamp` varchar(12) NOT NULL,
|
||||
`timestamp` varchar(14) NOT NULL,
|
||||
`description` varchar(100) NOT NULL,
|
||||
`location` varchar(100) NOT NULL,
|
||||
`applied` tinyint(1) DEFAULT '0',
|
||||
|
|
|
@ -27,9 +27,7 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
*/
|
||||
public function getModel()
|
||||
{
|
||||
$db = Database::instance(Kohana::config('unittest')->db_connection);
|
||||
|
||||
return new Model_Minion_Migration($db);
|
||||
return new Model_Minion_Migration($this->getKohanaConnection());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,8 +56,8 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
|
||||
$this->assertSame(
|
||||
array (
|
||||
'app' => '1293543858',
|
||||
'dblogger' => '1293544858',
|
||||
'app' => '20101216080000',
|
||||
'dblogger' => '20101225000000',
|
||||
),
|
||||
$versions
|
||||
);
|
||||
|
@ -80,18 +78,18 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
'direction' => true,
|
||||
'migrations' => array(
|
||||
array (
|
||||
'timestamp' => '1293543800',
|
||||
'timestamp' => '20101215165000',
|
||||
'description' => 'add-name-column-to-members',
|
||||
'location' => 'app',
|
||||
'applied' => '0',
|
||||
'id' => 'app:1293543800'
|
||||
'id' => 'app:20101215165000'
|
||||
),
|
||||
array (
|
||||
'timestamp' => '1293543828',
|
||||
'timestamp' => '20101216000000',
|
||||
'description' => 'add-index-on-name',
|
||||
'location' => 'app',
|
||||
'applied' => '0',
|
||||
'id' => 'app:1293543828'
|
||||
'id' => 'app:20101216000000'
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -99,11 +97,11 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
'direction' => true,
|
||||
'migrations' => array(
|
||||
array (
|
||||
'timestamp' => '1293544908',
|
||||
'timestamp' => '20101226112100',
|
||||
'description' => 'add-pk',
|
||||
'location' => 'dblogger',
|
||||
'applied' => '0',
|
||||
'id' => 'dblogger:1293544908'
|
||||
'id' => 'dblogger:20101226112100'
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -118,18 +116,18 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
'direction' => FALSE,
|
||||
'migrations' => array(
|
||||
array(
|
||||
'timestamp' => '1293543858',
|
||||
'timestamp' => '20101216080000',
|
||||
'description' => 'remove-password-salt-column',
|
||||
'location' => 'app',
|
||||
'applied' => '1',
|
||||
'id' => 'app:1293543858'
|
||||
'id' => 'app:20101216080000'
|
||||
),
|
||||
array(
|
||||
'timestamp' => '1293543728',
|
||||
'timestamp' => '20101215164400',
|
||||
'description' => 'create-tables',
|
||||
'location' => 'app',
|
||||
'applied' => '1',
|
||||
'id' => 'app:1293543728'
|
||||
'id' => 'app:20101215164400'
|
||||
),
|
||||
)
|
||||
),
|
||||
|
@ -137,18 +135,18 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
'direction' => FALSE,
|
||||
'migrations' => array(
|
||||
array(
|
||||
'timestamp' => '1293544858',
|
||||
'timestamp' => '20101225000000',
|
||||
'description' => 'remove-unique-index',
|
||||
'location' => 'dblogger',
|
||||
'applied' => '1',
|
||||
'id' => 'dblogger:1293544858'
|
||||
'id' => 'dblogger:20101225000000'
|
||||
),
|
||||
array(
|
||||
'timestamp' => '1293543858',
|
||||
'timestamp' => '20101215164500',
|
||||
'description' => 'create-table',
|
||||
'location' => 'dblogger',
|
||||
'applied' => '1',
|
||||
'id' => 'dblogger:1293543858'
|
||||
'id' => 'dblogger:20101215164500'
|
||||
),
|
||||
)
|
||||
),
|
||||
|
@ -179,4 +177,136 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
|
||||
$this->assertSame($expected, $results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_get_migration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provider_get_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'timestamp' => '20101215164400',
|
||||
'description' => 'create-tables',
|
||||
'location' => 'app',
|
||||
'applied' => '1',
|
||||
'id' => 'app:20101215164400'
|
||||
),
|
||||
'app',
|
||||
'20101215164400',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Model_Minion_Migration::get_migration can get a migration from
|
||||
* the database
|
||||
*
|
||||
* @test
|
||||
* @covers Model_Minion_Migration::get_migration
|
||||
* @dataProvider provider_get_migration
|
||||
* @param array Expected migration
|
||||
* @param string The migration's location
|
||||
* @param string The migration's timestamp
|
||||
*/
|
||||
public function test_get_migration($expected, $location, $timestamp)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getModel()->get_migration($location, $timestamp)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_get_migration_throws_exception_on_invalid_input
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provider_get_migration_throws_exception_on_invalid_input()
|
||||
{
|
||||
return array(
|
||||
array(NULL, NULL),
|
||||
array('app', NULL),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* If invalid input is passed to get_migration then it should throw an
|
||||
* exception
|
||||
*
|
||||
* @test
|
||||
* @covers Model_Minion_Migration::get_migration
|
||||
* @dataProvider provider_get_migration_throws_exception_on_invalid_input
|
||||
* @expectedException Kohana_Exception
|
||||
*/
|
||||
public function test_get_migration_throws_exception_on_invalid_input($location, $timestamp)
|
||||
{
|
||||
$this->getModel()->get_migration($location, $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_mark_migration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provider_mark_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'timestamp' => '20101215165000',
|
||||
'description' => 'add-name-column-to-members',
|
||||
'location' => 'app',
|
||||
'applied' => '1',
|
||||
'id' => 'app:20101215165000',
|
||||
),
|
||||
array(
|
||||
'timestamp' => '20101215165000',
|
||||
'location' => 'app',
|
||||
'description' => 'add-name-column-to-members',
|
||||
),
|
||||
TRUE
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'timestamp' => '20101215165000',
|
||||
'description' => 'add-name-column-to-members',
|
||||
'location' => 'app',
|
||||
'applied' => '0',
|
||||
'id' => 'app:20101215165000',
|
||||
),
|
||||
array(
|
||||
'timestamp' => '20101215165000',
|
||||
'location' => 'app',
|
||||
'description' => 'add-name-column-to-members',
|
||||
),
|
||||
FALSE
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Model_Minion_Migration::mark_migration() changes the applied
|
||||
* status of a migration
|
||||
*
|
||||
* @test
|
||||
* @covers Model_Minion_Migration::mark_migration
|
||||
* @dataProvider provider_mark_migration
|
||||
* @param array What the DB record should look like after migration is marked
|
||||
* @param array The migration to update
|
||||
* @param bool Whether the migration should be applied
|
||||
*/
|
||||
public function test_mark_migration($expected, $migration, $applied)
|
||||
{
|
||||
$model = $this->getModel();
|
||||
|
||||
$model->mark_migration($migration, $applied);
|
||||
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$model->get_migration($migration['location'], $migration['timestamp'])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<dataset>
|
||||
<migrations timestamp="1293543728" description="create-tables" location="app" applied="1" />
|
||||
<migrations timestamp="1293543800" description="add-name-column-to-members" location="app" applied="0" />
|
||||
<migrations timestamp="1293543828" description="add-index-on-name" location="app" applied="0" />
|
||||
<migrations timestamp="1293543858" description="remove-password-salt-column" location="app" applied="1" />
|
||||
<migrations timestamp="20101215164400" description="create-tables" location="app" applied="1" />
|
||||
<migrations timestamp="20101215165000" description="add-name-column-to-members" location="app" applied="0" />
|
||||
<migrations timestamp="20101216000000" description="add-index-on-name" location="app" applied="0" />
|
||||
<migrations timestamp="20101216080000" description="remove-password-salt-column" location="app" applied="1" />
|
||||
|
||||
<migrations timestamp="1293543858" description="create-table" location="dblogger" applied="1" />
|
||||
<migrations timestamp="1293544858" description="remove-unique-index" location="dblogger" applied="1" />
|
||||
<migrations timestamp="1293544908" description="add-pk" location="dblogger" applied="0" />
|
||||
<migrations timestamp="20101215164500" description="create-table" location="dblogger" applied="1" />
|
||||
<migrations timestamp="20101225000000" description="remove-unique-index" location="dblogger" applied="1" />
|
||||
<migrations timestamp="20101226112100" description="add-pk" location="dblogger" applied="0" />
|
||||
</dataset>
|
||||
|
|
Loading…
Reference in a new issue