Skip to content

Commit db56890

Browse files
committed
Correct Migration table definition
1 parent 3f1c54c commit db56890

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

Tests/Unit/MigrationTest.php

+34-5
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,46 @@ public function testMigrations() : void
1010
$tables = \PHPFUI\ORM::getTables();
1111
$this->assertContains('migration', $tables);
1212
$migrator = new \PHPFUI\ORM\Migrator();
13-
$migrator->migrateUpOne();
13+
$this->assertEmpty($migrator->getErrors());
14+
$this->assertTrue($migrator->migrationNeeded());
15+
$this->assertEquals(1, $migrator->migrateUpOne());
16+
$this->assertEquals('Migrated to 1 successfully', $migrator->getStatus());
17+
$this->assertEmpty($migrator->getErrors());
1418
$migrationTable = new \PHPFUI\ORM\Table\Migration();
1519
$this->assertCount(1, $migrationTable);
16-
$migrator->migrateUpOne();
20+
$this->assertEquals(2, $migrator->migrateUpOne());
21+
$this->assertEquals('Migrated to 2 successfully', $migrator->getStatus());
22+
$this->assertEmpty($migrator->getErrors());
1723
$this->assertCount(2, $migrationTable);
18-
$migrator->migrateDownOne();
24+
$this->assertEquals(1, $migrator->migrateDownOne());
25+
$this->assertEquals('Migrated to 1 successfully', $migrator->getStatus());
26+
$this->assertEmpty($migrator->getErrors());
1927
$this->assertCount(1, $migrationTable);
2028
$tables = \PHPFUI\ORM::getTables();
21-
$migrator->migrateDownOne();
29+
$this->assertEquals(0, $migrator->migrateDownOne());
30+
$this->assertEquals('Migrated to 0 successfully', $migrator->getStatus());
31+
$this->assertEmpty($migrator->getErrors());
2232
$this->assertCount(0, $migrationTable);
23-
$migrator->migrate();
33+
$this->assertEquals(3, $migrator->migrate());
34+
$this->assertEquals('Migrated to 3 successfully', $migrator->getStatus());
35+
$this->assertEmpty($migrator->getErrors());
2436
$this->assertCount(3, $migrationTable);
37+
$this->assertCount(3, $migrator);
38+
$this->assertEquals(3, $migrator->getCurrentMigrationId());
39+
$this->assertFalse($migrator->migrationNeeded());
40+
$migrator->migrateTo(1);
41+
$this->assertEquals('Migrated to 1 successfully', $migrator->getStatus());
42+
$this->assertEmpty($migrator->getErrors());
43+
$this->assertCount(1, $migrationTable);
44+
$this->assertCount(3, $migrator->getMigrationObjects(0, 100));
45+
$this->assertCount(1, $migrator->getMigrationObjects(0, 1));
46+
$this->assertEquals(\Tests\Fixtures\Migration\Migration_1::class, \get_debug_type($migrator->getMigrationObject(1)));
47+
48+
$migrator->migrateTo(-1);
49+
$this->assertCount(1, $migrator->getErrors());
50+
$this->assertContains('Negative Migration Id (-1) is invalid', $migrator->getErrors());
51+
$migrator->migrateTo(4);
52+
$this->assertCount(2, $migrator->getErrors());
53+
$this->assertContains('Target migration of 4 does not exist', $migrator->getErrors());
2554
}
2655
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"psr/log": "^3.0"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "<=12.0",
20+
"phpunit/phpunit": "^10.0 | ^11.0 | ^12.0",
2121
"phpfui/phpunit-syntax-coverage": "^1.0",
2222
"roave/security-advisories": "dev-latest",
2323
"friendsofphp/php-cs-fixer": "*",

src/PHPFUI/ORM/Migrator.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function migrateTo(int $migrationId) : bool
215215
$currentMigrationId = $previous;
216216
}
217217

218-
if ($currentMigrationId == $migrationId)
218+
if ($currentMigrationId === $migrationId)
219219
{
220220
$this->status = "Migrated to {$migrationId} successfully";
221221

@@ -237,8 +237,6 @@ public function migrateUpOne() : int
237237

238238
if (! $migration)
239239
{
240-
$this->status = "No next migration ({$id})";
241-
242240
return 0;
243241
}
244242
$result = $this->runUp($migration);
@@ -270,7 +268,10 @@ private function runDown(\PHPFUI\ORM\Migration $migration) : int
270268

271269
$highest = $this->migrationTable->getHighest();
272270

273-
return (int)$highest->migrationId;
271+
$id = (int)$highest->migrationId;
272+
$this->status = "Migrated to {$id} successfully";
273+
274+
return $id;
274275
}
275276

276277
/**
@@ -288,6 +289,9 @@ private function runUp(\PHPFUI\ORM\Migration $migration) : int
288289
$migrationRecord->migrationId = $migration->id();
289290
$migrationRecord->insert();
290291

291-
return $migration->id();
292+
$id = $migrationRecord->migrationId;
293+
$this->status = "Migrated to {$id} successfully";
294+
295+
return $id;
292296
}
293297
}

src/PHPFUI/ORM/Record/Definition/Migration.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace PHPFUI\ORM\Record\Definition;
44

55
/**
6-
* Autogenerated. Do not modify. Modify SQL table, then run oneOffScripts\generateCRUD.php table_name
6+
* Autogenerated. Do not modify. Modify SQL table, then generate with \PHPFUI\ORM\Tool\Generate\CRUD class.
77
*
8-
* @property int $migrationId MySQL type int(11)
8+
* @property int $migrationId MySQL type int
99
* @property \PHPFUI\ORM\Record\Migration $migration related record
10-
* @property string $ran MySQL type timestamp
10+
* @property string $ran MySQL type datetime
1111
*/
1212
abstract class Migration extends \PHPFUI\ORM\Record
1313
{
@@ -26,8 +26,8 @@ public function initFieldDefinitions() : static
2626
if (! \count(static::$fields))
2727
{
2828
static::$fields = [
29-
'migrationId' => new \PHPFUI\ORM\FieldDefinition('int(11)', 'int', 11, false, null, ),
30-
'ran' => new \PHPFUI\ORM\FieldDefinition('timestamp', 'string', 20, false, null, ),
29+
'migrationId' => new \PHPFUI\ORM\FieldDefinition('int', 'int', 0, false, ),
30+
'ran' => new \PHPFUI\ORM\FieldDefinition('datetime', 'string', 20, false, 'CURRENT_TIMESTAMP', ),
3131
];
3232
}
3333

0 commit comments

Comments
 (0)