Skip to content

Commit fade5c0

Browse files
authored
Merge pull request cycle#21 from spiral/pr/20-fix_sorting
Fix files sorting
2 parents 706b925 + 4d0faec commit fade5c0

File tree

6 files changed

+55
-31
lines changed

6 files changed

+55
-31
lines changed

src/FileRepository.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Spiral\Migrations;
1313

14-
use Doctrine\Common\Inflector\Inflector;
1514
use Spiral\Core\Container;
1615
use Spiral\Core\FactoryInterface;
1716
use Spiral\Files\Files;
@@ -63,6 +62,8 @@ public function __construct(MigrationConfig $config, FactoryInterface $factory =
6362
*/
6463
public function getMigrations(): array
6564
{
65+
$timestamps = [];
66+
$chunks = [];
6667
$migrations = [];
6768

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

77-
$migrations[$f['created']->getTimestamp() . $f['chunk']] = $migration->withState(
78-
new State($f['name'], $f['created'])
79-
);
78+
$timestamps[] = $f['created']->getTimestamp();
79+
$chunks[] = $f['chunk'];
80+
$migrations[] = $migration->withState(new State($f['name'], $f['created']));
8081
}
8182

82-
ksort($migrations);
83+
array_multisort($timestamps, $chunks, SORT_ASC | SORT_NATURAL, $migrations);
8384

8485
return $migrations;
8586
}

tests/Migrations/BaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ protected function makeMessage(string $table, Comparator $comparator)
400400
$names = [];
401401
foreach ($comparator->alteredColumns() as $pair) {
402402
$names[] = $pair[0]->getName();
403-
print_r($pair);
404403
}
405404

406405
return "Table '{$table}' not synced, column(s) '" . join(

tests/Migrations/BlueprintTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ public function testCreateWithForeignAliased(): void
131131
$this->assertTrue($blueprint->getSchema()->exists());
132132
}
133133

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

141139
$blueprint->addColumn('id', 'primary')
@@ -173,11 +171,9 @@ public function testUpdateTable(): void
173171
->update();
174172
}
175173

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

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

196-
/**
197-
* @expectedException \Spiral\Migrations\Exception\Operation\ColumnException
198-
*/
199192
public function testUpdateTableError5(): void
200193
{
194+
$this->expectException(\Spiral\Migrations\Exception\Operation\ColumnException::class);
201195
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');
202196

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

216-
/**
217-
* @expectedException \Spiral\Migrations\Exception\Operation\IndexException
218-
*/
219210
public function testUpdateTableError3(): void
220211
{
212+
$this->expectException(\Spiral\Migrations\Exception\Operation\IndexException::class);
221213
$blueprint = new TableBlueprint($capsule = new Capsule($this->db), 'sample');
222214

223215
$blueprint->addColumn('id', 'primary')

tests/Migrations/ExceptionsTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,29 +436,23 @@ public function testBadDateFormatMigrationFile(): void
436436
$this->repository->getMigrations();
437437
}
438438

439-
/**
440-
* @expectedException \Spiral\Migrations\Exception\RepositoryException
441-
*/
442439
public function testDuplicateClassMigration(): void
443440
{
441+
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
444442
$this->repository->registerMigration('unique_name_1', DuplicateColumnMigration::class);
445443
$this->repository->registerMigration('unique_name_2', DuplicateColumnMigration::class);
446444
}
447445

448-
/**
449-
* @expectedException \Spiral\Migrations\Exception\RepositoryException
450-
*/
451446
public function testDuplicateFileNameMigration(): void
452447
{
448+
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
453449
$this->repository->registerMigration('camel_case_duplicate', DuplicateColumnMigration::class);
454450
$this->repository->registerMigration('camelCaseDuplicate', CreateEmptyMigration::class);
455451
}
456452

457-
/**
458-
* @expectedException \Spiral\Migrations\Exception\RepositoryException
459-
*/
460453
public function testInvalidMigration(): void
461454
{
455+
$this->expectException(\Spiral\Migrations\Exception\RepositoryException::class);
462456
$this->repository->registerMigration('m', 'invalid');
463457
}
464458
}

tests/Migrations/MigratorTest.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,39 @@
1212
namespace Spiral\Migrations\Tests;
1313

1414
use Spiral\Migrations\Capsule;
15-
use Spiral\Migrations\State;
1615
use Spiral\Migrations\Exception\MigrationException;
16+
use Spiral\Migrations\State;
1717

1818
abstract class MigratorTest extends BaseTest
1919
{
20+
public function testSortingOrder(): void
21+
{
22+
$files = [
23+
'20200909.024119_333_333_migration_1.php' => 'A3',
24+
'20200909.030203_22_22_migration_1.php' => 'B2',
25+
'20200909.030203_23_23_migration_1.php' => 'B3',
26+
'20200909.024119_1_1_migration_1.php' => 'A1',
27+
'20200909.024119_22_22_migration_2.php' => 'A2',
28+
'20200909.024119_4444_4444_migration_2.php' => 'A4',
29+
'20200923.040608_0_0_migration_3.php' => 'C',
30+
'20200909.030203_1_1_migration_1.php' => 'B1',
31+
];
32+
$stub = file_get_contents(__DIR__ . '/../files/migration.stub');
33+
foreach ($files as $name => $class) {
34+
file_put_contents(__DIR__ . "/../files/$name", sprintf($stub, $class));
35+
}
36+
37+
$migrations = $this->repository->getMigrations();
38+
$classes = array_map(
39+
static function ($migration) {
40+
return get_class($migration);
41+
},
42+
array_values($migrations)
43+
);
44+
45+
$this->assertSame(['A1', 'A2', 'A3', 'A4', 'B1', 'B2', 'B3', 'C'], $classes);
46+
}
47+
2048
public function testIsConfigured(): void
2149
{
2250
$this->assertFalse($this->migrator->isConfigured());
@@ -101,11 +129,9 @@ public function testCapsule(): void
101129
$this->assertTrue($capsule->getTable('test')->exists());
102130
}
103131

104-
/**
105-
* @expectedException \Spiral\Migrations\Exception\CapsuleException
106-
*/
107132
public function testCapsuleException(): void
108133
{
134+
$this->expectException(\Spiral\Migrations\Exception\CapsuleException::class);
109135
$capsule = new Capsule($this->db);
110136

111137
$capsule->execute([

tests/files/migration.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
class %s extends \Spiral\Migrations\Migration
4+
{
5+
public function up()
6+
{
7+
}
8+
9+
public function down()
10+
{
11+
}
12+
}

0 commit comments

Comments
 (0)