diff --git a/server/config/demo/demo_repository.go b/server/config/demo/demo_repository.go index fcee382bb4..9156ac67c7 100644 --- a/server/config/demo/demo_repository.go +++ b/server/config/demo/demo_repository.go @@ -51,17 +51,15 @@ func (r *Repository) Create(ctx context.Context, demo Demo) (Demo, error) { return Demo{}, fmt.Errorf("could not get JSON data from opentelemetry store example: %w", err) } - tenantID := sqlutil.TenantID(ctx) - - _, err = tx.ExecContext(ctx, insertQuery, + params := sqlutil.TenantInsert(ctx, demo.ID, demo.Name, demo.Enabled, demo.Type, pokeshopJSONData, openTelemetryStoreJSONData, - tenantID, ) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { tx.Rollback() diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index d5f0df2d28..eb7424fb00 100644 --- a/server/datastore/datastore_repository.go +++ b/server/datastore/datastore_repository.go @@ -91,7 +91,8 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore } defer tx.Rollback() - _, err = tx.ExecContext(ctx, deleteQuery, DataStoreSingleID) + query, params := sqlutil.Tenant(ctx, deleteQuery, DataStoreSingleID) + _, err = tx.ExecContext(ctx, query, params...) if err != nil { return DataStore{}, fmt.Errorf("datastore repository sql exec delete: %w", err) } @@ -101,17 +102,15 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore return DataStore{}, fmt.Errorf("could not marshal values field configuration: %w", err) } - tenantID := sqlutil.TenantID(ctx) - - _, err = tx.ExecContext(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, dataStore.ID, dataStore.Name, dataStore.Type, dataStore.Default, valuesJSON, dataStore.CreatedAt, - tenantID, ) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { return DataStore{}, fmt.Errorf("datastore repository sql exec create: %w", err) } diff --git a/server/executor/pollingprofile/polling_profile_repository.go b/server/executor/pollingprofile/polling_profile_repository.go index 0be2462ec0..8ae50d60e6 100644 --- a/server/executor/pollingprofile/polling_profile_repository.go +++ b/server/executor/pollingprofile/polling_profile_repository.go @@ -68,15 +68,14 @@ func (r *Repository) Update(ctx context.Context, updated PollingProfile) (Pollin } } - tenantID := sqlutil.TenantID(ctx) - _, err = tx.ExecContext(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, updated.Default, updated.Strategy, periodicJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { return PollingProfile{}, fmt.Errorf("sql exec insert: %w", err) } diff --git a/server/executor/testrunner/testrunner_repository.go b/server/executor/testrunner/testrunner_repository.go index 9ba9b5be41..cc41b10a6f 100644 --- a/server/executor/testrunner/testrunner_repository.go +++ b/server/executor/testrunner/testrunner_repository.go @@ -51,11 +51,12 @@ func (r *Repository) Update(ctx context.Context, updated TestRunner) (TestRunner } defer tx.Rollback() - _, err = tx.ExecContext(ctx, deleteQuery) + + query, params := sqlutil.Tenant(ctx, deleteQuery) + _, err = tx.ExecContext(ctx, query, params...) if err != nil { return TestRunner{}, fmt.Errorf("sql exec delete: %w", err) } - tenantID := sqlutil.TenantID(ctx) var requiredGatesJSON []byte if updated.RequiredGates != nil { @@ -65,12 +66,12 @@ func (r *Repository) Update(ctx context.Context, updated TestRunner) (TestRunner } } - _, err = tx.ExecContext(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, requiredGatesJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { return TestRunner{}, fmt.Errorf("sql exec insert: %w", err) } diff --git a/server/linter/analyzer/analyzer_repository.go b/server/linter/analyzer/analyzer_repository.go index 2d4379d85c..d0eb5586a5 100644 --- a/server/linter/analyzer/analyzer_repository.go +++ b/server/linter/analyzer/analyzer_repository.go @@ -90,17 +90,14 @@ func (r *Repository) Update(ctx context.Context, linter Linter) (Linter, error) } } - tenantID := sqlutil.TenantID(ctx) - _, err = tx.ExecContext( - ctx, - insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, updated.Enabled, updated.MinimumScore, pluginsJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { return Linter{}, fmt.Errorf("sql exec insert: %w", err) } @@ -125,7 +122,8 @@ func (r *Repository) Delete(ctx context.Context, id id.ID) error { } defer tx.Rollback() - _, err = tx.ExecContext(ctx, deleteQuery, id) + query, params := sqlutil.Tenant(ctx, deleteQuery, id) + _, err = tx.ExecContext(ctx, query, params...) if err != nil { return fmt.Errorf("sql error: %w", err) } diff --git a/server/migrations/33_add_composite_pkey.down.sql b/server/migrations/33_add_composite_pkey.down.sql new file mode 100644 index 0000000000..cf8eb08799 --- /dev/null +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -0,0 +1,67 @@ +BEGIN; + +ALTER TABLE data_stores +DROP CONSTRAINT data_stores_pkey, +ADD PRIMARY KEY (id), +ALTER COLUMN tenant_id DROP DEFAULT, +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE data_stores +SET tenant_id = null +WHERE tenant_id = ''; + +ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; + + +ALTER TABLE demos +DROP CONSTRAINT demos_pkey, +ADD PRIMARY KEY (id), +ALTER COLUMN tenant_id DROP DEFAULT, +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE demos +SET tenant_id = null +WHERE tenant_id = ''; + +ALTER TABLE demos ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; + + +ALTER TABLE polling_profiles +DROP CONSTRAINT polling_profiles_pkey, +ADD PRIMARY KEY (id), +ALTER COLUMN tenant_id DROP DEFAULT, +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE polling_profiles +SET tenant_id = null +WHERE tenant_id = ''; + +ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; + + +ALTER TABLE linters +DROP CONSTRAINT linters_pkey, +ADD PRIMARY KEY (id), +ALTER COLUMN tenant_id DROP DEFAULT, +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE linters +SET tenant_id = null +WHERE tenant_id = ''; + +ALTER TABLE linters ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; + + +ALTER TABLE test_runners +DROP CONSTRAINT test_runners_pkey, +ADD PRIMARY KEY (id), +ALTER COLUMN tenant_id DROP DEFAULT, +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE test_runners +SET tenant_id = null +WHERE tenant_id = ''; + +ALTER TABLE test_runners ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; + +COMMIT; \ No newline at end of file diff --git a/server/migrations/33_add_composite_pkey.up.sql b/server/migrations/33_add_composite_pkey.up.sql new file mode 100644 index 0000000000..ecebab6d72 --- /dev/null +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -0,0 +1,62 @@ +BEGIN; + +ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE varchar; + +UPDATE data_stores +SET tenant_id = '' +WHERE tenant_id is null; + +ALTER TABLE data_stores +DROP CONSTRAINT data_stores_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT ''; + + +ALTER TABLE demos ALTER COLUMN tenant_id TYPE varchar; + +UPDATE demos +SET tenant_id = '' +WHERE tenant_id is null; + +ALTER TABLE demos +DROP CONSTRAINT demos_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT ''; + + +ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE varchar; + +UPDATE polling_profiles +SET tenant_id = '' +WHERE tenant_id is null; + +ALTER TABLE polling_profiles +DROP CONSTRAINT polling_profiles_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT ''; + + +ALTER TABLE linters ALTER COLUMN tenant_id TYPE varchar; + +UPDATE linters +SET tenant_id = '' +WHERE tenant_id is null; + +ALTER TABLE linters +DROP CONSTRAINT linters_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT ''; + + +ALTER TABLE test_runners ALTER COLUMN tenant_id TYPE varchar; + +UPDATE test_runners +SET tenant_id = '' +WHERE tenant_id is null; + +ALTER TABLE test_runners +DROP CONSTRAINT test_runners_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT ''; + +COMMIT; \ No newline at end of file diff --git a/server/pkg/sqlutil/tenant.go b/server/pkg/sqlutil/tenant.go index 5ee968827b..2ac8dd290c 100644 --- a/server/pkg/sqlutil/tenant.go +++ b/server/pkg/sqlutil/tenant.go @@ -35,6 +35,15 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params . return query + condition, append(params, *tenantID) } +func TenantInsert(ctx context.Context, params ...any) []any { + tenantID := TenantID(ctx) + if tenantID == nil { + return append(params, "") + } + + return append(params, *tenantID) +} + func TenantID(ctx context.Context) *string { tenantID := ctx.Value(middleware.TenantIDKey)