mirror of
https://github.com/Oreolek/kohana-migrations.git
synced 2024-06-26 03:40:54 +03:00
Merge branch '3.1/develop' into 3.2/develop
This commit is contained in:
commit
5f1fecf77d
|
@ -141,7 +141,7 @@ class Minion_Migration_Manager {
|
|||
return;
|
||||
}
|
||||
|
||||
$filename = Minion_Migration_Util::get_filename_from_migration($migration);
|
||||
$filename = $this->_model->get_filename_from_migration($migration);
|
||||
|
||||
if ( ! ($file = Kohana::find_file('migrations', $filename, FALSE)))
|
||||
{
|
||||
|
@ -154,7 +154,7 @@ class Minion_Migration_Manager {
|
|||
);
|
||||
}
|
||||
|
||||
$class = Minion_Migration_Util::get_class_from_migration($migration);
|
||||
$class = $this->_model->get_class_from_migration($migration);
|
||||
|
||||
include_once $file;
|
||||
|
||||
|
|
|
@ -1,124 +0,0 @@
|
|||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
|
||||
/**
|
||||
* Provides a set of utility functions for managing migrations
|
||||
*
|
||||
* @author Matt Button <matthew@sigswitch.com>
|
||||
*/
|
||||
class Minion_Migration_Util {
|
||||
|
||||
/**
|
||||
* Parses a set of files generated by Kohana::find_files and compiles it
|
||||
* down into an array of migrations
|
||||
*
|
||||
* @param array Available files
|
||||
* @return array Available Migrations
|
||||
*/
|
||||
public static function compile_migrations_from_files(array $files)
|
||||
{
|
||||
$migrations = array();
|
||||
|
||||
foreach ($files as $file => $path)
|
||||
{
|
||||
// If this is a directory we're dealing with
|
||||
if (is_array($path))
|
||||
{
|
||||
$migrations += Minion_Migration_Util::compile_migrations_from_files($path);
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration = Minion_Migration_Util::get_migration_from_filename($file);
|
||||
|
||||
$migrations[$migration['group'].':'.$migration['timestamp']] = $migration;
|
||||
}
|
||||
}
|
||||
|
||||
return $migrations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts information about a migration from its filename.
|
||||
*
|
||||
* Returns an array like:
|
||||
*
|
||||
* array(
|
||||
* 'group' => 'mygroup',
|
||||
* 'timestamp' => '1293214439',
|
||||
* 'description' => 'initial-setup',
|
||||
* 'id' => 'mygroup:1293214439'
|
||||
* );
|
||||
*
|
||||
* @param string The migration's filename
|
||||
* @return array Array of components about the migration
|
||||
*/
|
||||
public static function get_migration_from_filename($file)
|
||||
{
|
||||
$migration = array();
|
||||
|
||||
// Get rid of the file's "migrations/" prefix, the file extension and then
|
||||
// the filename itself. The "group" is essentially a slash delimited
|
||||
// path from the migrations folder to the migration file
|
||||
$migration['group'] = dirname(substr($file, 11, -strlen(EXT)));
|
||||
|
||||
if(strpos(basename($file), "_"))
|
||||
{
|
||||
list($migration['timestamp'], $migration['description'])
|
||||
= explode('_', basename($file, EXT), 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration['timestamp'] = basename($file, EXT);
|
||||
$migration['description'] = "";
|
||||
}
|
||||
$migration['id'] = $migration['group'].':'.$migration['timestamp'];
|
||||
|
||||
return $migration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a migration file from its timestamp, description and group
|
||||
*
|
||||
* @param integer|array The migration's ID or an array of timestamp, description
|
||||
* @param string The migration group
|
||||
* @return string Path to the migration file
|
||||
*/
|
||||
public static function get_filename_from_migration(array $migration)
|
||||
{
|
||||
$group = $migration['group'];
|
||||
|
||||
if(!empty($migration['description']))
|
||||
{
|
||||
$migration = $migration['timestamp'].'_'.$migration['description'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration = $migration['timestamp'];
|
||||
}
|
||||
|
||||
$group = ( ! empty($group)) ? (rtrim($group, '/').'/') : '';
|
||||
|
||||
return $group.$migration.EXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to work out the class name from either an array of migration
|
||||
* info, or from a migration id
|
||||
*
|
||||
* @param string|array The migration's ID or array of migration data
|
||||
* @return string The migration class name
|
||||
*/
|
||||
public static function get_class_from_migration($migration)
|
||||
{
|
||||
if (is_string($migration))
|
||||
{
|
||||
$migration = str_replace(array(':', '/'), ' ', $migration);
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration = str_replace('/', ' ', $migration['group']).'_'.$migration['timestamp'];
|
||||
}
|
||||
|
||||
return 'Migration_'.str_replace(array(' ', '-'), '_', ucwords($migration));
|
||||
}
|
||||
}
|
|
@ -37,7 +37,125 @@ class Model_Minion_Migration extends Model
|
|||
{
|
||||
$files = Kohana::list_files('migrations');
|
||||
|
||||
return Minion_Migration_Util::compile_migrations_from_files($files);
|
||||
return $this->compile_migrations_from_files($files);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a set of files generated by Kohana::find_files and compiles it
|
||||
* down into an array of migrations
|
||||
*
|
||||
* @param array Available files
|
||||
* @return array Available Migrations
|
||||
*/
|
||||
public function compile_migrations_from_files(array $files)
|
||||
{
|
||||
$migrations = array();
|
||||
|
||||
foreach ($files as $file => $path)
|
||||
{
|
||||
// If this is a directory we're dealing with
|
||||
if (is_array($path))
|
||||
{
|
||||
$migrations += $this->compile_migrations_from_files($path);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Skip files without an extension of EXT
|
||||
if ('.'.pathinfo($file, PATHINFO_EXTENSION) !== EXT)
|
||||
continue;
|
||||
|
||||
$migration = $this->get_migration_from_filename($file);
|
||||
|
||||
$migrations[$migration['group'].':'.$migration['timestamp']] = $migration;
|
||||
}
|
||||
}
|
||||
|
||||
return $migrations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts information about a migration from its filename.
|
||||
*
|
||||
* Returns an array like:
|
||||
*
|
||||
* array(
|
||||
* 'group' => 'mygroup',
|
||||
* 'timestamp' => '1293214439',
|
||||
* 'description' => 'initial-setup',
|
||||
* 'id' => 'mygroup:1293214439'
|
||||
* );
|
||||
*
|
||||
* @param string The migration's filename
|
||||
* @return array Array of components about the migration
|
||||
*/
|
||||
public function get_migration_from_filename($file)
|
||||
{
|
||||
$migration = array();
|
||||
|
||||
// Get rid of the file's "migrations/" prefix, the file extension and then
|
||||
// the filename itself. The "group" is essentially a slash delimited
|
||||
// path from the migrations folder to the migration file
|
||||
$migration['group'] = dirname(substr($file, 11, -strlen(EXT)));
|
||||
|
||||
if(strpos(basename($file), "_"))
|
||||
{
|
||||
list($migration['timestamp'], $migration['description'])
|
||||
= explode('_', basename($file, EXT), 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration['timestamp'] = basename($file, EXT);
|
||||
$migration['description'] = "";
|
||||
}
|
||||
$migration['id'] = $migration['group'].':'.$migration['timestamp'];
|
||||
|
||||
return $migration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a migration file from its timestamp, description and group
|
||||
*
|
||||
* @param integer|array The migration's ID or an array of timestamp, description
|
||||
* @param string The migration group
|
||||
* @return string Path to the migration file
|
||||
*/
|
||||
public function get_filename_from_migration(array $migration)
|
||||
{
|
||||
$group = $migration['group'];
|
||||
|
||||
if(!empty($migration['description']))
|
||||
{
|
||||
$migration = $migration['timestamp'].'_'.$migration['description'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration = $migration['timestamp'];
|
||||
}
|
||||
|
||||
$group = ( ! empty($group)) ? (rtrim($group, '/').'/') : '';
|
||||
|
||||
return $group.$migration.EXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to work out the class name from either an array of migration
|
||||
* info, or from a migration id
|
||||
*
|
||||
* @param string|array The migration's ID or array of migration data
|
||||
* @return string The migration class name
|
||||
*/
|
||||
public function get_class_from_migration($migration)
|
||||
{
|
||||
if (is_string($migration))
|
||||
{
|
||||
$migration = str_replace(array(':', '/'), ' ', $migration);
|
||||
}
|
||||
else
|
||||
{
|
||||
$migration = str_replace('/', ' ', $migration['group']).'_'.$migration['timestamp'];
|
||||
}
|
||||
|
||||
return 'Migration_'.str_replace(array(' ', '-'), '_', ucwords($migration));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -495,4 +495,165 @@ class Minion_Migration_ModelTest extends Kohana_Unittest_Database_TestCase
|
|||
$this->getModel()->resolve_target( (array) $group, $target)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_compile_migrations_from_files()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provider_compile_migrations_from_files()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'myapp:015151051' => array('group' => 'myapp', 'description' => 'setup', 'timestamp' => '015151051', 'id' => 'myapp:015151051'),
|
||||
'myapp:015161051' => array('group' => 'myapp', 'description' => 'add-comments', 'timestamp' => '015161051', 'id' => 'myapp:015161051'),
|
||||
),
|
||||
array(
|
||||
'migrations/myapp' => array(
|
||||
// This file should be ignored
|
||||
'migrations/myapp/015151051_setup.sql'
|
||||
=> '/var/www/app/groups/myapp/migrations/myapp/015151051_setup.sql',
|
||||
'migrations/myapp/015151051_setup.php'
|
||||
=> '/var/www/app/groups/myapp/migrations/myapp/015151051_setup.php',
|
||||
'migrations/myapp/015161051_add-comments.php'
|
||||
=> '/var/www/app/groups/myapp/migrations/myapp/015161051_add-comments.php',
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that Minion_Migration_Util::compile_migrations_from_files accurately
|
||||
* compiles a set of files down into a set of migration files
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::compile_migrations_from_files
|
||||
* @dataProvider provider_compile_migrations_from_files
|
||||
* @param array Expected output
|
||||
* @param array Input Files
|
||||
*/
|
||||
public function test_compile_migrations_from_files($expected, array $files)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getModel()->compile_migrations_from_files($files)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_extract_migration_info_from_filename
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_migration_from_filename()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'group' => 'myapp',
|
||||
'description' => 'initial-setup',
|
||||
'timestamp' => '1293214439',
|
||||
'id' => 'myapp:1293214439',
|
||||
),
|
||||
'migrations/myapp/1293214439_initial-setup.php',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_migration_info_from_filename()
|
||||
* correctly extracts information about the migration from its filename
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_migration_from_filename
|
||||
* @dataProvider provider_get_migration_from_filename
|
||||
* @param array Expected output
|
||||
* @param string Input filename
|
||||
*/
|
||||
public function test_get_migration_from_filename($expected, $file)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getModel()->get_migration_from_filename($file)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_convert_migration_to_filename
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_filename_from_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'myapp/1293214439_initial-setup.php',
|
||||
array(
|
||||
'group' => 'myapp',
|
||||
'timestamp' => '1293214439',
|
||||
'description' => 'initial-setup',
|
||||
'id' => 'myapp:1293214439'
|
||||
),
|
||||
'myapp',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_filename_from_migration generates
|
||||
* accurate filenames when given a variety of migration information
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_filename_from_migration
|
||||
* @dataProvider provider_get_filename_from_migration
|
||||
* @param string Expected output
|
||||
* @param mixed Migration id
|
||||
* @param mixed group
|
||||
*/
|
||||
public function test_get_filename_from_migration($expected, $migration, $group)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getModel()->get_filename_from_migration($migration, $group)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_get_class_from_migration
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_class_from_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'Migration_Kohana_201012290258',
|
||||
'kohana:201012290258',
|
||||
),
|
||||
array(
|
||||
'Migration_Kohana_201012290258',
|
||||
array('group' => 'kohana', 'timestamp' => '201012290258'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_class_from_migration can generate
|
||||
* a class name from information about a migration
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_class_from_migration
|
||||
* @dataProvider provider_get_class_from_migration
|
||||
* @param string Expected output
|
||||
* @param string|array Migration info
|
||||
*/
|
||||
public function test_get_class_from_migration($expected, $migration)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
$this->getModel()->get_class_from_migration($migration)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,169 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Testcase for Minion_Migration_Util
|
||||
*
|
||||
* @group minion
|
||||
* @group minion.tasks
|
||||
* @group minion.tasks.migrations
|
||||
*/
|
||||
class Minion_Migration_UtilTest extends Kohana_Unittest_TestCase {
|
||||
|
||||
/**
|
||||
* Provides test data for test_compile_migrations_from_files()
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provider_compile_migrations_from_files()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'myapp:015151051' => array('group' => 'myapp', 'description' => 'setup', 'timestamp' => '015151051', 'id' => 'myapp:015151051'),
|
||||
'myapp:015161051' => array('group' => 'myapp', 'description' => 'add-comments', 'timestamp' => '015161051', 'id' => 'myapp:015161051'),
|
||||
),
|
||||
array(
|
||||
'migrations/myapp' => array(
|
||||
'migrations/myapp/015151051_setup.php'
|
||||
=> '/var/www/app/groups/myapp/migrations/myapp/015151051_setup.php',
|
||||
'migrations/myapp/015161051_add-comments.php'
|
||||
=> '/var/www/app/groups/myapp/migrations/myapp/015161051_add-comments.php',
|
||||
),
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that Minion_Migration_Util::compile_migrations_from_files accurately
|
||||
* compiles a set of files down into a set of migration files
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::compile_migrations_from_files
|
||||
* @dataProvider provider_compile_migrations_from_files
|
||||
* @param array Expected output
|
||||
* @param array Input Files
|
||||
*/
|
||||
public function test_compile_migrations_from_files($expected, array $files)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
Minion_Migration_Util::compile_migrations_from_files($files)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_extract_migration_info_from_filename
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_migration_from_filename()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'group' => 'myapp',
|
||||
'description' => 'initial-setup',
|
||||
'timestamp' => '1293214439',
|
||||
'id' => 'myapp:1293214439',
|
||||
),
|
||||
'migrations/myapp/1293214439_initial-setup.php',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_migration_info_from_filename()
|
||||
* correctly extracts information about the migration from its filename
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_migration_from_filename
|
||||
* @dataProvider provider_get_migration_from_filename
|
||||
* @param array Expected output
|
||||
* @param string Input filename
|
||||
*/
|
||||
public function test_get_migration_from_filename($expected, $file)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
Minion_Migration_Util::get_migration_from_filename($file)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_convert_migration_to_filename
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_filename_from_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'myapp/1293214439_initial-setup.php',
|
||||
array(
|
||||
'group' => 'myapp',
|
||||
'timestamp' => '1293214439',
|
||||
'description' => 'initial-setup',
|
||||
'id' => 'myapp:1293214439'
|
||||
),
|
||||
'myapp',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_filename_from_migration generates
|
||||
* accurate filenames when given a variety of migration information
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_filename_from_migration
|
||||
* @dataProvider provider_get_filename_from_migration
|
||||
* @param string Expected output
|
||||
* @param mixed Migration id
|
||||
* @param mixed group
|
||||
*/
|
||||
public function test_get_filename_from_migration($expected, $migration, $group)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
Minion_Migration_Util::get_filename_from_migration($migration, $group)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides test data for test_get_class_from_migration
|
||||
*
|
||||
* @return array Test Data
|
||||
*/
|
||||
public function provider_get_class_from_migration()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'Migration_Kohana_201012290258',
|
||||
'kohana:201012290258',
|
||||
),
|
||||
array(
|
||||
'Migration_Kohana_201012290258',
|
||||
array('group' => 'kohana', 'timestamp' => '201012290258'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that Minion_Migration_Util::get_class_from_migration can generate
|
||||
* a class name from information about a migration
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Migration_Util::get_class_from_migration
|
||||
* @dataProvider provider_get_class_from_migration
|
||||
* @param string Expected output
|
||||
* @param string|array Migration info
|
||||
*/
|
||||
public function test_get_class_from_migration($expected, $migration)
|
||||
{
|
||||
$this->assertSame(
|
||||
$expected,
|
||||
Minion_Migration_Util::get_class_from_migration($migration)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue