Skip to content

Commit 43a33ae

Browse files
committed
Adding check on autoincremented primary key
1 parent 7fe1589 commit 43a33ae

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/SchemaAnalyzer.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ private function isJunctionTable(Table $table) {
8181
$fkColumnNames[$fkColumns[0]] = true;
8282
}
8383

84-
// Let's check that the third column (the ID is NOT a foreign key)
85-
if (count($columns) == 3 && isset($fkColumnNames[$pkColumns[0]])) {
86-
return false;
84+
if (count($columns) == 3) {
85+
// Let's check that the third column (the ID is NOT a foreign key)
86+
if (isset($fkColumnNames[$pkColumns[0]])) {
87+
return false;
88+
}
89+
90+
// Let's check that the primary key is autoincremented
91+
if (!$table->getColumn($pkColumns[0])->getAutoincrement()) {
92+
return false;
93+
}
8794
}
8895

8996
return true;

tests/SchemaAnalyzerTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testJointureTableDetectionWith3Columns() {
6464
$schema = $this->getBaseSchema();
6565

6666
$role_right = $schema->createTable("role_right");
67-
$role_right->addColumn("id", "integer", array("unsigned" => true));
67+
$role_right->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
6868
$role_right->addColumn("role_id", "integer", array("unsigned" => true));
6969
$role_right->addColumn("right_id", "integer", array("unsigned" => true));
7070

@@ -82,11 +82,29 @@ public function testJointureTableDetectionWith3Columns() {
8282
}
8383
}
8484

85+
public function testJointureTableDetectionWith3ColumnsNoAutoincrement() {
86+
$schema = $this->getBaseSchema();
87+
88+
$role_right = $schema->createTable("role_right");
89+
$role_right->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => false));
90+
$role_right->addColumn("role_id", "integer", array("unsigned" => true));
91+
$role_right->addColumn("right_id", "integer", array("unsigned" => true));
92+
93+
$role_right->addForeignKeyConstraint($schema->getTable('role'), array("role_id"), array("id"), array("onUpdate" => "CASCADE"));
94+
$role_right->addForeignKeyConstraint($schema->getTable('right'), array("right_id"), array("id"), array("onUpdate" => "CASCADE"));
95+
$role_right->setPrimaryKey(["id"]);
96+
97+
$schemaAnalyzer = new SchemaAnalyzer($schema);
98+
$junctionTables = $schemaAnalyzer->detectJunctionTables();
99+
100+
$this->assertCount(0, $junctionTables);
101+
}
102+
85103
public function testJointureTableDetectionWith4Columns() {
86104
$schema = $this->getBaseSchema();
87105

88106
$role_right = $schema->createTable("role_right");
89-
$role_right->addColumn("id", "integer", array("unsigned" => true));
107+
$role_right->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
90108
$role_right->addColumn("role_id", "integer", array("unsigned" => true));
91109
$role_right->addColumn("right_id", "integer", array("unsigned" => true));
92110
$role_right->addColumn("label", "string", array("length" => 32));
@@ -140,7 +158,7 @@ public function testJointureTableDetectionWith3ColumnsWithPkIsFk() {
140158
$schema = $this->getBaseSchema();
141159

142160
$role_right = $schema->createTable("role_right");
143-
$role_right->addColumn("id", "integer", array("unsigned" => true));
161+
$role_right->addColumn("id", "integer", array("unsigned" => true, "autoincrement" => true));
144162
$role_right->addColumn("role_id", "integer", array("unsigned" => true));
145163
$role_right->addColumn("right_id", "integer", array("unsigned" => true));
146164

0 commit comments

Comments
 (0)