Skip to content

Commit

Permalink
chore: namespaces down migrations (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Mar 14, 2023
1 parent f80825b commit 419d919
Show file tree
Hide file tree
Showing 36 changed files with 699 additions and 276 deletions.
2 changes: 1 addition & 1 deletion cmd/flipt/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion config/migrations/cockroachdb/3_create_namespaces.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* TODO */
DROP TABLE IF EXISTS namespaces CASCADE;
1 change: 0 additions & 1 deletion config/migrations/cockroachdb/4_namespaces_flags.down.sql

This file was deleted.

15 changes: 0 additions & 15 deletions config/migrations/cockroachdb/4_namespaces_flags.up.sql

This file was deleted.

70 changes: 70 additions & 0 deletions config/migrations/cockroachdb/4_namespaces_relationships.down.sql
Original file line number Diff line number Diff line change
@@ -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;
66 changes: 66 additions & 0 deletions config/migrations/cockroachdb/4_namespaces_relationships.up.sql
Original file line number Diff line number Diff line change
@@ -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;

This file was deleted.

18 changes: 0 additions & 18 deletions config/migrations/cockroachdb/5_namespaces_segments.up.sql

This file was deleted.

1 change: 0 additions & 1 deletion config/migrations/cockroachdb/6_namespaces_rules.down.sql

This file was deleted.

7 changes: 0 additions & 7 deletions config/migrations/cockroachdb/6_namespaces_rules.up.sql

This file was deleted.

1 change: 0 additions & 1 deletion config/migrations/mysql/5_namespaces_flags.down.sql

This file was deleted.

13 changes: 0 additions & 13 deletions config/migrations/mysql/5_namespaces_flags.up.sql

This file was deleted.

79 changes: 79 additions & 0 deletions config/migrations/mysql/5_namespaces_relationships.down.sql
Original file line number Diff line number Diff line change
@@ -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;
83 changes: 83 additions & 0 deletions config/migrations/mysql/5_namespaces_relationships.up.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion config/migrations/mysql/6_namespaces_segments.down.sql

This file was deleted.

Loading

0 comments on commit 419d919

Please sign in to comment.