Skip to content

Commit

Permalink
Added disableForeignKeyConstraints and enableForeignKeyConstraints me…
Browse files Browse the repository at this point in the history
…thods to make migrations with FKs easier. Fixes #1964
  • Loading branch information
lonnieezell committed Jun 7, 2019
1 parent 2f83549 commit ecfe9f4
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
24 changes: 24 additions & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,30 @@ public function getForeignKeyData(string $table)

//--------------------------------------------------------------------

/**
* Disables foreign key checks temporarily.
*/
public function disableForeignKeyChecks()
{
$sql = $this->_disableForeignKeyChecks();

return $this->query($sql);
}

//--------------------------------------------------------------------

/**
* Enables foreign key checks temporarily.
*/
public function enableForeignKeyChecks()
{
$sql = $this->_enableForeignKeyChecks();

return $this->query($sql);
}

//--------------------------------------------------------------------

/**
* Allows the engine to be set into a mode where queries are not
* actually executed, but they are still generated, timed, etc.
Expand Down
4 changes: 2 additions & 2 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ public function dropTable(string $tableName, bool $ifExists = false, bool $casca
return true;
}

$this->db->query('PRAGMA foreign_keys = OFF');
$this->db->disableForeignKeyChecks();

$query = $this->db->query($query);

$this->db->query('PRAGMA foreign_keys = ON');
$this->db->enableForeignKeyChecks();

// Update table list cache
if ($query && ! empty($this->db->dataCache['table_names']))
Expand Down
24 changes: 24 additions & 0 deletions system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,30 @@ public function _foreignKeyData(string $table): array

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to disable foreign key checks.
*
* @return string
*/
protected function _disableForeignKeyChecks()
{
return 'SET FOREIGN_KEY_CHECKS=0';
}

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to enable foreign key checks.
*
* @return string
*/
protected function _enableForeignKeyChecks()
{
return 'SET FOREIGN_KEY_CHECKS=1';
}

//--------------------------------------------------------------------

/**
* Returns the last error code and message.
*
Expand Down
24 changes: 24 additions & 0 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ public function _foreignKeyData(string $table): array

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to disable foreign key checks.
*
* @return string
*/
protected function _disableForeignKeyChecks()
{
return 'SET CONSTRAINTS ALL DEFERRED';
}

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to enable foreign key checks.
*
* @return string
*/
protected function _enableForeignKeyChecks()
{
return 'SET CONSTRAINTS ALL IMMEDIATE;';
}

//--------------------------------------------------------------------

/**
* Returns the last error code and message.
*
Expand Down
24 changes: 24 additions & 0 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ public function _foreignKeyData(string $table): array

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to disable foreign key checks.
*
* @return string
*/
protected function _disableForeignKeyChecks()
{
return 'PRAGMA foreign_keys = OFF';
}

//--------------------------------------------------------------------

/**
* Returns platform-specific SQL to enable foreign key checks.
*
* @return string
*/
protected function _enableForeignKeyChecks()
{
return 'PRAGMA foreign_keys = ON';
}

//--------------------------------------------------------------------

/**
* Returns the last error code and message.
*
Expand Down
18 changes: 18 additions & 0 deletions user_guide_src/source/dbmgmt/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ The database connection and the database Forge class are both available to you t
Alternatively, you can use a command-line call to generate a skeleton migration file. See
below for more details.

Foreign Keys
============

When your tables include Foreign Keys, migrations can often cause problems as you attempt to drop tables and columns.
To temporarily bypass the foreign key checks while running migrations, use the ``disableForeignKeyConstraints()`` and
``enableForeignKeyConstraints()`` methods on the database connection.

::

public function up()
{
$this->db->disableForeignKeyConstraints();

// Migration rules would go here...

$this->db->enableForeignKeyConstraints();
}

Using $currentVersion
=====================

Expand Down

0 comments on commit ecfe9f4

Please sign in to comment.