Skip to content

Commit 9938e39

Browse files
committed
Migration testing
1 parent 9cf622f commit 9938e39

File tree

10 files changed

+106
-27
lines changed

10 files changed

+106
-27
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Migration;
4+
5+
class Migration_1 extends \PHPFUI\ORM\Migration
6+
{
7+
public function description() : string
8+
{
9+
return 'Crash Test Dummy';
10+
}
11+
12+
public function down() : bool
13+
{
14+
return true;
15+
}
16+
17+
public function up() : bool
18+
{
19+
return true;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Migration;
4+
5+
class Migration_2 extends \PHPFUI\ORM\Migration
6+
{
7+
public function description() : string
8+
{
9+
return 'Dummy #2';
10+
}
11+
12+
public function down() : bool
13+
{
14+
return true;
15+
}
16+
17+
public function up() : bool
18+
{
19+
return true;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tests\Fixtures\Migration;
4+
5+
class Migration_3 extends \PHPFUI\ORM\Migration
6+
{
7+
public function description() : string
8+
{
9+
return 'Another dummy';
10+
}
11+
12+
public function down() : bool
13+
{
14+
return true;
15+
}
16+
17+
public function up() : bool
18+
{
19+
return true;
20+
}
21+
}

Tests/Unit/MigrationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
class MigrationTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testMigrations() : void
8+
{
9+
$transaction = new \PHPFUI\ORM\Transaction();
10+
$tables = \PHPFUI\ORM::getTables();
11+
$this->assertContains('migration', $tables);
12+
$migrator = new \PHPFUI\ORM\Migrator();
13+
$migrator->migrateUpOne();
14+
$migrationTable = new \PHPFUI\ORM\Table\Migration();
15+
$this->assertCount(1, $migrationTable);
16+
$migrator->migrateUpOne();
17+
$this->assertCount(2, $migrationTable);
18+
$migrator->migrateDownOne();
19+
$this->assertCount(1, $migrationTable);
20+
$tables = \PHPFUI\ORM::getTables();
21+
$migrator->migrateDownOne();
22+
$this->assertCount(0, $migrationTable);
23+
$migrator->migrate();
24+
$this->assertCount(3, $migrationTable);
25+
}
26+
}

Tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function autoload(string $className) : void
3535
\PHPFUI\ORM::$namespaceRoot = __DIR__ . '/..';
3636
\PHPFUI\ORM::$recordNamespace = 'Tests\\App\\Record';
3737
\PHPFUI\ORM::$tableNamespace = 'Tests\\App\\Table';
38-
\PHPFUI\ORM::$migrationNamespace = 'Tests\\App\\Migration';
38+
\PHPFUI\ORM::$migrationNamespace = 'Tests\\Fixtures\\Migration';
3939
\PHPFUI\ORM::$idSuffix = '_id';
4040

4141
\PHPFUI\ORM::setTranslationCallback(\PHPFUI\Translation\Translator::trans(...));

northwind/northwind-default.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ create table dateRecord (
516516
timestampDefaultCurrentNotNull timestamp not null default CURRENT_TIMESTAMP,
517517
PRIMARY KEY (dateRecordId));
518518

519+
drop table if exists migration;
520+
create table migration (migrationId int NOT NULL primary key, ran TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
521+
519522
SET SQL_MODE=@OLD_SQL_MODE;
520523
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
521524
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

northwind/northwind-sqlite.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,7 @@ create table dateRecord (
505505
dateDefaultNotNull date not null default '2000-01-02',
506506
timestampDefaultCurrentNullable timestamp DEFAULT CURRENT_TIMESTAMP,
507507
timestampDefaultCurrentNotNull timestamp not null default CURRENT_TIMESTAMP);
508+
509+
drop table if exists migration;
510+
create table migration (migrationId int NOT NULL primary key, ran TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
511+

northwind/northwind.db

8 KB
Binary file not shown.

src/PHPFUI/ORM/Migration.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,7 @@ public function executeAlters() : bool
5757
*/
5858
public function getAllTables(string $type = 'BASE TABLE') : array
5959
{
60-
$where = '';
61-
62-
if ($type)
63-
{
64-
$where = " where Table_Type = '{$type}'";
65-
}
66-
67-
$tableArray = \PHPFUI\ORM::getRows('show full tables' . $where);
68-
69-
$tables = [];
70-
71-
foreach ($tableArray as $row)
72-
{
73-
$tables[] = \array_shift($row);
74-
}
75-
76-
return $tables;
60+
return \PHPFUI\ORM::getTables();
7761
}
7862

7963
/** @return string[] */
@@ -500,18 +484,17 @@ private function alter(string $type, string $table, string $field, string $extra
500484
$this->alters[$table][] = $sql;
501485
}
502486

503-
private function getFieldInfo(string $table, string $field) : array
487+
private function getFieldInfo(string $table, string $field) : ?PHPFUI\ORM\Schema\Field
504488
{
505-
$rows = \PHPFUI\ORM::getArrayCursor("SHOW COLUMNS FROM `{$table}`");
506-
507-
foreach ($rows as $row)
489+
$fields = \PHPFUI\ORM::describeTable($table);
490+
foreach ($fields as $field)
508491
{
509-
if (0 == \strcasecmp((string)$row['Field'], $field))
492+
if ($field->name == $field)
510493
{
511-
return $row;
494+
return $field;
512495
}
513496
}
514497

515-
return [];
498+
return null;
516499
}
517500
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ abstract class Migration extends \PHPFUI\ORM\Record
1616
/** @var array<string, array<mixed>> */
1717
protected static array $fields = [
1818
// MYSQL_TYPE, PHP_TYPE, LENGTH, KEY, ALLOWS_NULL, DEFAULT
19-
'migrationId' => ['int(11)', 'int', 11, true, false, ],
20-
'ran' => ['timestamp', 'string', 20, false, false, ],
19+
'migrationId' => ['int(11)', 'int', 11, true, false, null, ],
20+
'ran' => ['timestamp', 'string', 20, false, false, null, ],
2121
];
2222

2323
/** @var array<string, true> */

0 commit comments

Comments
 (0)