-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add/remove foreign keys * Add option to disable foreign key checks * Add foreign key tests * Don't drop keys that don't exist
- Loading branch information
1 parent
c341cad
commit 96b0295
Showing
5 changed files
with
172 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
-- test - Add unnamed foreign key | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
ALTER TABLE `t` | ||
ADD KEY `ux` (`ux`), | ||
ADD CONSTRAINT `t_ibfk_1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
|
||
-- test - Add named foreign key | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
ALTER TABLE `t` | ||
ADD KEY `forKey1` (`ux`), | ||
ADD CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
|
||
-- test - Key automatically gets added for foreign key as per MySQL | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
ALTER TABLE `t` | ||
ADD KEY `forKey1` (`ux`) | ||
|
||
-- test - Automatically added key already exists in the current table so no change is required | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
KEY `forKey1` (`ux`), | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
|
||
-- test - Drop unnamed foreign key | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL | ||
); | ||
ALTER TABLE `t` | ||
DROP FOREIGN KEY `t_ibfk_1` | ||
|
||
-- test - Drop named foreign key | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `forKey1` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL | ||
); | ||
ALTER TABLE `t` | ||
DROP FOREIGN KEY `forKey1` | ||
|
||
-- test - Rename foreign key | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `foo` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
create table t ( | ||
`ux` int(11) DEFAULT NULL, | ||
CONSTRAINT `bar` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) | ||
); | ||
ALTER TABLE `t` | ||
ADD KEY `bar` (`ux`), | ||
DROP FOREIGN KEY `foo`, | ||
ADD CONSTRAINT `bar` FOREIGN KEY (`ux`) REFERENCES `u` (`x`) |