Browse Source

Add foreign key constraints to issue metadata

Alan Hardman 5 years ago
parent
commit
50317dcc4f
2 changed files with 31 additions and 3 deletions
  1. 24 0
      db/14.12.30.sql
  2. 7 3
      db/database.sql

+ 24 - 0
db/14.12.30.sql

@@ -0,0 +1,24 @@
+# This database update occured after commit f6f4e8de
+# This update may take a while to run on large databases!
+
+# Clean potentially messy data that could break the upgrade process
+UPDATE issue SET owner_id = NULL WHERE owner_id = 0;
+UPDATE issue SET sprint_id = NULL WHERE sprint_id = 0;
+
+# Adding foreign key constraints to issue metadata
+ALTER TABLE `issue`
+	CHANGE `status` `status` INT(10) UNSIGNED DEFAULT 1 NOT NULL;
+ALTER TABLE `issue`
+	ADD INDEX `status` (`status`);
+ALTER TABLE `issue`
+	ADD CONSTRAINT `issue_type_id` FOREIGN KEY (`type_id`) REFERENCES `issue_type`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT,
+	ADD CONSTRAINT `issue_sprint_id` FOREIGN KEY (`sprint_id`) REFERENCES `sprint`(`id`) ON UPDATE CASCADE ON DELETE SET NULL,
+	ADD CONSTRAINT `issue_owner_id` FOREIGN KEY (`owner_id`) REFERENCES `user`(`id`) ON UPDATE CASCADE ON DELETE SET NULL,
+	ADD CONSTRAINT `issue_status` FOREIGN KEY (`status`) REFERENCES `issue_status`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT;
+
+# Prevent deleting users with live comments
+ALTER TABLE `issue_comment` DROP FOREIGN KEY `comment_user`;
+ALTER TABLE `issue_comment` ADD CONSTRAINT `comment_user` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT;
+
+# Update Version
+UPDATE `config` SET `value` = '14.12.30' WHERE `attribute` = 'version';

+ 7 - 3
db/database.sql

@@ -62,7 +62,11 @@ CREATE TABLE `issue` (
 	KEY `repeat_cycle` (`repeat_cycle`),
 	KEY `due_date` (`due_date`),
 	KEY `type_id` (`type_id`),
-	KEY `parent_id` (`parent_id`)
+	KEY `parent_id` (`parent_id`),
+	CONSTRAINT `issue_type_id` FOREIGN KEY (`type_id`) REFERENCES `issue_type`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT,
+	CONSTRAINT `issue_sprint_id` FOREIGN KEY (`sprint_id`) REFERENCES `sprint`(`id`) ON UPDATE CASCADE ON DELETE SET NULL,
+	CONSTRAINT `issue_owner_id` FOREIGN KEY (`owner_id`) REFERENCES `user`(`id`) ON UPDATE CASCADE ON DELETE SET NULL,
+	CONSTRAINT `issue_status` FOREIGN KEY (`status`) REFERENCES `issue_status`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 DROP TABLE IF EXISTS `issue_comment`;
@@ -77,7 +81,7 @@ CREATE TABLE `issue_comment` (
 	KEY `issue_id` (`issue_id`),
 	KEY `user` (`user_id`),
 	CONSTRAINT `comment_issue` FOREIGN KEY (`issue_id`) REFERENCES `issue` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
-	CONSTRAINT `comment_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
+	CONSTRAINT `comment_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 DROP TABLE IF EXISTS `issue_file`;
@@ -261,4 +265,4 @@ CREATE TABLE `config` (
 	UNIQUE KEY `attribute` (`attribute`)
 ) ;
 
-INSERT INTO `config` (`attribute`, `value`) VALUES ('version', '14.12.29');
+INSERT INTO `config` (`attribute`, `value`) VALUES ('version', '14.12.30');