Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Enable or disable Foreign Key Constraints #13333

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ public function rename($from, $to)
$this->build($blueprint);
}

/**
* Enable foreign key constraints.
*
* @return bool
*/
public function enableForeignKeyConstraints()
{
return $this->connection->statement(
$this->grammar->compileEnableForeignKeyConstraints()
);
}

/**
* Disable foreign key constraints.
*
* @return bool
*/
public function disableForeignKeyConstraints()
{
return $this->connection->statement(
$this->grammar->compileDisableForeignKeyConstraints()
);
}

/**
* Execute the blueprint to build / modify the table.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "rename table {$from} to ".$this->wrapTable($command->to);
}

/**
* Compile a enable foreign key constraints command.
*
* @return string
*/
public function compileEnableForeignKeyConstraints()
{
return 'SET FOREIGN_KEY_CHECKS=1;';
}

/**
* Compile a disable foreign key constraints command.
*
* @return string
*/
public function compileDisableForeignKeyConstraints()
{
return 'SET FOREIGN_KEY_CHECKS=0;';
}

/**
* Create the column definition for a char type.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,26 @@ public function compileDropForeign(Blueprint $blueprint, Fluent $command)
return "alter table {$table} drop constraint {$index}";
}

/**
* Compile a enable foreign key constraints command.
*
* @return string
*/
public function compileEnableForeignKeyConstraints()
{
return 'SET CONSTRAINTS ALL IMMEDIATE;';
}

/**
* Compile a disable foreign key constraints command.
*
* @return string
*/
public function compileDisableForeignKeyConstraints()
{
return 'SET CONSTRAINTS ALL DEFERRED;';
}

/**
* Compile a rename table command.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,26 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "alter table {$from} rename to ".$this->wrapTable($command->to);
}

/**
* Compile a enable foreign key constraints command.
*
* @return string
*/
public function compileEnableForeignKeyConstraints()
{
return 'PRAGMA foreign_keys = ON;';
}

/**
* Compile a disable foreign key constraints command.
*
* @return string
*/
public function compileDisableForeignKeyConstraints()
{
return 'PRAGMA foreign_keys = OFF;';
}

/**
* Create the column definition for a char type.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "sp_rename {$from}, ".$this->wrapTable($command->to);
}

/**
* Compile a enable foreign key constraints command.
*
* @return string
*/
public function compileEnableForeignKeyConstraints()
{
return 'EXEC sp_msforeachtable @command1="print \'?\'", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all";';
}

/**
* Compile a disable foreign key constraints command.
*
* @return string
*/
public function compileDisableForeignKeyConstraints()
{
return 'EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all";';
}

/**
* Create the column definition for a char type.
*
Expand Down