Merge pull request #626 from MyIgel/migration
migration: Order by migrated and append not migrated, add more help parameters
This commit is contained in:
commit
2ef5a7621a
|
@ -19,8 +19,8 @@ $app->register(MigrationServiceProvider::class);
|
||||||
$migration = $app->get('db.migration');
|
$migration = $app->get('db.migration');
|
||||||
$migration->setOutput(function ($text) { echo $text . PHP_EOL; });
|
$migration->setOutput(function ($text) { echo $text . PHP_EOL; });
|
||||||
|
|
||||||
if (isset($argv[1]) && strtolower($argv[1]) == 'help') {
|
if (isset($argv[1]) && in_array(strtolower($argv[1]), ['help', '--help', '-h'])) {
|
||||||
echo PHP_EOL . 'Usage: ' . $argv[1] . ' [up|down] [one-step]' . PHP_EOL . PHP_EOL;
|
echo PHP_EOL . 'Usage: ' . $argv[0] . ' [up|down] [one-step]' . PHP_EOL . PHP_EOL;
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,26 +50,33 @@ class Migrate
|
||||||
public function run($path, $type = self::UP, $oneStep = false)
|
public function run($path, $type = self::UP, $oneStep = false)
|
||||||
{
|
{
|
||||||
$this->initMigration();
|
$this->initMigration();
|
||||||
$migrations = $this->getMigrations($path);
|
$migrations = $this->mergeMigrations(
|
||||||
$migrated = $this->getMigrated();
|
$this->getMigrations($path),
|
||||||
|
$this->getMigrated()
|
||||||
|
);
|
||||||
|
|
||||||
if ($type == self::DOWN) {
|
if ($type == self::DOWN) {
|
||||||
$migrations = array_reverse($migrations, true);
|
$migrations = $migrations->reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($migrations as $file => $migration) {
|
foreach ($migrations as $migration) {
|
||||||
|
/** @var array $migration */
|
||||||
|
$name = $migration['migration'];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
($type == self::UP && $migrated->contains('migration', $migration))
|
($type == self::UP && isset($migration['id']))
|
||||||
|| ($type == self::DOWN && !$migrated->contains('migration', $migration))
|
|| ($type == self::DOWN && !isset($migration['id']))
|
||||||
) {
|
) {
|
||||||
call_user_func($this->output, 'Skipping ' . $migration);
|
($this->output)('Skipping ' . $name);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func($this->output, 'Migrating ' . $migration . ' (' . $type . ')');
|
($this->output)('Migrating ' . $name . ' (' . $type . ')');
|
||||||
|
|
||||||
$this->migrate($file, $migration, $type);
|
if (isset($migration['path'])) {
|
||||||
$this->setMigrated($migration, $type);
|
$this->migrate($migration['path'], $name, $type);
|
||||||
|
}
|
||||||
|
$this->setMigrated($name, $type);
|
||||||
|
|
||||||
if ($oneStep) {
|
if ($oneStep) {
|
||||||
return;
|
return;
|
||||||
|
@ -92,6 +99,38 @@ class Migrate
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge file migrations with already migrated tables
|
||||||
|
*
|
||||||
|
* @param Collection $migrations
|
||||||
|
* @param Collection $migrated
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
protected function mergeMigrations(Collection $migrations, Collection $migrated)
|
||||||
|
{
|
||||||
|
$return = $migrated;
|
||||||
|
$return->transform(function ($migration) use ($migrations) {
|
||||||
|
$migration = (array)$migration;
|
||||||
|
if ($migrations->contains('migration', $migration['migration'])) {
|
||||||
|
$migration += $migrations
|
||||||
|
->where('migration', $migration['migration'])
|
||||||
|
->first();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $migration;
|
||||||
|
});
|
||||||
|
|
||||||
|
$migrations->each(function ($migration) use ($return) {
|
||||||
|
if ($return->contains('migration', $migration['migration'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$return->add($migration);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all migrated migrations
|
* Get all migrated migrations
|
||||||
*
|
*
|
||||||
|
@ -99,7 +138,9 @@ class Migrate
|
||||||
*/
|
*/
|
||||||
protected function getMigrated()
|
protected function getMigrated()
|
||||||
{
|
{
|
||||||
return $this->getTableQuery()->get();
|
return $this->getTableQuery()
|
||||||
|
->orderBy('id')
|
||||||
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -144,20 +185,24 @@ class Migrate
|
||||||
* Get a list of migration files
|
* Get a list of migration files
|
||||||
*
|
*
|
||||||
* @param string $dir
|
* @param string $dir
|
||||||
* @return array
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
protected function getMigrations($dir)
|
protected function getMigrations($dir)
|
||||||
{
|
{
|
||||||
$files = $this->getMigrationFiles($dir);
|
$files = $this->getMigrationFiles($dir);
|
||||||
|
|
||||||
$migrations = [];
|
$migrations = new Collection();
|
||||||
foreach ($files as $dir) {
|
foreach ($files as $dir) {
|
||||||
$name = str_replace('.php', '', basename($dir));
|
$name = str_replace('.php', '', basename($dir));
|
||||||
$migrations[$dir] = $name;
|
$migrations[] = [
|
||||||
|
'migration' => $name,
|
||||||
|
'path' => $dir,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
asort($migrations);
|
return $migrations->sortBy(function ($value) {
|
||||||
return $migrations;
|
return $value['migration'];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -18,6 +18,7 @@ class MigrateTest extends TestCase
|
||||||
* @covers \Engelsystem\Database\Migration\Migrate::getMigrations
|
* @covers \Engelsystem\Database\Migration\Migrate::getMigrations
|
||||||
* @covers \Engelsystem\Database\Migration\Migrate::run
|
* @covers \Engelsystem\Database\Migration\Migrate::run
|
||||||
* @covers \Engelsystem\Database\Migration\Migrate::setOutput
|
* @covers \Engelsystem\Database\Migration\Migrate::setOutput
|
||||||
|
* @covers \Engelsystem\Database\Migration\Migrate::mergeMigrations
|
||||||
*/
|
*/
|
||||||
public function testRun()
|
public function testRun()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue