Skip to content

Commit ad5f19f

Browse files
committed
Update getForeignKeys to public and add is_deferrable/initially_deferred for PSQL
1 parent a7563b2 commit ad5f19f

File tree

8 files changed

+104
-19
lines changed

8 files changed

+104
-19
lines changed

src/Phinx/Db/Adapter/AdapterInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,14 @@ public function hasPrimaryKey(string $tableName, string|array $columns, ?string
461461
*/
462462
public function hasForeignKey(string $tableName, string|array $columns, ?string $constraint = null): bool;
463463

464+
/**
465+
* Get an array of foreign keys from a particular table.
466+
*
467+
* @param string $tableName Table name
468+
* @return array
469+
*/
470+
public function getForeignKeys(string $tableName): array;
471+
464472
/**
465473
* Returns an array of the supported Phinx column types.
466474
*

src/Phinx/Db/Adapter/AdapterWrapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
402402
return $this->getAdapter()->hasForeignKey($tableName, $columns, $constraint);
403403
}
404404

405+
/**
406+
* @inheritDoc
407+
*/
408+
public function getForeignKeys(string $tableName): array
409+
{
410+
return $this->getAdapter()->getForeignKeys($tableName);
411+
}
412+
405413
/**
406414
* @inheritDoc
407415
*/

src/Phinx/Db/Adapter/MysqlAdapter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -849,12 +849,9 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
849849
}
850850

851851
/**
852-
* Get an array of foreign keys from a particular table.
853-
*
854-
* @param string $tableName Table name
855-
* @return array
852+
* @inheritDoc
856853
*/
857-
protected function getForeignKeys(string $tableName): array
854+
public function getForeignKeys(string $tableName): array
858855
{
859856
if (strpos($tableName, '.') !== false) {
860857
[$schema, $tableName] = explode('.', $tableName);

src/Phinx/Db/Adapter/PostgresAdapter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,14 +960,17 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
960960
* @param string $tableName Table name
961961
* @return array
962962
*/
963-
protected function getForeignKeys(string $tableName): array
963+
public function getForeignKeys(string $tableName): array
964964
{
965965
$parts = $this->getSchemaName($tableName);
966966
$foreignKeys = [];
967967
$rows = $this->fetchAll(sprintf(
968968
"SELECT
969969
tc.constraint_name,
970-
tc.table_name, kcu.column_name,
970+
tc.table_name,
971+
tc.is_deferrable,
972+
tc.initially_deferred,
973+
kcu.column_name,
971974
ccu.table_name AS referenced_table_name,
972975
ccu.column_name AS referenced_column_name
973976
FROM
@@ -981,6 +984,8 @@ protected function getForeignKeys(string $tableName): array
981984
));
982985
foreach ($rows as $row) {
983986
$foreignKeys[$row['constraint_name']]['table'] = $row['table_name'];
987+
$foreignKeys[$row['constraint_name']]['is_deferrable'] = $row['is_deferrable'] === 'YES';
988+
$foreignKeys[$row['constraint_name']]['initially_deferred'] = $row['initially_deferred'] === 'YES';
984989
$foreignKeys[$row['constraint_name']]['columns'][] = $row['column_name'];
985990
$foreignKeys[$row['constraint_name']]['referenced_table'] = $row['referenced_table_name'];
986991
$foreignKeys[$row['constraint_name']]['referenced_columns'][] = $row['referenced_column_name'];

src/Phinx/Db/Adapter/SQLiteAdapter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,12 +1558,9 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
15581558
}
15591559

15601560
/**
1561-
* Get an array of foreign keys from a particular table.
1562-
*
1563-
* @param string $tableName Table name
1564-
* @return array
1561+
* @inheritDoc
15651562
*/
1566-
protected function getForeignKeys(string $tableName): array
1563+
public function getForeignKeys(string $tableName): array
15671564
{
15681565
$foreignKeys = [];
15691566

src/Phinx/Db/Adapter/SqlServerAdapter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -957,12 +957,9 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
957957
}
958958

959959
/**
960-
* Get an array of foreign keys from a particular table.
961-
*
962-
* @param string $tableName Table name
963-
* @return array
960+
* @inheritDoc
964961
*/
965-
protected function getForeignKeys(string $tableName): array
962+
public function getForeignKeys(string $tableName): array
966963
{
967964
$foreignKeys = [];
968965
$rows = $this->fetchAll(sprintf(

src/Phinx/Db/Adapter/TablePrefixAdapter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ public function hasForeignKey(string $tableName, $columns, ?string $constraint =
320320
return parent::hasForeignKey($adapterTableName, $columns, $constraint);
321321
}
322322

323+
/**
324+
* @inheritDoc
325+
*/
326+
public function getForeignKeys(string $tableName): array
327+
{
328+
$adapterTableName = $this->getAdapterTableName($tableName);
329+
330+
return parent::getForeignKeys($adapterTableName);
331+
}
332+
323333
/**
324334
* {@inheritDoc}
325335
*

tests/Phinx/Db/Adapter/PostgresAdapterTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,8 @@ public function testAddForeignKeyDeferrable()
16201620
$table = new Table('table', [], $this->adapter);
16211621
$table
16221622
->addColumn('ref_table_id', 'integer')
1623-
->addForeignKey(
1623+
->addForeignKeyWithName(
1624+
'my_constraint',
16241625
['ref_table_id'],
16251626
'ref_table',
16261627
['id'],
@@ -1630,7 +1631,69 @@ public function testAddForeignKeyDeferrable()
16301631
)
16311632
->save();
16321633

1633-
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id']));
1634+
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id'], 'my_constraint'));
1635+
1636+
$foreignKeys = $this->adapter->getForeignKeys($table->getName());
1637+
1638+
$this->assertTrue(isset($foreignKeys['my_constraint']));
1639+
$this->assertTrue($foreignKeys['my_constraint']['is_deferrable']);
1640+
$this->assertTrue($foreignKeys['my_constraint']['initially_deferred']);
1641+
}
1642+
1643+
public function testAddForeignKeyNotDeferrable()
1644+
{
1645+
$refTable = new Table('ref_table', [], $this->adapter);
1646+
$refTable->addColumn('field1', 'string')->save();
1647+
1648+
$table = new Table('table', [], $this->adapter);
1649+
$table
1650+
->addColumn('ref_table_id', 'integer')
1651+
->addForeignKeyWithName(
1652+
'my_constraint',
1653+
['ref_table_id'],
1654+
'ref_table',
1655+
['id'],
1656+
[
1657+
'deferrable' => 'IMMEDIATE',
1658+
]
1659+
)
1660+
->save();
1661+
1662+
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id'], 'my_constraint'));
1663+
1664+
$foreignKeys = $this->adapter->getForeignKeys($table->getName());
1665+
1666+
$this->assertTrue(isset($foreignKeys['my_constraint']));
1667+
$this->assertTrue($foreignKeys['my_constraint']['is_deferrable']);
1668+
$this->assertFalse($foreignKeys['my_constraint']['initially_deferred']);
1669+
}
1670+
1671+
public function testAddForeignKeyImmediate()
1672+
{
1673+
$refTable = new Table('ref_table', [], $this->adapter);
1674+
$refTable->addColumn('field1', 'string')->save();
1675+
1676+
$table = new Table('table', [], $this->adapter);
1677+
$table
1678+
->addColumn('ref_table_id', 'integer')
1679+
->addForeignKeyWithName(
1680+
'my_constraint',
1681+
['ref_table_id'],
1682+
'ref_table',
1683+
['id'],
1684+
[
1685+
'deferrable' => 'NOT_DEFERRED',
1686+
]
1687+
)
1688+
->save();
1689+
1690+
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), ['ref_table_id'], 'my_constraint'));
1691+
1692+
$foreignKeys = $this->adapter->getForeignKeys($table->getName());
1693+
1694+
$this->assertTrue(isset($foreignKeys['my_constraint']));
1695+
$this->assertFalse($foreignKeys['my_constraint']['is_deferrable']);
1696+
$this->assertFalse($foreignKeys['my_constraint']['initially_deferred']);
16341697
}
16351698

16361699
public function testAddForeignKeyWithSchema()

0 commit comments

Comments
 (0)