From 4b3520e45a4dcfa0211e38b4c661fa0133c221ad Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Thu, 2 May 2024 15:05:53 +0300 Subject: [PATCH] fix(migrations): change the order of queries for mysql (#3039) Signed-off-by: Roman Dmytrenko --- .../mysql/5_namespaces_relationships.up.sql | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/config/migrations/mysql/5_namespaces_relationships.up.sql b/config/migrations/mysql/5_namespaces_relationships.up.sql index 14ec7289f1..24c64d8d1c 100644 --- a/config/migrations/mysql/5_namespaces_relationships.up.sql +++ b/config/migrations/mysql/5_namespaces_relationships.up.sql @@ -1,3 +1,9 @@ +-- Drop previously created foreign key +ALTER TABLE constraints DROP FOREIGN KEY `constraints_ibfk_1`; +ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_1`; +ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_2`; +ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_1`; + -- Flags -- Add column namespace_key with a default value @@ -6,19 +12,18 @@ ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'defaul -- Drop previously created unique index ALTER TABLE flags DROP INDEX `key`, ADD INDEX `key` (`key`) USING BTREE; --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE flags ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; - -- Drop primary key constraint and add a new composite primary key on namespace_key and key columns ALTER TABLE flags DROP PRIMARY KEY, ADD PRIMARY KEY (`namespace_key`, `key`); +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE flags ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; + -- Variants -- Add column namespace_key with a default value ALTER TABLE variants ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; -- Drop previously created foreign key -ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_1`; -- Drop previously created unique index and add a new unique index on namespace_key, flag_key and key columns ALTER TABLE variants DROP INDEX `variants_flag_key_key`, ADD UNIQUE INDEX `variants_namespace_flag_key` (`namespace_key`, `flag_key`, `key`) USING BTREE; @@ -31,26 +36,23 @@ ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags( -- Segments --- Add column namespace_key with a default value -ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - -- Drop previously created unique index and add a new unique index on namespace_key and key columns ALTER TABLE segments DROP INDEX `key`, ADD INDEX `key` (`key`) USING BTREE; --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE segments ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; +-- Add column namespace_key with a default value +ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; -- Drop primary key constraint and add a new composite primary key on namespace_key and key columns ALTER TABLE segments DROP PRIMARY KEY, ADD PRIMARY KEY (`namespace_key`, `key`); +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE segments ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; + -- Constraints -- Add column namespace_key with a default value ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- Drop previously created foreign key -ALTER TABLE constraints DROP FOREIGN KEY `constraints_ibfk_1`; - -- Drop previously created index and add a new index on namespace_key and segment_key columns ALTER TABLE constraints DROP INDEX `segment_key`, ADD INDEX `constraints_namespace_segment_key` (`namespace_key`, `segment_key`) USING BTREE; @@ -65,10 +67,6 @@ ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES -- Add column namespace_key with a default value ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- Drop previously created foreign keys -ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_1`; -ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_2`; - -- Drop previously created index and add a new index on namespace_key, flag_key and segment_key columns ALTER TABLE rules DROP INDEX `flag_key`, ADD INDEX `rules_namespace_flag_key` (`namespace_key`, `flag_key`) USING BTREE; ALTER TABLE rules DROP INDEX `segment_key`, ADD INDEX `rules_namespace_segment_key` (`namespace_key`, `segment_key`) USING BTREE;