Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from spiral/pr/20-fix_sorting
Browse files Browse the repository at this point in the history
Fix files sorting
  • Loading branch information
vvval committed Oct 12, 2020
2 parents 706b925 + 4d0faec commit fade5c0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
11 changes: 6 additions & 5 deletions src/FileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Spiral\Migrations;

use Doctrine\Common\Inflector\Inflector;
use Spiral\Core\Container;
use Spiral\Core\FactoryInterface;
use Spiral\Files\Files;
Expand Down Expand Up @@ -63,6 +62,8 @@ public function __construct(MigrationConfig $config, FactoryInterface $factory =
*/
public function getMigrations(): array
{
$timestamps = [];
$chunks = [];
$migrations = [];

foreach ($this->getFiles() as $f) {
Expand All @@ -74,12 +75,12 @@ public function getMigrations(): array
/** @var MigrationInterface $migration */
$migration = $this->factory->make($f['class']);

$migrations[$f['created']->getTimestamp() . $f['chunk']] = $migration->withState(
new State($f['name'], $f['created'])
);
$timestamps[] = $f['created']->getTimestamp();
$chunks[] = $f['chunk'];
$migrations[] = $migration->withState(new State($f['name'], $f['created']));
}

ksort($migrations);
array_multisort($timestamps, $chunks, SORT_ASC | SORT_NATURAL, $migrations);

return $migrations;
}
Expand Down
1 change: 0 additions & 1 deletion tests/Migrations/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ protected function makeMessage(string $table, Comparator $comparator)
$names = [];
foreach ($comparator->alteredColumns() as $pair) {
$names[] = $pair[0]->getName();
print_r($pair);
}

return "Table '{$table}' not synced, column(s) '" . join(
Expand Down
16 changes: 4 additions & 12 deletions tests/Migrations/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,9 @@ public function testCreateWithForeignAliased(): void
$this->assertTrue($blueprint->getSchema()->exists());
}

/**
* @expectedException \Spiral\Migrations\Exception\Operation\TableException
*/
public function testUpdateTableError(): void
{
$this->expectException(\Spiral\Migrations\Exception\Operation\TableException::class);
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
Expand Down Expand Up @@ -173,11 +171,9 @@ public function testUpdateTable(): void
->update();
}

/**
* @expectedException \Spiral\Migrations\Exception\Operation\ColumnException
*/
public function testUpdateTableError2(): void
{
$this->expectException(\Spiral\Migrations\Exception\Operation\ColumnException::class);
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
Expand All @@ -193,11 +189,9 @@ public function testUpdateTableError2(): void
$blueprint->addColumn('value', 'int')->update();
}

/**
* @expectedException \Spiral\Migrations\Exception\Operation\ColumnException
*/
public function testUpdateTableError5(): void
{
$this->expectException(\Spiral\Migrations\Exception\Operation\ColumnException::class);
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
Expand All @@ -213,11 +207,9 @@ public function testUpdateTableError5(): void
$blueprint->addColumn('value', 'int')->update();
}

/**
* @expectedException \Spiral\Migrations\Exception\Operation\IndexException
*/
public function testUpdateTableError3(): void
{
$this->expectException(\Spiral\Migrations\Exception\Operation\IndexException::class);
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');

$blueprint->addColumn('id', 'primary')
Expand Down
12 changes: 3 additions & 9 deletions tests/Migrations/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,29 +436,23 @@ public function testBadDateFormatMigrationFile(): void
$this->repository->getMigrations();
}

/**
* @expectedException \Spiral\Migrations\Exception\RepositoryException
*/
public function testDuplicateClassMigration(): void
{
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
$this->repository->registerMigration('unique_name_1', DuplicateColumnMigration::class);
$this->repository->registerMigration('unique_name_2', DuplicateColumnMigration::class);
}

/**
* @expectedException \Spiral\Migrations\Exception\RepositoryException
*/
public function testDuplicateFileNameMigration(): void
{
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
$this->repository->registerMigration('camel_case_duplicate', DuplicateColumnMigration::class);
$this->repository->registerMigration('camelCaseDuplicate', CreateEmptyMigration::class);
}

/**
* @expectedException \Spiral\Migrations\Exception\RepositoryException
*/
public function testInvalidMigration(): void
{
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
$this->repository->registerMigration('m', 'invalid');
}
}
34 changes: 30 additions & 4 deletions tests/Migrations/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,39 @@
namespace Spiral\Migrations\Tests;

use Spiral\Migrations\Capsule;
use Spiral\Migrations\State;
use Spiral\Migrations\Exception\MigrationException;
use Spiral\Migrations\State;

abstract class MigratorTest extends BaseTest
{
public function testSortingOrder(): void
{
$files = [
'20200909.024119_333_333_migration_1.php' => 'A3',
'20200909.030203_22_22_migration_1.php' => 'B2',
'20200909.030203_23_23_migration_1.php' => 'B3',
'20200909.024119_1_1_migration_1.php' => 'A1',
'20200909.024119_22_22_migration_2.php' => 'A2',
'20200909.024119_4444_4444_migration_2.php' => 'A4',
'20200923.040608_0_0_migration_3.php' => 'C',
'20200909.030203_1_1_migration_1.php' => 'B1',
];
$stub = file_get_contents(__DIR__ . '/../files/migration.stub');
foreach ($files as $name => $class) {
file_put_contents(__DIR__ . "/../files/$name", sprintf($stub, $class));
}

$migrations = $this->repository->getMigrations();
$classes = array_map(
static function ($migration) {
return get_class($migration);
},
array_values($migrations)
);

$this->assertSame(['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'C'], $classes);
}

public function testIsConfigured(): void
{
$this->assertFalse($this->migrator->isConfigured());
Expand Down Expand Up @@ -101,11 +129,9 @@ public function testCapsule(): void
$this->assertTrue($capsule->getTable('test')->exists());
}

/**
* @expectedException \Spiral\Migrations\Exception\CapsuleException
*/
public function testCapsuleException(): void
{
$this->expectException(\Spiral\Migrations\Exception\CapsuleException::class);
$capsule = new Capsule($this->db);

$capsule->execute([
Expand Down
12 changes: 12 additions & 0 deletions tests/files/migration.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class %s extends \Spiral\Migrations\Migration
{
public function up()
{
}

public function down()
{
}
}

0 comments on commit fade5c0

Please sign in to comment.