From d1219ae8e2c137f17f3d2e64d018718757892b9b Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Mon, 13 Mar 2023 14:42:57 -0400 Subject: [PATCH 1/6] chore: wip namespaces down migrations --- .../sqlite3/7_namespaces_flags.down.sql | 66 +++++++++++++++++- .../sqlite3/8_namespaces_segments.down.sql | 67 ++++++++++++++++++- 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/config/migrations/sqlite3/7_namespaces_flags.down.sql b/config/migrations/sqlite3/7_namespaces_flags.down.sql index 48e70a7ed5..84d48001c0 100644 --- a/config/migrations/sqlite3/7_namespaces_flags.down.sql +++ b/config/migrations/sqlite3/7_namespaces_flags.down.sql @@ -1 +1,65 @@ -/* TODO */ \ No newline at end of file +PRAGMA foreign_keys=off; + +-- Create temp tables + +-- Variants +CREATE TABLE IF NOT EXISTS variants_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + attachment TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + UNIQUE (flag_key, key), + FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE +); + +INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) + SELECT id, flag_key, key, name, description, attachment, created_at, updated_at + FROM variants; + +DROP TABLE variants; + +ALTER TABLE variants_temp RENAME TO variants; + +-- Rules +CREATE TABLE IF NOT EXISTS rules_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + segment_key VARCHAR(255) NOT NULL, + rank INTEGER DEFAULT 1 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE +); + +INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) + SELECT id, flag_key, segment_key, rank, created_at, updated_at + FROM rules; + +DROP TABLE rules; + +ALTER TABLE rules_temp RENAME TO rules; + +-- Flags +CREATE TABLE IF NOT EXISTS flags_temp ( + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + enabled BOOLEAN DEFAULT FALSE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + PRIMARY KEY (key) +); + +INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) + SELECT key, name, description, enabled, created_at, updated_at + FROM flags; + +DROP TABLE flags; + +ALTER TABLE flags_temp RENAME TO flags; + +PRAGMA foreign_keys=on; diff --git a/config/migrations/sqlite3/8_namespaces_segments.down.sql b/config/migrations/sqlite3/8_namespaces_segments.down.sql index 48e70a7ed5..80ecfbb2d2 100644 --- a/config/migrations/sqlite3/8_namespaces_segments.down.sql +++ b/config/migrations/sqlite3/8_namespaces_segments.down.sql @@ -1 +1,66 @@ -/* TODO */ \ No newline at end of file +PRAGMA foreign_keys=off; + +-- Create temp tables + +-- Constraints +CREATE TABLE IF NOT EXISTS constraints_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + segment_key VARCHAR(255) NOT NULL, + type INTEGER DEFAULT 0 NOT NULL, + property VARCHAR(255) NOT NULL, + operator VARCHAR(255) NOT NULL, + value TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (segment_key) REFERENCES segments (key) ON DELETE CASCADE +); + +INSERT INTO constraints_temp (id, segment_key, type, property, operator, value, created_at, updated_at) + SELECT id, segment_key, type, property, operator, value, created_at, updated_at + FROM constraints; + +DROP TABLE constraints; + +ALTER TABLE constraints_temp RENAME TO constraints; + + +-- Rules +CREATE TABLE IF NOT EXISTS rules_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + segment_key VARCHAR(255) NOT NULL, + rank INTEGER DEFAULT 1 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE, + FOREIGN KEY (segment_key) REFERENCES segments (key) ON DELETE CASCADE +); + +INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) + SELECT id, flag_key, segment_key, rank, created_at, updated_at + FROM rules; + +DROP TABLE rules; + +ALTER TABLE rules_temp RENAME TO rules; + +-- Segments +CREATE TABLE IF NOT EXISTS segments_temp ( + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + match_type INTEGER DEFAULT 0 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + PRIMARY KEY (key) +); + +INSERT INTO segments_temp (key, name, description, match_type, created_at, updated_at) + SELECT key, name, description, match_type, created_at, updated_at + FROM segments; + +DROP TABLE segments; + +ALTER TABLE segments_temp RENAME TO segments; + +PRAGMA foreign_keys=on; From d2f09bb2384f467122a7727073ce4c29adc6afa6 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:28:41 -0400 Subject: [PATCH 2/6] chore: fix sqlite migrations --- .../mysql/5_namespaces_flags.down.sql | 17 +++- .../sqlite3/6_create_namespaces.up.sql | 2 - .../sqlite3/7_namespaces_flags.down.sql | 65 ------------- .../sqlite3/7_namespaces_flags.up.sql | 68 -------------- ...ql => 7_namespaces_relationships.down.sql} | 94 ++++++++++++++----- ....sql => 7_namespaces_relationships.up.sql} | 64 +++++++++++-- internal/storage/sql/migrator.go | 2 +- 7 files changed, 142 insertions(+), 170 deletions(-) delete mode 100644 config/migrations/sqlite3/7_namespaces_flags.down.sql delete mode 100644 config/migrations/sqlite3/7_namespaces_flags.up.sql rename config/migrations/sqlite3/{8_namespaces_segments.down.sql => 7_namespaces_relationships.down.sql} (53%) rename config/migrations/sqlite3/{8_namespaces_segments.up.sql => 7_namespaces_relationships.up.sql} (52%) diff --git a/config/migrations/mysql/5_namespaces_flags.down.sql b/config/migrations/mysql/5_namespaces_flags.down.sql index 48e70a7ed5..0b8422d769 100644 --- a/config/migrations/mysql/5_namespaces_flags.down.sql +++ b/config/migrations/mysql/5_namespaces_flags.down.sql @@ -1 +1,16 @@ -/* TODO */ \ No newline at end of file + -- drop previously created foreign keys +ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_1`; +ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_2`; + +-- drop previously created unique index +ALTER TABLE variants DROP INDEX `variants_namespace_flag_key`, ADD UNIQUE INDEX `variants_flag_key_key` (`flag_key`, `key`) USING BTREE; +ALTER TABLE variants DROP COLUMN namespace_key; + +-- drop foreign key to namespaces +ALTER TABLE flags DROP FOREIGN KEY `flags_ibfk_1`; +-- drop previously created index +ALTER TABLE flags DROP INDEX `key`; +-- drop namespaces, key primary key +ALTER TABLE flags DROP PRIMARY KEY, ADD PRIMARY KEY (`key`); +ALTER TABLE flags DROP COLUMN namespace_key; + diff --git a/config/migrations/sqlite3/6_create_namespaces.up.sql b/config/migrations/sqlite3/6_create_namespaces.up.sql index f63b307aff..ace1dd243e 100644 --- a/config/migrations/sqlite3/6_create_namespaces.up.sql +++ b/config/migrations/sqlite3/6_create_namespaces.up.sql @@ -1,4 +1,3 @@ --- Create namespaces table CREATE TABLE IF NOT EXISTS namespaces ( key VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, @@ -8,5 +7,4 @@ CREATE TABLE IF NOT EXISTS namespaces ( updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL ); --- Create default namespace INSERT INTO namespaces (key, name, description, protected) VALUES ('default', 'Default', 'Default namespace', true); \ No newline at end of file diff --git a/config/migrations/sqlite3/7_namespaces_flags.down.sql b/config/migrations/sqlite3/7_namespaces_flags.down.sql deleted file mode 100644 index 84d48001c0..0000000000 --- a/config/migrations/sqlite3/7_namespaces_flags.down.sql +++ /dev/null @@ -1,65 +0,0 @@ -PRAGMA foreign_keys=off; - --- Create temp tables - --- Variants -CREATE TABLE IF NOT EXISTS variants_temp ( - id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, - flag_key VARCHAR(255) NOT NULL, - key VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - attachment TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - UNIQUE (flag_key, key), - FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE -); - -INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) - SELECT id, flag_key, key, name, description, attachment, created_at, updated_at - FROM variants; - -DROP TABLE variants; - -ALTER TABLE variants_temp RENAME TO variants; - --- Rules -CREATE TABLE IF NOT EXISTS rules_temp ( - id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, - flag_key VARCHAR(255) NOT NULL, - segment_key VARCHAR(255) NOT NULL, - rank INTEGER DEFAULT 1 NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE -); - -INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) - SELECT id, flag_key, segment_key, rank, created_at, updated_at - FROM rules; - -DROP TABLE rules; - -ALTER TABLE rules_temp RENAME TO rules; - --- Flags -CREATE TABLE IF NOT EXISTS flags_temp ( - key VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - enabled BOOLEAN DEFAULT FALSE NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (key) -); - -INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) - SELECT key, name, description, enabled, created_at, updated_at - FROM flags; - -DROP TABLE flags; - -ALTER TABLE flags_temp RENAME TO flags; - -PRAGMA foreign_keys=on; diff --git a/config/migrations/sqlite3/7_namespaces_flags.up.sql b/config/migrations/sqlite3/7_namespaces_flags.up.sql deleted file mode 100644 index cde0aeaf5e..0000000000 --- a/config/migrations/sqlite3/7_namespaces_flags.up.sql +++ /dev/null @@ -1,68 +0,0 @@ -PRAGMA foreign_keys=off; - --- Create temp tables - --- Flags -CREATE TABLE IF NOT EXISTS flags_temp ( - key VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - enabled BOOLEAN DEFAULT FALSE NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE, - PRIMARY KEY (namespace_key, key) -); - -INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) - SELECT key, name, description, enabled, created_at, updated_at - FROM flags; - -DROP TABLE flags; - -ALTER TABLE flags_temp RENAME TO flags; - --- Variants -CREATE TABLE IF NOT EXISTS variants_temp ( - id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, - flag_key VARCHAR(255) NOT NULL, - key VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - attachment TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE, - UNIQUE (namespace_key, flag_key, key), - FOREIGN KEY (namespace_key, flag_key) REFERENCES flags (namespace_key, key) ON DELETE CASCADE -); - -INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) - SELECT id, flag_key, key, name, description, attachment, created_at, updated_at - FROM variants; - -DROP TABLE variants; - -ALTER TABLE variants_temp RENAME TO variants; - --- Rules -CREATE TABLE IF NOT EXISTS rules_temp ( - id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, - flag_key VARCHAR(255) NOT NULL, - segment_key VARCHAR(255) NOT NULL, - rank INTEGER DEFAULT 1 NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE, - FOREIGN KEY (namespace_key, flag_key) REFERENCES flags (namespace_key, key) ON DELETE CASCADE -); - -INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) - SELECT id, flag_key, segment_key, rank, created_at, updated_at - FROM rules; - -DROP TABLE rules; - -ALTER TABLE rules_temp RENAME TO rules; - -PRAGMA foreign_keys=on; \ No newline at end of file diff --git a/config/migrations/sqlite3/8_namespaces_segments.down.sql b/config/migrations/sqlite3/7_namespaces_relationships.down.sql similarity index 53% rename from config/migrations/sqlite3/8_namespaces_segments.down.sql rename to config/migrations/sqlite3/7_namespaces_relationships.down.sql index 80ecfbb2d2..0f7e981ffd 100644 --- a/config/migrations/sqlite3/8_namespaces_segments.down.sql +++ b/config/migrations/sqlite3/7_namespaces_relationships.down.sql @@ -1,8 +1,75 @@ PRAGMA foreign_keys=off; --- Create temp tables +CREATE TABLE IF NOT EXISTS rules_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + segment_key VARCHAR(255) NOT NULL, + rank INTEGER DEFAULT 1 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL +); + +INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) + SELECT id, flag_key, segment_key, rank, created_at, updated_at + FROM rules; + +DROP TABLE rules; +ALTER TABLE rules_temp RENAME TO rules; + +CREATE TABLE IF NOT EXISTS flags_temp ( + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + enabled BOOLEAN DEFAULT FALSE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + PRIMARY KEY (key) +); + +INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) + SELECT key, name, description, enabled, created_at, updated_at + FROM flags; + +DROP TABLE flags; +ALTER TABLE flags_temp RENAME TO flags; + +CREATE TABLE IF NOT EXISTS variants_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + attachment TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + UNIQUE (flag_key, key), + FOREIGN KEY (flag_key) REFERENCES flags (key) ON DELETE CASCADE +); + +INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) + SELECT id, flag_key, key, name, description, attachment, created_at, updated_at + FROM variants; + +DROP TABLE variants; +ALTER TABLE variants_temp RENAME TO variants; + +CREATE TABLE IF NOT EXISTS segments_temp ( + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + match_type INTEGER DEFAULT 0 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + PRIMARY KEY (key) +); + +INSERT INTO segments_temp (key, name, description, match_type, created_at, updated_at) + SELECT key, name, description, match_type, created_at, updated_at + FROM segments; + +DROP TABLE segments; +ALTER TABLE segments_temp RENAME TO segments; --- Constraints CREATE TABLE IF NOT EXISTS constraints_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, segment_key VARCHAR(255) NOT NULL, @@ -20,11 +87,8 @@ INSERT INTO constraints_temp (id, segment_key, type, property, operator, value, FROM constraints; DROP TABLE constraints; - ALTER TABLE constraints_temp RENAME TO constraints; - --- Rules CREATE TABLE IF NOT EXISTS rules_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, flag_key VARCHAR(255) NOT NULL, @@ -41,26 +105,6 @@ INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) FROM rules; DROP TABLE rules; - ALTER TABLE rules_temp RENAME TO rules; --- Segments -CREATE TABLE IF NOT EXISTS segments_temp ( - key VARCHAR(255) NOT NULL, - name VARCHAR(255) NOT NULL, - description TEXT NOT NULL, - match_type INTEGER DEFAULT 0 NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, - PRIMARY KEY (key) -); - -INSERT INTO segments_temp (key, name, description, match_type, created_at, updated_at) - SELECT key, name, description, match_type, created_at, updated_at - FROM segments; - -DROP TABLE segments; - -ALTER TABLE segments_temp RENAME TO segments; - PRAGMA foreign_keys=on; diff --git a/config/migrations/sqlite3/8_namespaces_segments.up.sql b/config/migrations/sqlite3/7_namespaces_relationships.up.sql similarity index 52% rename from config/migrations/sqlite3/8_namespaces_segments.up.sql rename to config/migrations/sqlite3/7_namespaces_relationships.up.sql index b97bf6770a..1274fe74aa 100644 --- a/config/migrations/sqlite3/8_namespaces_segments.up.sql +++ b/config/migrations/sqlite3/7_namespaces_relationships.up.sql @@ -1,8 +1,61 @@ PRAGMA foreign_keys=off; --- Create temp tables +CREATE TABLE IF NOT EXISTS rules_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + segment_key VARCHAR(255) NOT NULL, + rank INTEGER DEFAULT 1 NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE +); + +INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) + SELECT id, flag_key, segment_key, rank, created_at, updated_at + FROM rules; + +DROP TABLE rules; +ALTER TABLE rules_temp RENAME TO rules; + +CREATE TABLE IF NOT EXISTS flags_temp ( + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + enabled BOOLEAN DEFAULT FALSE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE, + PRIMARY KEY (namespace_key, key) +); + +INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) + SELECT key, name, description, enabled, created_at, updated_at + FROM flags; + +DROP TABLE flags; +ALTER TABLE flags_temp RENAME TO flags; + +CREATE TABLE IF NOT EXISTS variants_temp ( + id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, + flag_key VARCHAR(255) NOT NULL, + key VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + description TEXT NOT NULL, + attachment TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, + namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE, + UNIQUE (namespace_key, flag_key, key), + FOREIGN KEY (namespace_key, flag_key) REFERENCES flags (namespace_key, key) ON DELETE CASCADE +); + +INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) + SELECT id, flag_key, key, name, description, attachment, created_at, updated_at + FROM variants; + +DROP TABLE variants; +ALTER TABLE variants_temp RENAME TO variants; --- Segments CREATE TABLE IF NOT EXISTS segments_temp ( key VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, @@ -19,10 +72,8 @@ INSERT INTO segments_temp (key, name, description, match_type, created_at, updat FROM segments; DROP TABLE segments; - ALTER TABLE segments_temp RENAME TO segments; --- Constraints CREATE TABLE IF NOT EXISTS constraints_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, segment_key VARCHAR(255) NOT NULL, @@ -41,10 +92,8 @@ INSERT INTO constraints_temp (id, segment_key, type, property, operator, value, FROM constraints; DROP TABLE constraints; - ALTER TABLE constraints_temp RENAME TO constraints; --- Rules CREATE TABLE IF NOT EXISTS rules_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, flag_key VARCHAR(255) NOT NULL, @@ -62,7 +111,6 @@ INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) FROM rules; DROP TABLE rules; - ALTER TABLE rules_temp RENAME TO rules; -PRAGMA foreign_keys=on; \ No newline at end of file +PRAGMA foreign_keys=on; diff --git a/internal/storage/sql/migrator.go b/internal/storage/sql/migrator.go index b09a28f95f..52cfe04665 100644 --- a/internal/storage/sql/migrator.go +++ b/internal/storage/sql/migrator.go @@ -17,7 +17,7 @@ import ( ) var expectedVersions = map[Driver]uint{ - SQLite: 8, + SQLite: 7, Postgres: 9, MySQL: 7, CockroachDB: 6, From 64c11bdbfef82fe94d897920c85fd9f707b1786c Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Tue, 14 Mar 2023 10:20:32 -0400 Subject: [PATCH 3/6] chore: fix postgres down migrations --- .../1_variants_unique_per_flag.down.sql | 2 +- .../1_variants_unique_per_flag.up.sql | 2 +- .../postgres/6_create_namespaces.down.sql | 2 +- .../postgres/7_namespaces_flags.down.sql | 1 - .../postgres/7_namespaces_flags.up.sql | 15 ---- .../7_namespaces_relationships.down.sql | 71 +++++++++++++++++++ .../7_namespaces_relationships.up.sql | 40 +++++++++++ .../postgres/8_namespaces_segments.down.sql | 1 - .../postgres/8_namespaces_segments.up.sql | 18 ----- .../postgres/9_namespaces_rules.down.sql | 1 - .../postgres/9_namespaces_rules.up.sql | 7 -- .../sqlite3/6_create_namespaces.up.sql | 2 + .../sqlite3/7_namespaces_relationships.up.sql | 24 +++++++ internal/storage/sql/migrator.go | 4 +- 14 files changed, 141 insertions(+), 49 deletions(-) delete mode 100644 config/migrations/postgres/7_namespaces_flags.down.sql delete mode 100644 config/migrations/postgres/7_namespaces_flags.up.sql create mode 100644 config/migrations/postgres/7_namespaces_relationships.down.sql create mode 100644 config/migrations/postgres/7_namespaces_relationships.up.sql delete mode 100644 config/migrations/postgres/8_namespaces_segments.down.sql delete mode 100644 config/migrations/postgres/8_namespaces_segments.up.sql delete mode 100644 config/migrations/postgres/9_namespaces_rules.down.sql delete mode 100644 config/migrations/postgres/9_namespaces_rules.up.sql diff --git a/config/migrations/postgres/1_variants_unique_per_flag.down.sql b/config/migrations/postgres/1_variants_unique_per_flag.down.sql index d8154bb23c..3b749b4fd3 100644 --- a/config/migrations/postgres/1_variants_unique_per_flag.down.sql +++ b/config/migrations/postgres/1_variants_unique_per_flag.down.sql @@ -1,2 +1,2 @@ -ALTER TABLE variants DROP CONSTRAINT variants_flag_key_key_key; +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_flag_key_key_key; ALTER TABLE variants ADD UNIQUE(key); diff --git a/config/migrations/postgres/1_variants_unique_per_flag.up.sql b/config/migrations/postgres/1_variants_unique_per_flag.up.sql index a768047e63..b3e8b7ac07 100644 --- a/config/migrations/postgres/1_variants_unique_per_flag.up.sql +++ b/config/migrations/postgres/1_variants_unique_per_flag.up.sql @@ -1,2 +1,2 @@ -ALTER TABLE variants DROP CONSTRAINT variants_key_key; +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_key_key; ALTER TABLE variants ADD UNIQUE(flag_key, key); diff --git a/config/migrations/postgres/6_create_namespaces.down.sql b/config/migrations/postgres/6_create_namespaces.down.sql index 48e70a7ed5..24fdbc33ce 100644 --- a/config/migrations/postgres/6_create_namespaces.down.sql +++ b/config/migrations/postgres/6_create_namespaces.down.sql @@ -1 +1 @@ -/* TODO */ \ No newline at end of file +DROP TABLE IF EXISTS namespaces CASCADE; \ No newline at end of file diff --git a/config/migrations/postgres/7_namespaces_flags.down.sql b/config/migrations/postgres/7_namespaces_flags.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/postgres/7_namespaces_flags.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/postgres/7_namespaces_flags.up.sql b/config/migrations/postgres/7_namespaces_flags.up.sql deleted file mode 100644 index 006970e3b3..0000000000 --- a/config/migrations/postgres/7_namespaces_flags.up.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- 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 CONSTRAINT IF EXISTS flags_pkey CASCADE; -ALTER TABLE flags ADD CONSTRAINT flags_pkey PRIMARY KEY (namespace_key, key); - -ALTER TABLE variants ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; -ALTER TABLE variants DROP CONSTRAINT "variants_flag_key_key_key"; -ALTER TABLE variants ADD CONSTRAINT "variants_namespace_flag_key" UNIQUE (namespace_key, flag_key, key); -ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/postgres/7_namespaces_relationships.down.sql b/config/migrations/postgres/7_namespaces_relationships.down.sql new file mode 100644 index 0000000000..796dd0351d --- /dev/null +++ b/config/migrations/postgres/7_namespaces_relationships.down.sql @@ -0,0 +1,71 @@ +-- Flags +---------------- + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_namespace_key_fkey CASCADE; + +-- Drop primary key constraint and add a new composite primary key on key column +ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_pkey CASCADE; +ALTER TABLE flags ADD CONSTRAINT flags_pkey PRIMARY KEY (key); + +-- Drop column namespace_key +ALTER TABLE flags DROP COLUMN IF EXISTS namespace_key; + +-- Variants +---------------- + +-- Drop foreign key constraints on namespace_key and flag_key columns +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_namespace_key_flag_key_fkey CASCADE; +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_namespace_key_fkey CASCADE; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE variants ADD FOREIGN KEY (flag_key) REFERENCES flags(key) ON DELETE CASCADE; + +-- Drop unique constraint created by previous migration and add a new unique constraint on flag_key and key columns +ALTER TABLE variants ADD CONSTRAINT variants_flag_key UNIQUE (flag_key, key); + +-- Drop column namespace_key +ALTER TABLE variants DROP COLUMN IF EXISTS namespace_key; + +-- Segments +---------------- + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_namespace_key_fkey CASCADE; + +-- Drop primary key constraint and add a new primary key constraint on key column +ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_pkey CASCADE; +ALTER TABLE segments ADD PRIMARY KEY (key); + +-- Drop column namespace_key +ALTER TABLE segments DROP COLUMN IF EXISTS namespace_key; + +-- Constraints +---------------- + +-- Drop foreign key constraints on namespace_key and segment_key columns +ALTER TABLE constraints DROP CONSTRAINT IF EXISTS constraints_namespace_key_segment_key_fkey CASCADE; +ALTER TABLE constraints DROP CONSTRAINT IF EXISTS constraints_namespace_key_fkey CASCADE; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE constraints ADD FOREIGN KEY (segment_key) REFERENCES segments(key) ON DELETE CASCADE; + +-- Drop column namespace_key +ALTER TABLE constraints DROP COLUMN IF EXISTS namespace_key; + +-- Rules +---------------- + +-- Drop foreign key constraints on namespace_key, flag_key and segment_key columns +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_fkey CASCADE; +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_flag_key_fkey CASCADE; +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_segment_key_fkey CASCADE; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE rules ADD FOREIGN KEY (flag_key) REFERENCES flags(key) ON DELETE CASCADE; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE rules ADD FOREIGN KEY (segment_key) REFERENCES segments(key) ON DELETE CASCADE; + +-- Drop column namespace_key +ALTER TABLE rules DROP COLUMN IF EXISTS namespace_key; diff --git a/config/migrations/postgres/7_namespaces_relationships.up.sql b/config/migrations/postgres/7_namespaces_relationships.up.sql new file mode 100644 index 0000000000..6313990dd9 --- /dev/null +++ b/config/migrations/postgres/7_namespaces_relationships.up.sql @@ -0,0 +1,40 @@ +-- Add column namespace_key with a default value +ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- 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 CONSTRAINT IF EXISTS flags_pkey CASCADE; +ALTER TABLE flags ADD CONSTRAINT flags_pkey PRIMARY KEY (namespace_key, key); + +ALTER TABLE variants ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_flag_key_key_key CASCADE; +ALTER TABLE variants ADD CONSTRAINT variants_namespace_flag_key UNIQUE (namespace_key, flag_key, key); +ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; +ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, 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'; + +-- 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; + +-- Drop primary key constraint and add a new composite primary key on namespace_key and key columns +ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_pkey CASCADE; +ALTER TABLE segments ADD CONSTRAINT segments_pkey PRIMARY KEY (namespace_key, key); + +-- Add column namespace_key with a default value +ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; + +-- Add column namespace_key with a default value +ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/postgres/8_namespaces_segments.down.sql b/config/migrations/postgres/8_namespaces_segments.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/postgres/8_namespaces_segments.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/postgres/8_namespaces_segments.up.sql b/config/migrations/postgres/8_namespaces_segments.up.sql deleted file mode 100644 index 0f2760bfe9..0000000000 --- a/config/migrations/postgres/8_namespaces_segments.up.sql +++ /dev/null @@ -1,18 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- 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; - --- Drop primary key constraint and add a new composite primary key on namespace_key and key columns -ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_pkey CASCADE; -ALTER TABLE segments ADD CONSTRAINT segments_pkey PRIMARY KEY (namespace_key, key); - --- Add column namespace_key with a default value -ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; - --- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/postgres/9_namespaces_rules.down.sql b/config/migrations/postgres/9_namespaces_rules.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/postgres/9_namespaces_rules.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/postgres/9_namespaces_rules.up.sql b/config/migrations/postgres/9_namespaces_rules.up.sql deleted file mode 100644 index 106797edb9..0000000000 --- a/config/migrations/postgres/9_namespaces_rules.up.sql +++ /dev/null @@ -1,7 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/sqlite3/6_create_namespaces.up.sql b/config/migrations/sqlite3/6_create_namespaces.up.sql index ace1dd243e..f63b307aff 100644 --- a/config/migrations/sqlite3/6_create_namespaces.up.sql +++ b/config/migrations/sqlite3/6_create_namespaces.up.sql @@ -1,3 +1,4 @@ +-- Create namespaces table CREATE TABLE IF NOT EXISTS namespaces ( key VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, name VARCHAR(255) NOT NULL, @@ -7,4 +8,5 @@ CREATE TABLE IF NOT EXISTS namespaces ( updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL ); +-- Create default namespace INSERT INTO namespaces (key, name, description, protected) VALUES ('default', 'Default', 'Default namespace', true); \ No newline at end of file diff --git a/config/migrations/sqlite3/7_namespaces_relationships.up.sql b/config/migrations/sqlite3/7_namespaces_relationships.up.sql index 1274fe74aa..07c7478275 100644 --- a/config/migrations/sqlite3/7_namespaces_relationships.up.sql +++ b/config/migrations/sqlite3/7_namespaces_relationships.up.sql @@ -1,5 +1,6 @@ PRAGMA foreign_keys=off; +-- Create temporary rules table CREATE TABLE IF NOT EXISTS rules_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, flag_key VARCHAR(255) NOT NULL, @@ -10,13 +11,17 @@ CREATE TABLE IF NOT EXISTS rules_temp ( namespace_key VARCHAR(255) NOT NULL DEFAULT 'default' REFERENCES namespaces ON DELETE CASCADE ); +-- Copy data from rules table to temporary rules table INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) SELECT id, flag_key, segment_key, rank, created_at, updated_at FROM rules; +-- Drop old rules table DROP TABLE rules; +-- Rename temporary rules table to rules ALTER TABLE rules_temp RENAME TO rules; +-- Create temporary flags table CREATE TABLE IF NOT EXISTS flags_temp ( key VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, @@ -28,13 +33,17 @@ CREATE TABLE IF NOT EXISTS flags_temp ( PRIMARY KEY (namespace_key, key) ); +-- Copy data from flags table to temporary flags table INSERT INTO flags_temp (key, name, description, enabled, created_at, updated_at) SELECT key, name, description, enabled, created_at, updated_at FROM flags; +-- Drop old flags table DROP TABLE flags; +-- Rename temporary flags table to flags ALTER TABLE flags_temp RENAME TO flags; +-- Create temporary variants table CREATE TABLE IF NOT EXISTS variants_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, flag_key VARCHAR(255) NOT NULL, @@ -49,13 +58,17 @@ CREATE TABLE IF NOT EXISTS variants_temp ( FOREIGN KEY (namespace_key, flag_key) REFERENCES flags (namespace_key, key) ON DELETE CASCADE ); +-- Copy data from variants table to temporary variants table INSERT INTO variants_temp (id, flag_key, key, name, description, attachment, created_at, updated_at) SELECT id, flag_key, key, name, description, attachment, created_at, updated_at FROM variants; +-- Drop old variants table DROP TABLE variants; +-- Rename temporary variants table to variants ALTER TABLE variants_temp RENAME TO variants; +-- Create temporary segments table CREATE TABLE IF NOT EXISTS segments_temp ( key VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, @@ -67,13 +80,17 @@ CREATE TABLE IF NOT EXISTS segments_temp ( PRIMARY KEY (namespace_key, key) ); +-- Copy data from segments table to temporary segments table INSERT INTO segments_temp (key, name, description, match_type, created_at, updated_at) SELECT key, name, description, match_type, created_at, updated_at FROM segments; +-- Drop old segments table DROP TABLE segments; +-- Rename temporary segments table to segments ALTER TABLE segments_temp RENAME TO segments; +-- Create temporary constraints table CREATE TABLE IF NOT EXISTS constraints_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, segment_key VARCHAR(255) NOT NULL, @@ -87,13 +104,17 @@ CREATE TABLE IF NOT EXISTS constraints_temp ( FOREIGN KEY (namespace_key, segment_key) REFERENCES segments (namespace_key, key) ON DELETE CASCADE ); +-- Copy data from constraints table to temporary constraints table INSERT INTO constraints_temp (id, segment_key, type, property, operator, value, created_at, updated_at) SELECT id, segment_key, type, property, operator, value, created_at, updated_at FROM constraints; +-- Drop old constraints table DROP TABLE constraints; +-- Rename temporary constraints table to constraints ALTER TABLE constraints_temp RENAME TO constraints; +-- Create temporary rules table CREATE TABLE IF NOT EXISTS rules_temp ( id VARCHAR(255) PRIMARY KEY UNIQUE NOT NULL, flag_key VARCHAR(255) NOT NULL, @@ -106,11 +127,14 @@ CREATE TABLE IF NOT EXISTS rules_temp ( FOREIGN KEY (namespace_key, segment_key) REFERENCES segments (namespace_key, key) ON DELETE CASCADE ); +-- Copy data from rules table to temporary rules table INSERT INTO rules_temp (id, flag_key, segment_key, rank, created_at, updated_at) SELECT id, flag_key, segment_key, rank, created_at, updated_at FROM rules; +-- Drop old rules table DROP TABLE rules; +-- Rename temporary rules table to rules ALTER TABLE rules_temp RENAME TO rules; PRAGMA foreign_keys=on; diff --git a/internal/storage/sql/migrator.go b/internal/storage/sql/migrator.go index 52cfe04665..fec4d438d5 100644 --- a/internal/storage/sql/migrator.go +++ b/internal/storage/sql/migrator.go @@ -18,7 +18,7 @@ import ( var expectedVersions = map[Driver]uint{ SQLite: 7, - Postgres: 9, + Postgres: 7, MySQL: 7, CockroachDB: 6, } @@ -100,7 +100,6 @@ func (m *Migrator) Up(force bool) error { } m.logger.Debug("migrations complete") - return nil } @@ -136,6 +135,5 @@ func (m *Migrator) Down() error { } m.logger.Debug("down migrations complete") - return nil } From 3d83caa871c4edf9cdee1b22f2230084e4b63712 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Tue, 14 Mar 2023 11:01:15 -0400 Subject: [PATCH 4/6] chore: fix mysql migrations --- .../mysql/5_namespaces_flags.down.sql | 16 ---- .../mysql/5_namespaces_flags.up.sql | 13 --- .../mysql/5_namespaces_relationships.down.sql | 79 ++++++++++++++++++ .../mysql/5_namespaces_relationships.up.sql | 83 +++++++++++++++++++ .../mysql/6_namespaces_segments.down.sql | 1 - .../mysql/6_namespaces_segments.up.sql | 13 --- .../mysql/7_namespaces_rules.down.sql | 0 .../mysql/7_namespaces_rules.up.sql | 12 --- .../7_namespaces_relationships.down.sql | 2 +- .../7_namespaces_relationships.up.sql | 32 ++++++- internal/storage/sql/migrator.go | 2 +- 11 files changed, 195 insertions(+), 58 deletions(-) delete mode 100644 config/migrations/mysql/5_namespaces_flags.down.sql delete mode 100644 config/migrations/mysql/5_namespaces_flags.up.sql create mode 100644 config/migrations/mysql/5_namespaces_relationships.down.sql create mode 100644 config/migrations/mysql/5_namespaces_relationships.up.sql delete mode 100644 config/migrations/mysql/6_namespaces_segments.down.sql delete mode 100644 config/migrations/mysql/6_namespaces_segments.up.sql delete mode 100644 config/migrations/mysql/7_namespaces_rules.down.sql delete mode 100644 config/migrations/mysql/7_namespaces_rules.up.sql diff --git a/config/migrations/mysql/5_namespaces_flags.down.sql b/config/migrations/mysql/5_namespaces_flags.down.sql deleted file mode 100644 index 0b8422d769..0000000000 --- a/config/migrations/mysql/5_namespaces_flags.down.sql +++ /dev/null @@ -1,16 +0,0 @@ - -- drop previously created foreign keys -ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_1`; -ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_2`; - --- drop previously created unique index -ALTER TABLE variants DROP INDEX `variants_namespace_flag_key`, ADD UNIQUE INDEX `variants_flag_key_key` (`flag_key`, `key`) USING BTREE; -ALTER TABLE variants DROP COLUMN namespace_key; - --- drop foreign key to namespaces -ALTER TABLE flags DROP FOREIGN KEY `flags_ibfk_1`; --- drop previously created index -ALTER TABLE flags DROP INDEX `key`; --- drop namespaces, key primary key -ALTER TABLE flags DROP PRIMARY KEY, ADD PRIMARY KEY (`key`); -ALTER TABLE flags DROP COLUMN namespace_key; - diff --git a/config/migrations/mysql/5_namespaces_flags.up.sql b/config/migrations/mysql/5_namespaces_flags.up.sql deleted file mode 100644 index 09f8c495bc..0000000000 --- a/config/migrations/mysql/5_namespaces_flags.up.sql +++ /dev/null @@ -1,13 +0,0 @@ -ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - -- drop previously created unique index -ALTER TABLE flags DROP INDEX `key`, ADD INDEX `key` (`key`) USING BTREE; -ALTER TABLE flags ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; -ALTER TABLE flags DROP PRIMARY KEY, ADD PRIMARY KEY (`namespace_key`, `key`); - -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 -ALTER TABLE variants DROP INDEX `variants_flag_key_key`, ADD UNIQUE INDEX `variants_namespace_flag_key` (`namespace_key`, `flag_key`, `key`) USING BTREE; -ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; -ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(`namespace_key`, `key`) ON DELETE CASCADE; diff --git a/config/migrations/mysql/5_namespaces_relationships.down.sql b/config/migrations/mysql/5_namespaces_relationships.down.sql new file mode 100644 index 0000000000..8a75d347e5 --- /dev/null +++ b/config/migrations/mysql/5_namespaces_relationships.down.sql @@ -0,0 +1,79 @@ +-- Rules + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_1`; + +-- Drop foreign key constraint on namespace_key and flag_key columns +ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_2`; + +-- Drop foreign key constraint on namespace_key and segment_key columns +ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_3`; + +-- Drop previously created index and add a new index on flag_key and segment_key columns +ALTER TABLE rules DROP INDEX `rules_namespace_flag_key`, ADD INDEX `flag_key` (`flag_key`) USING BTREE; +ALTER TABLE rules DROP INDEX `rules_namespace_segment_key`, ADD INDEX `segment_key` (`segment_key`) USING BTREE; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE rules ADD FOREIGN KEY (flag_key) REFERENCES flags(`key`) ON DELETE CASCADE; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE rules ADD FOREIGN KEY (segment_key) REFERENCES segments(`key`) ON DELETE CASCADE; + +-- Drop column namespace_key +ALTER TABLE rules DROP COLUMN namespace_key; + +-- Variants + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_1`; + +-- Drop foreign key constraint on namespace_key and flag_key columns +ALTER TABLE variants DROP FOREIGN KEY `variants_ibfk_2`; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE variants ADD FOREIGN KEY (flag_key) REFERENCES flags(`key`) ON DELETE CASCADE; + +-- Drop previously created unique index and add a new unique index on flag_key and key columns +ALTER TABLE variants DROP INDEX `variants_namespace_flag_key`, ADD UNIQUE INDEX `variants_flag_key_key` (`flag_key`, `key`) USING BTREE; + +-- Drop column namespace_key +ALTER TABLE variants DROP COLUMN namespace_key; + +-- Flags + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE flags DROP FOREIGN KEY `flags_ibfk_1`; + +-- Drop primary key constraint and add a new primary key on key column +ALTER TABLE flags DROP PRIMARY KEY, ADD PRIMARY KEY (`key`); + +-- Drop column namespace_key +ALTER TABLE flags DROP COLUMN namespace_key; + +-- Constraints + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE constraints DROP FOREIGN KEY `constraints_ibfk_1`; + +-- Drop foreign key constraint on namespace_key and segment_key columns +ALTER TABLE constraints DROP FOREIGN KEY `constraints_ibfk_2`; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE constraints ADD FOREIGN KEY (segment_key) REFERENCES segments(`key`) ON DELETE CASCADE; + +-- Drop index on namespace_key and segment_key columns and add a new index on segment_key column +ALTER TABLE constraints DROP INDEX `constraints_namespace_segment_key`, ADD INDEX `segment_key` (`segment_key`) USING BTREE; + +-- Drop column namespace_key +ALTER TABLE constraints DROP COLUMN namespace_key; + +-- Segments + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE segments DROP FOREIGN KEY `segments_ibfk_1`; + +-- Drop primary key constraint and add a new primary key on key column +ALTER TABLE segments DROP PRIMARY KEY, ADD PRIMARY KEY (`key`); + +-- Drop column namespace_key +ALTER TABLE segments DROP COLUMN namespace_key; \ No newline at end of file diff --git a/config/migrations/mysql/5_namespaces_relationships.up.sql b/config/migrations/mysql/5_namespaces_relationships.up.sql new file mode 100644 index 0000000000..14ec7289f1 --- /dev/null +++ b/config/migrations/mysql/5_namespaces_relationships.up.sql @@ -0,0 +1,83 @@ +-- Flags + +-- Add column namespace_key with a default value +ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- 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`); + +-- 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; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and flag_key columns referencing namespace_key and key columns of flags table +ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(`namespace_key`, `key`) ON DELETE CASCADE; + +-- 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; + +-- 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`); + +-- 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; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(`namespace_key`, `key`) ON DELETE CASCADE; + +-- Rules + +-- 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; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and flag_key columns referencing namespace_key and key columns of flags table +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(`namespace_key`, `key`) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(`namespace_key`, `key`) ON DELETE CASCADE; diff --git a/config/migrations/mysql/6_namespaces_segments.down.sql b/config/migrations/mysql/6_namespaces_segments.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/mysql/6_namespaces_segments.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/mysql/6_namespaces_segments.up.sql b/config/migrations/mysql/6_namespaces_segments.up.sql deleted file mode 100644 index 7e7ced76fa..0000000000 --- a/config/migrations/mysql/6_namespaces_segments.up.sql +++ /dev/null @@ -1,13 +0,0 @@ -ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- drop previously created unique index -ALTER TABLE segments DROP INDEX `key`, ADD INDEX `key` (`key`) USING BTREE; -ALTER TABLE segments ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; -ALTER TABLE segments DROP PRIMARY KEY, ADD PRIMARY KEY (`namespace_key`, `key`); - -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 -ALTER TABLE constraints DROP INDEX `segment_key`, ADD INDEX `constraints_namespace_segment_key` (`namespace_key`, `segment_key`) USING BTREE; -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(`namespace_key`, `key`) ON DELETE CASCADE; diff --git a/config/migrations/mysql/7_namespaces_rules.down.sql b/config/migrations/mysql/7_namespaces_rules.down.sql deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/config/migrations/mysql/7_namespaces_rules.up.sql b/config/migrations/mysql/7_namespaces_rules.up.sql deleted file mode 100644 index 9275e42c80..0000000000 --- a/config/migrations/mysql/7_namespaces_rules.up.sql +++ /dev/null @@ -1,12 +0,0 @@ -ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- drop previously created foreign key -ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_1`; --- drop previously created foreign key -ALTER TABLE rules DROP FOREIGN KEY `rules_ibfk_2`; --- drop previously created index -ALTER TABLE rules DROP INDEX `flag_key`, ADD INDEX `rules_namespace_flag_key` (`namespace_key`, `flag_key`) USING BTREE; --- drop previously created index -ALTER TABLE rules DROP INDEX `segment_key`, ADD INDEX `rules_namespace_segment_key` (`namespace_key`, `segment_key`) USING BTREE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(`key`) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(`namespace_key`, `key`) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(`namespace_key`, `key`) ON DELETE CASCADE; diff --git a/config/migrations/postgres/7_namespaces_relationships.down.sql b/config/migrations/postgres/7_namespaces_relationships.down.sql index 796dd0351d..a69f259e1f 100644 --- a/config/migrations/postgres/7_namespaces_relationships.down.sql +++ b/config/migrations/postgres/7_namespaces_relationships.down.sql @@ -4,7 +4,7 @@ -- Drop foreign key constraint on namespace_key column ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_namespace_key_fkey CASCADE; --- Drop primary key constraint and add a new composite primary key on key column +-- Drop primary key constraint and add a new primary key on key column ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_pkey CASCADE; ALTER TABLE flags ADD CONSTRAINT flags_pkey PRIMARY KEY (key); diff --git a/config/migrations/postgres/7_namespaces_relationships.up.sql b/config/migrations/postgres/7_namespaces_relationships.up.sql index 6313990dd9..dd2216210a 100644 --- a/config/migrations/postgres/7_namespaces_relationships.up.sql +++ b/config/migrations/postgres/7_namespaces_relationships.up.sql @@ -1,3 +1,6 @@ +-- Flags +---------------- + -- Add column namespace_key with a default value ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; @@ -8,12 +11,27 @@ ALTER TABLE flags ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_pkey CASCADE; ALTER TABLE flags ADD CONSTRAINT flags_pkey PRIMARY KEY (namespace_key, key); +-- 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 unique index ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_flag_key_key_key CASCADE; + +-- Add unique index on namespace_key, flag_key and key columns ALTER TABLE variants ADD CONSTRAINT variants_namespace_flag_key UNIQUE (namespace_key, flag_key, key); + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and flag_key columns referencing namespace_key and key columns of flags table ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; +-- Segments +---------------- + -- Add column namespace_key with a default value ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; @@ -24,17 +42,29 @@ ALTER TABLE segments ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_pkey CASCADE; ALTER TABLE segments ADD CONSTRAINT segments_pkey PRIMARY KEY (namespace_key, key); +-- Constraints +---------------- + -- Add column namespace_key with a default value ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; +-- Rules +---------------- + -- Add column namespace_key with a default value ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; -- Add foreign key constraint on namespace_key column referencing key column of namespaces table ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and flag_key columns referencing namespace_key and key columns of flags table ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/internal/storage/sql/migrator.go b/internal/storage/sql/migrator.go index fec4d438d5..1121d1606b 100644 --- a/internal/storage/sql/migrator.go +++ b/internal/storage/sql/migrator.go @@ -19,7 +19,7 @@ import ( var expectedVersions = map[Driver]uint{ SQLite: 7, Postgres: 7, - MySQL: 7, + MySQL: 5, CockroachDB: 6, } From 5869a4d2179df2ca26ddcfe83a16523d16134665 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Tue, 14 Mar 2023 11:45:59 -0400 Subject: [PATCH 5/6] chore: fix cockroachdb --- cmd/flipt/import.go | 2 +- config/local.yml | 2 +- .../cockroachdb/3_create_namespaces.down.sql | 2 +- .../cockroachdb/4_namespaces_flags.down.sql | 1 - .../cockroachdb/4_namespaces_flags.up.sql | 15 ---- .../4_namespaces_relationships.down.sql | 70 +++++++++++++++++++ .../4_namespaces_relationships.up.sql | 66 +++++++++++++++++ .../5_namespaces_segments.down.sql | 1 - .../cockroachdb/5_namespaces_segments.up.sql | 18 ----- .../cockroachdb/6_namespaces_rules.down.sql | 1 - .../cockroachdb/6_namespaces_rules.up.sql | 7 -- internal/storage/sql/migrator.go | 4 +- 12 files changed, 141 insertions(+), 48 deletions(-) delete mode 100644 config/migrations/cockroachdb/4_namespaces_flags.down.sql delete mode 100644 config/migrations/cockroachdb/4_namespaces_flags.up.sql create mode 100644 config/migrations/cockroachdb/4_namespaces_relationships.down.sql create mode 100644 config/migrations/cockroachdb/4_namespaces_relationships.up.sql delete mode 100644 config/migrations/cockroachdb/5_namespaces_segments.down.sql delete mode 100644 config/migrations/cockroachdb/5_namespaces_segments.up.sql delete mode 100644 config/migrations/cockroachdb/6_namespaces_rules.down.sql delete mode 100644 config/migrations/cockroachdb/6_namespaces_rules.up.sql diff --git a/cmd/flipt/import.go b/cmd/flipt/import.go index 2f86b1cb10..7569c1c133 100644 --- a/cmd/flipt/import.go +++ b/cmd/flipt/import.go @@ -49,7 +49,7 @@ func runImport(ctx context.Context, logger *zap.Logger, args []string) error { switch driver { case sql.SQLite: store = sqlite.NewStore(db, logger) - case sql.Postgres: + case sql.Postgres, sql.CockroachDB: store = postgres.NewStore(db, logger) case sql.MySQL: store = mysql.NewStore(db, logger) diff --git a/config/local.yml b/config/local.yml index d4763cac45..0cbd230e5e 100644 --- a/config/local.yml +++ b/config/local.yml @@ -29,4 +29,4 @@ cors: # grpc_port: 9000 db: - url: file:flipt.db + url: cockroach://root@localhost:26257/defaultdb?sslmode=disable diff --git a/config/migrations/cockroachdb/3_create_namespaces.down.sql b/config/migrations/cockroachdb/3_create_namespaces.down.sql index 48e70a7ed5..24fdbc33ce 100644 --- a/config/migrations/cockroachdb/3_create_namespaces.down.sql +++ b/config/migrations/cockroachdb/3_create_namespaces.down.sql @@ -1 +1 @@ -/* TODO */ \ No newline at end of file +DROP TABLE IF EXISTS namespaces CASCADE; \ No newline at end of file diff --git a/config/migrations/cockroachdb/4_namespaces_flags.down.sql b/config/migrations/cockroachdb/4_namespaces_flags.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/cockroachdb/4_namespaces_flags.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/cockroachdb/4_namespaces_flags.up.sql b/config/migrations/cockroachdb/4_namespaces_flags.up.sql deleted file mode 100644 index 8c080ebf87..0000000000 --- a/config/migrations/cockroachdb/4_namespaces_flags.up.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- 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 ALTER PRIMARY KEY USING COLUMNS (namespace_key, key); -DROP INDEX IF EXISTS flags_key_key CASCADE; - -ALTER TABLE variants ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; -DROP INDEX IF EXISTS "variants_flag_key_key" CASCADE; -ALTER TABLE variants ADD CONSTRAINT "variants_namespace_flag_key" UNIQUE (namespace_key, flag_key, key); -ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/cockroachdb/4_namespaces_relationships.down.sql b/config/migrations/cockroachdb/4_namespaces_relationships.down.sql new file mode 100644 index 0000000000..cca39b77d9 --- /dev/null +++ b/config/migrations/cockroachdb/4_namespaces_relationships.down.sql @@ -0,0 +1,70 @@ +-- Flags +---------------- + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE flags DROP CONSTRAINT IF EXISTS flags_namespace_key_fkey CASCADE; + +-- Drop primary key constraint and add a new composite primary key on namespace_key and key columns +ALTER TABLE flags ALTER PRIMARY KEY USING COLUMNS (key); +DROP INDEX IF EXISTS flags_namespace_key_key_key CASCADE; + +-- Drop column namespace_key +ALTER TABLE flags DROP COLUMN IF EXISTS namespace_key; + +-- Variants +---------------- + +-- Drop foreign key constraints on namespace_key and flag_key columns +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_namespace_flag_key_fkey CASCADE; +ALTER TABLE variants DROP CONSTRAINT IF EXISTS variants_namespace_key_fkey CASCADE; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE variants ADD FOREIGN KEY (flag_key) REFERENCES flags(key) ON DELETE CASCADE; + +-- Drop unique constraint created by previous migration and add a new unique constraint on flag_key and key columns +ALTER TABLE variants ADD CONSTRAINT variants_flag_key UNIQUE (flag_key, key); + +-- Drop column namespace_key +ALTER TABLE variants DROP COLUMN IF EXISTS namespace_key; + +-- Segments +---------------- + +-- Drop foreign key constraint on namespace_key column +ALTER TABLE segments DROP CONSTRAINT IF EXISTS segments_namespace_key_fkey CASCADE; + +-- Drop primary key constraint and add a new primary key constraint on key column +ALTER TABLE segments ALTER PRIMARY KEY USING COLUMNS (key); +DROP INDEX IF EXISTS segments_namespace_key_key_key CASCADE; + +-- Drop column namespace_key +ALTER TABLE segments DROP COLUMN IF EXISTS namespace_key; + +-- Constraints +---------------- + +-- Drop foreign key constraints on namespace_key and segment_key columns +ALTER TABLE constraints DROP CONSTRAINT IF EXISTS constraints_namespace_key_fkey CASCADE; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE constraints ADD FOREIGN KEY (segment_key) REFERENCES segments(key) ON DELETE CASCADE; + +-- Drop column namespace_key +ALTER TABLE constraints DROP COLUMN IF EXISTS namespace_key; + +-- Rules +---------------- + +-- Drop foreign key constraints on namespace_key, flag_key and segment_key columns +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_fkey CASCADE; +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_flag_key_fkey CASCADE; +ALTER TABLE rules DROP CONSTRAINT IF EXISTS rules_namespace_key_segment_key_fkey CASCADE; + +-- Add foreign key constraint on flag_key column referencing key column of flags table +ALTER TABLE rules ADD FOREIGN KEY (flag_key) REFERENCES flags(key) ON DELETE CASCADE; + +-- Add foreign key constraint on segment_key column referencing key column of segments table +ALTER TABLE rules ADD FOREIGN KEY (segment_key) REFERENCES segments(key) ON DELETE CASCADE; + +-- Drop column namespace_key +ALTER TABLE rules DROP COLUMN IF EXISTS namespace_key; diff --git a/config/migrations/cockroachdb/4_namespaces_relationships.up.sql b/config/migrations/cockroachdb/4_namespaces_relationships.up.sql new file mode 100644 index 0000000000..ec61f74b39 --- /dev/null +++ b/config/migrations/cockroachdb/4_namespaces_relationships.up.sql @@ -0,0 +1,66 @@ +-- Flags +------------------ + +-- Add column namespace_key with a default value +ALTER TABLE flags ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- 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 ALTER PRIMARY KEY USING COLUMNS (namespace_key, key); +DROP INDEX IF EXISTS flags_key_key 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 unique index +DROP INDEX IF EXISTS "variants_flag_key_key" CASCADE; + +-- Add unique index on namespace_key, flag_key and key columns +ALTER TABLE variants ADD CONSTRAINT "variants_namespace_flag_key" UNIQUE (namespace_key, flag_key, key); + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE variants ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and flag_key columns referencing namespace_key and key columns of flags table +ALTER TABLE variants ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; + +-- Segments +------------------ + +-- Add column namespace_key with a default value +ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- 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; + +-- Drop primary key constraint and add a new composite primary key on namespace_key and key columns +ALTER TABLE segments ALTER PRIMARY KEY USING COLUMNS (namespace_key, key); +DROP INDEX IF EXISTS segments_key_key CASCADE; + +-- Constraints +------------------ + +-- Add column namespace_key with a default value +ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; + +-- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table +ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; + +-- Rules +------------------ + +-- Add column namespace_key with a default value +ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; + +-- Add foreign key constraint on namespace_key column referencing key column of namespaces table +ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; +ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/cockroachdb/5_namespaces_segments.down.sql b/config/migrations/cockroachdb/5_namespaces_segments.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/cockroachdb/5_namespaces_segments.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/cockroachdb/5_namespaces_segments.up.sql b/config/migrations/cockroachdb/5_namespaces_segments.up.sql deleted file mode 100644 index 11d3381fc2..0000000000 --- a/config/migrations/cockroachdb/5_namespaces_segments.up.sql +++ /dev/null @@ -1,18 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE segments ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- 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; - --- Drop primary key constraint and add a new composite primary key on namespace_key and key columns -ALTER TABLE segments ALTER PRIMARY KEY USING COLUMNS (namespace_key, key); -DROP INDEX IF EXISTS segments_key_key CASCADE; - --- Add column namespace_key with a default value -ALTER TABLE constraints ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; - --- Add foreign key constraint on namespace_key and segment_key columns referencing namespace_key and key columns of segments table -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE constraints ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/config/migrations/cockroachdb/6_namespaces_rules.down.sql b/config/migrations/cockroachdb/6_namespaces_rules.down.sql deleted file mode 100644 index 48e70a7ed5..0000000000 --- a/config/migrations/cockroachdb/6_namespaces_rules.down.sql +++ /dev/null @@ -1 +0,0 @@ -/* TODO */ \ No newline at end of file diff --git a/config/migrations/cockroachdb/6_namespaces_rules.up.sql b/config/migrations/cockroachdb/6_namespaces_rules.up.sql deleted file mode 100644 index 106797edb9..0000000000 --- a/config/migrations/cockroachdb/6_namespaces_rules.up.sql +++ /dev/null @@ -1,7 +0,0 @@ --- Add column namespace_key with a default value -ALTER TABLE rules ADD COLUMN namespace_key VARCHAR(255) NOT NULL DEFAULT 'default'; - --- Add foreign key constraint on namespace_key column referencing key column of namespaces table -ALTER TABLE rules ADD FOREIGN KEY (namespace_key) REFERENCES namespaces(key) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, flag_key) REFERENCES flags(namespace_key, key) ON DELETE CASCADE; -ALTER TABLE rules ADD FOREIGN KEY (namespace_key, segment_key) REFERENCES segments(namespace_key, key) ON DELETE CASCADE; \ No newline at end of file diff --git a/internal/storage/sql/migrator.go b/internal/storage/sql/migrator.go index 1121d1606b..0929633a9f 100644 --- a/internal/storage/sql/migrator.go +++ b/internal/storage/sql/migrator.go @@ -20,7 +20,7 @@ var expectedVersions = map[Driver]uint{ SQLite: 7, Postgres: 7, MySQL: 5, - CockroachDB: 6, + CockroachDB: 4, } // Migrator is responsible for migrating the database schema @@ -128,7 +128,7 @@ func (m *Migrator) Up(force bool) error { // Down returns the down migrations (drops the database) func (m *Migrator) Down() error { - m.logger.Debug("Running down migrations...") + m.logger.Debug("running down migrations...") if err := m.migrator.Down(); err != nil { return fmt.Errorf("reverting migrations: %w", err) From b0ea40262e0daf629e47388f2b73b11604c4cba3 Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Tue, 14 Mar 2023 11:50:15 -0400 Subject: [PATCH 6/6] chore: reset local config --- config/local.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/local.yml b/config/local.yml index 0cbd230e5e..d4763cac45 100644 --- a/config/local.yml +++ b/config/local.yml @@ -29,4 +29,4 @@ cors: # grpc_port: 9000 db: - url: cockroach://root@localhost:26257/defaultdb?sslmode=disable + url: file:flipt.db