Skip to content

Commit 7cca9df

Browse files
committed
Update DB schema tooling to be table order agnostic
1 parent bdb1d1d commit 7cca9df

3 files changed

Lines changed: 34 additions & 18 deletions

File tree

SETUP/db_schema.sql

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Disable foreign key checks to until all tables are created
2+
SET FOREIGN_KEY_CHECKS=0;
13

24
#
35
# Table structure for table `access_log`
@@ -774,6 +776,23 @@ CREATE TABLE `user_teams` (
774776
);
775777
# --------------------------------------------------------
776778

779+
#
780+
# Table structure for table `user_teams_membership`
781+
#
782+
# Creation:
783+
# Last update:
784+
#
785+
786+
CREATE TABLE `user_teams_membership` (
787+
`u_id` int(11) unsigned NOT NULL,
788+
`t_id` int(11) unsigned NOT NULL,
789+
PRIMARY KEY (`u_id`,`t_id`),
790+
KEY (`t_id`),
791+
FOREIGN KEY (`u_id`) REFERENCES `users` (`u_id`),
792+
FOREIGN KEY (`t_id`) REFERENCES `user_teams` (`id`)
793+
);
794+
# --------------------------------------------------------
795+
777796
#
778797
# Table structure for table `users`
779798
#
@@ -813,23 +832,6 @@ CREATE TABLE `users` (
813832
);
814833
# --------------------------------------------------------
815834

816-
#
817-
# Table structure for table `user_teams_membership`
818-
#
819-
# Creation:
820-
# Last update:
821-
#
822-
823-
CREATE TABLE `user_teams_membership` (
824-
`u_id` int(11) unsigned NOT NULL,
825-
`t_id` int(11) unsigned NOT NULL,
826-
PRIMARY KEY (`u_id`,`t_id`),
827-
KEY (`t_id`),
828-
FOREIGN KEY (`u_id`) REFERENCES `users` (`u_id`),
829-
FOREIGN KEY (`t_id`) REFERENCES `user_teams` (`id`)
830-
);
831-
# --------------------------------------------------------
832-
833835
#
834836
# Table structure for table `usersettings`
835837
#
@@ -868,3 +870,6 @@ CREATE TABLE `wordcheck_events` (
868870
);
869871
# --------------------------------------------------------
870872

873+
# Re-enable foreign key checks
874+
SET FOREIGN_KEY_CHECKS=1;
875+

SETUP/dump_db_schema.template

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ get_table_names()
9696
tables=`get_table_names`
9797
9898
if true; then
99+
# The order tables are dumped is not consistent and it's possible to have
100+
# tables with foreign keys before the foreign key tables are created.
101+
# To make sure the dump is loadable, we have to temporarily disable
102+
# foreign key checks for this session.
103+
echo "SET FOREIGN_KEY_CHECKS=0;"
104+
99105
mysqldump --user=$db_user --password="$db_password" \
100106
--no-data --quote-names --add-drop-table=FALSE --force \
101107
$db_name $tables |
@@ -119,5 +125,7 @@ if true; then
119125
# Skip MySQL command sequences as they may be version-specific
120126
# (and generate a lot of noise in the diffs).
121127
grep -v '/*!'
128+
129+
# Re-enable foreign key checks
130+
echo "SET FOREIGN_KEY_CHECKS=1;"
122131
fi
123-
# vim: sw=4 ts=4 expandtab ai

SETUP/install_db.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
DPDatabase::query("CREATE DATABASE IF NOT EXISTS $db_name");
2020
DPDatabase::query("USE $db_name");
21+
DPDatabase::query("SET FOREIGN_KEY_CHECKS=0");
2122

2223
// Declare all variables
2324
$db_schema = "db_schema.sql";
@@ -50,4 +51,6 @@
5051
DPDatabase::query($lines);
5152
}
5253

54+
DPDatabase::query("SET FOREIGN_KEY_CHECKS=1");
55+
5356
echo "Tables have been created.\n";

0 commit comments

Comments
 (0)