Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): add composite primary key for single row resources #3155

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions server/config/demo/demo_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions server/datastore/datastore_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions server/executor/pollingprofile/polling_profile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 5 additions & 4 deletions server/executor/testrunner/testrunner_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
10 changes: 4 additions & 6 deletions server/linter/analyzer/analyzer_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
67 changes: 67 additions & 0 deletions server/migrations/33_add_composite_pkey.down.sql
Original file line number Diff line number Diff line change
@@ -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;
62 changes: 62 additions & 0 deletions server/migrations/33_add_composite_pkey.up.sql
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions server/pkg/sqlutil/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading