Skip to content

Commit

Permalink
[GH-4243] Move test up to generic SchemaManagerFunctionalTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Sep 9, 2020
1 parent f5ccf9c commit 0c384b4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,56 @@ public function testSchemaDiffForeignKeys(): void
);
}
}

public function testUnnamedForeignKeyConstraintHandling(): void
{
$sm = $this->connection->getSchemaManager();
$sm->tryMethod('dropTable', 'unnamedfk_referencing');
$sm->tryMethod('dropTable', 'unnamedfk_referenced1');
$sm->tryMethod('dropTable', 'unnamedfk_referenced2');

$schema = new Schema();

$referencingTable = $schema->createTable('unnamedfk_referencing');
$referencingTable->addColumn('id', 'integer', ['autoincrement' => true]);
$referencingTable->addColumn('reference1_id1', 'integer');
$referencingTable->addColumn('reference1_id2', 'integer');
$referencingTable->addColumn('reference2_id', 'integer');
$referencingTable->setPrimaryKey(['id']);

$referenced1Table = $schema->createTable('unnamedfk_referenced1');
$referenced1Table->addColumn('id1', 'integer', ['notnull' => true]);
$referenced1Table->addColumn('id2', 'integer', ['notnull' => true]);
$referenced1Table->setPrimaryKey(['id1', 'id2']);

$referenced2Table = $schema->createTable('unnamedfk_referenced2');
$referenced2Table->addColumn('id', 'integer', ['notnull' => true]);
$referenced2Table->setPrimaryKey(['id']);

$referencingTable->addForeignKeyConstraint(
'unnamedfk_referenced1',
['reference1_id1', 'reference1_id2'],
['id1', 'id2']
);

$referencingTable->addForeignKeyConstraint(
'unnamedfk_referenced2',
['reference2_id'],
['id']
);

$sqls = $schema->toSql($this->connection->getDatabasePlatform());

foreach ($sqls as $sql) {
$this->connection->exec($sql);
}

$comparator = new Comparator();
$onlineTableAfter = $sm->listTableDetails('unnamedfk_referencing');
$diff = $comparator->diffTable($referencingTable, $onlineTableAfter);

$this->assertFalse($diff);
}
}

interface ListTableColumnsDispatchEventListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,54 +257,4 @@ public function testOnlyOwnCommentIsParsed(): void
->getColumn('col1')
->getComment());
}

public function testUnnamedForeignKeyConstraintHandling(): void
{
$sql = <<<SQL
DROP TABLE IF EXISTS "unnamedfk_referenced1";
DROP TABLE IF EXISTS "unnamedfk_referenced2";
DROP TABLE IF EXISTS "unnamedfk_referencing";
CREATE TABLE "unnamedfk_referenced1" (
"id1" integer not null,
"id2" integer not null,
primary key("id1", "id2")
);
CREATE TABLE "unnamedfk_referenced2" (
"id" integer not null primary key autoincrement
);
CREATE TABLE "unnamedfk_referencing" (
"id" integer not null primary key autoincrement,
"reference1_id1" integer not null,
"reference1_id2" integer not null,
"reference2_id" integer not null,
foreign key("reference1_id1", "reference1_id2") references "unnamedfk_referenced1"("id1", "id2") on delete cascade,
foreign key("reference2_id") references "unnamedfk_referenced2"("id") on delete cascade
)
SQL;

$this->connection->exec($sql);

$sm = $this->connection->getSchemaManager();
$onlineTable = $sm->listTableDetails('unnamedfk_referencing');

$offlineTable = new Table('unnamedfk_referencing');
$offlineTable->addColumn('id', 'integer');
$offlineTable->addColumn('reference1_id1', 'integer');
$offlineTable->addColumn('reference1_id2', 'integer');
$offlineTable->addColumn('reference2_id', 'integer');

$comparator = new Schema\Comparator();
$diff = $comparator->diffTable($offlineTable, $onlineTable);

$sqls = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff);

foreach ($sqls as $sql) {
$this->connection->exec($sql);
}

$onlineTableAfter = $sm->listTableDetails('unnamedfk_referencing');
$diff = $comparator->diffTable($onlineTable, $onlineTableAfter);

$this->assertFalse($diff);
}
}

0 comments on commit 0c384b4

Please sign in to comment.