131 lines
5.5 KiB
PHP
131 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Db2;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
class SchemaTest extends Db2TestCase
|
|
{
|
|
public function test_table_and_column_lifecycle(): void
|
|
{
|
|
$db = $this->db();
|
|
$schema = $db->getSchemaBuilder();
|
|
$table = 'stage5_'.bin2hex(random_bytes(6));
|
|
$renamed = $table.'_renamed';
|
|
|
|
try {
|
|
$schema->create($table, function (Blueprint $table) {
|
|
$table->id();
|
|
});
|
|
$this->assertTrue($schema->hasTable($table));
|
|
|
|
$schema->table($table, function (Blueprint $table) {
|
|
$table->string('label', 20)->nullable();
|
|
});
|
|
$this->assertTrue($schema->hasColumn($table, 'label'));
|
|
|
|
$schema->table($table, function (Blueprint $table) {
|
|
$table->string('label', 40)->nullable()->change();
|
|
});
|
|
$this->assertSame('varchar(40)', strtolower($schema->getColumnType($table, 'label', true)));
|
|
|
|
$schema->table($table, function (Blueprint $table) {
|
|
$table->dropColumn('label');
|
|
});
|
|
$this->assertFalse($schema->hasColumn($table, 'label'));
|
|
|
|
$schema->rename($table, $renamed);
|
|
$this->assertFalse($schema->hasTable($table));
|
|
$this->assertTrue($schema->hasTable($renamed));
|
|
$schema->dropIfExists($renamed);
|
|
$schema->dropIfExists($renamed);
|
|
$this->assertFalse($schema->hasTable($renamed));
|
|
} finally {
|
|
foreach ([$table, $renamed] as $name) {
|
|
if ($schema->hasTable($name)) {
|
|
$db->statement('DROP TABLE "'.$name.'"');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_constraints_and_catalog_metadata(): void
|
|
{
|
|
$db = $this->db();
|
|
$schema = $db->getSchemaBuilder();
|
|
$namespace = $db->getConfig('schema');
|
|
$parent = 'stage5_'.bin2hex(random_bytes(6));
|
|
$child = $parent.'_child';
|
|
|
|
try {
|
|
$schema->create($namespace.'.'.$parent, function (Blueprint $table) {
|
|
$table->id();
|
|
$table->integer('code');
|
|
$table->primary('code', 'stage5_explicit_primary');
|
|
});
|
|
$schema->create($namespace.'.'.$child, function (Blueprint $table) use ($parent, $namespace) {
|
|
$table->id();
|
|
$table->integer('code')->default(1);
|
|
$table->index(['code', 'id'], 'i_'.$parent);
|
|
$table->unique(['id', 'code'], 'u_'.$parent);
|
|
$table->foreign('code', 'f_'.$parent)->references('code')->on($namespace.'.'.$parent)->cascadeOnDelete();
|
|
});
|
|
|
|
$columns = array_column($schema->getColumns($namespace.'.'.$child), null, 'name');
|
|
$this->assertTrue($columns['id']['auto_increment']);
|
|
$this->assertSame("'1'", $columns['code']['default']);
|
|
$indexes = array_column($schema->getIndexes($namespace.'.'.$child), null, 'name');
|
|
$this->assertSame(['code', 'id'], $indexes['i_'.$parent]['columns']);
|
|
$this->assertTrue($indexes['u_'.$parent]['unique']);
|
|
$foreign = array_column($schema->getForeignKeys($namespace.'.'.$child), null, 'name')['f_'.$parent];
|
|
$this->assertSame(['code'], $foreign['columns']);
|
|
$this->assertSame(['code'], $foreign['foreign_columns']);
|
|
$this->assertSame('cascade', $foreign['on_delete']);
|
|
|
|
$schema->table($namespace.'.'.$child, function (Blueprint $table) use ($parent) {
|
|
$table->dropForeign('f_'.$parent);
|
|
$table->dropIndex('i_'.$parent);
|
|
$table->dropUnique('u_'.$parent);
|
|
});
|
|
$this->assertSame([], $schema->getForeignKeys($child));
|
|
$this->assertFalse($schema->hasIndex($child, 'i_'.$parent));
|
|
$this->assertFalse($schema->hasIndex($child, 'u_'.$parent));
|
|
} finally {
|
|
foreach ([$child, $parent] as $name) {
|
|
if ($schema->hasTable($name)) {
|
|
$db->statement('DROP TABLE "'.$name.'"');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_laravel_migrator_records_up_and_rollback(): void
|
|
{
|
|
$db = $this->db();
|
|
$schema = $db->getSchemaBuilder();
|
|
$table = 'stage5_'.bin2hex(random_bytes(6));
|
|
$repository = new \Illuminate\Database\Migrations\DatabaseMigrationRepository(app('db'), $table.'_history');
|
|
$migrator = new \Illuminate\Database\Migrations\Migrator($repository, app('db'), app('files'), app('events'));
|
|
config(['stage5.table' => $table]);
|
|
$file = base_path('tests/Fixtures/Db2/2026_01_01_000000_stage5_schema.php');
|
|
|
|
try {
|
|
$migrator->usingConnection('db2_local', function () use ($file, $schema, $table, $repository, $migrator) {
|
|
$repository->createRepository();
|
|
$this->assertSame([$file], $migrator->run([$file]));
|
|
$this->assertTrue($schema->hasTable($table));
|
|
$this->assertCount(1, $repository->getRan());
|
|
$this->assertSame([$file], $migrator->rollback([$file]));
|
|
$this->assertFalse($schema->hasTable($table));
|
|
$this->assertSame([], $repository->getRan());
|
|
});
|
|
} finally {
|
|
foreach ([$table, $table.'_history'] as $name) {
|
|
if ($schema->hasTable($name)) {
|
|
$db->statement('DROP TABLE "'.$name.'"');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|