Skip to content

Commit

Permalink
chore: fix cockroachdb
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Mar 14, 2023
1 parent 3d83caa commit 5869a4d
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 48 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/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ cors:
# grpc_port: 9000

db:
url: file:flipt.db
url: cockroach://root@localhost:26257/defaultdb?sslmode=disable
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.

4 changes: 2 additions & 2 deletions internal/storage/sql/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5869a4d

Please sign in to comment.