Skip to content

Commit 7500911

Browse files
authored
fix(server): add composite primary key for single row resources (#3155)
1 parent 6895114 commit 7500911

File tree

8 files changed

+155
-22
lines changed

8 files changed

+155
-22
lines changed

server/config/demo/demo_repository.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ func (r *Repository) Create(ctx context.Context, demo Demo) (Demo, error) {
5151
return Demo{}, fmt.Errorf("could not get JSON data from opentelemetry store example: %w", err)
5252
}
5353

54-
tenantID := sqlutil.TenantID(ctx)
55-
56-
_, err = tx.ExecContext(ctx, insertQuery,
54+
params := sqlutil.TenantInsert(ctx,
5755
demo.ID,
5856
demo.Name,
5957
demo.Enabled,
6058
demo.Type,
6159
pokeshopJSONData,
6260
openTelemetryStoreJSONData,
63-
tenantID,
6461
)
62+
_, err = tx.ExecContext(ctx, insertQuery, params...)
6563

6664
if err != nil {
6765
tx.Rollback()

server/datastore/datastore_repository.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore
9191
}
9292
defer tx.Rollback()
9393

94-
_, err = tx.ExecContext(ctx, deleteQuery, DataStoreSingleID)
94+
query, params := sqlutil.Tenant(ctx, deleteQuery, DataStoreSingleID)
95+
_, err = tx.ExecContext(ctx, query, params...)
9596
if err != nil {
9697
return DataStore{}, fmt.Errorf("datastore repository sql exec delete: %w", err)
9798
}
@@ -101,17 +102,15 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore
101102
return DataStore{}, fmt.Errorf("could not marshal values field configuration: %w", err)
102103
}
103104

104-
tenantID := sqlutil.TenantID(ctx)
105-
106-
_, err = tx.ExecContext(ctx, insertQuery,
105+
params = sqlutil.TenantInsert(ctx,
107106
dataStore.ID,
108107
dataStore.Name,
109108
dataStore.Type,
110109
dataStore.Default,
111110
valuesJSON,
112111
dataStore.CreatedAt,
113-
tenantID,
114112
)
113+
_, err = tx.ExecContext(ctx, insertQuery, params...)
115114
if err != nil {
116115
return DataStore{}, fmt.Errorf("datastore repository sql exec create: %w", err)
117116
}

server/executor/pollingprofile/polling_profile_repository.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,14 @@ func (r *Repository) Update(ctx context.Context, updated PollingProfile) (Pollin
6868
}
6969
}
7070

71-
tenantID := sqlutil.TenantID(ctx)
72-
_, err = tx.ExecContext(ctx, insertQuery,
71+
params = sqlutil.TenantInsert(ctx,
7372
updated.ID,
7473
updated.Name,
7574
updated.Default,
7675
updated.Strategy,
7776
periodicJSON,
78-
tenantID,
7977
)
78+
_, err = tx.ExecContext(ctx, insertQuery, params...)
8079
if err != nil {
8180
return PollingProfile{}, fmt.Errorf("sql exec insert: %w", err)
8281
}

server/executor/testrunner/testrunner_repository.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ func (r *Repository) Update(ctx context.Context, updated TestRunner) (TestRunner
5151
}
5252

5353
defer tx.Rollback()
54-
_, err = tx.ExecContext(ctx, deleteQuery)
54+
55+
query, params := sqlutil.Tenant(ctx, deleteQuery)
56+
_, err = tx.ExecContext(ctx, query, params...)
5557
if err != nil {
5658
return TestRunner{}, fmt.Errorf("sql exec delete: %w", err)
5759
}
58-
tenantID := sqlutil.TenantID(ctx)
5960

6061
var requiredGatesJSON []byte
6162
if updated.RequiredGates != nil {
@@ -65,12 +66,12 @@ func (r *Repository) Update(ctx context.Context, updated TestRunner) (TestRunner
6566
}
6667
}
6768

68-
_, err = tx.ExecContext(ctx, insertQuery,
69+
params = sqlutil.TenantInsert(ctx,
6970
updated.ID,
7071
updated.Name,
7172
requiredGatesJSON,
72-
tenantID,
7373
)
74+
_, err = tx.ExecContext(ctx, insertQuery, params...)
7475
if err != nil {
7576
return TestRunner{}, fmt.Errorf("sql exec insert: %w", err)
7677
}

server/linter/analyzer/analyzer_repository.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,14 @@ func (r *Repository) Update(ctx context.Context, linter Linter) (Linter, error)
9090
}
9191
}
9292

93-
tenantID := sqlutil.TenantID(ctx)
94-
_, err = tx.ExecContext(
95-
ctx,
96-
insertQuery,
93+
params = sqlutil.TenantInsert(ctx,
9794
updated.ID,
9895
updated.Name,
9996
updated.Enabled,
10097
updated.MinimumScore,
10198
pluginsJSON,
102-
tenantID,
10399
)
100+
_, err = tx.ExecContext(ctx, insertQuery, params...)
104101
if err != nil {
105102
return Linter{}, fmt.Errorf("sql exec insert: %w", err)
106103
}
@@ -125,7 +122,8 @@ func (r *Repository) Delete(ctx context.Context, id id.ID) error {
125122
}
126123
defer tx.Rollback()
127124

128-
_, err = tx.ExecContext(ctx, deleteQuery, id)
125+
query, params := sqlutil.Tenant(ctx, deleteQuery, id)
126+
_, err = tx.ExecContext(ctx, query, params...)
129127
if err != nil {
130128
return fmt.Errorf("sql error: %w", err)
131129
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
BEGIN;
2+
3+
ALTER TABLE data_stores
4+
DROP CONSTRAINT data_stores_pkey,
5+
ADD PRIMARY KEY (id),
6+
ALTER COLUMN tenant_id DROP DEFAULT,
7+
ALTER COLUMN tenant_id DROP NOT NULL;
8+
9+
UPDATE data_stores
10+
SET tenant_id = null
11+
WHERE tenant_id = '';
12+
13+
ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid;
14+
15+
16+
ALTER TABLE demos
17+
DROP CONSTRAINT demos_pkey,
18+
ADD PRIMARY KEY (id),
19+
ALTER COLUMN tenant_id DROP DEFAULT,
20+
ALTER COLUMN tenant_id DROP NOT NULL;
21+
22+
UPDATE demos
23+
SET tenant_id = null
24+
WHERE tenant_id = '';
25+
26+
ALTER TABLE demos ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid;
27+
28+
29+
ALTER TABLE polling_profiles
30+
DROP CONSTRAINT polling_profiles_pkey,
31+
ADD PRIMARY KEY (id),
32+
ALTER COLUMN tenant_id DROP DEFAULT,
33+
ALTER COLUMN tenant_id DROP NOT NULL;
34+
35+
UPDATE polling_profiles
36+
SET tenant_id = null
37+
WHERE tenant_id = '';
38+
39+
ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid;
40+
41+
42+
ALTER TABLE linters
43+
DROP CONSTRAINT linters_pkey,
44+
ADD PRIMARY KEY (id),
45+
ALTER COLUMN tenant_id DROP DEFAULT,
46+
ALTER COLUMN tenant_id DROP NOT NULL;
47+
48+
UPDATE linters
49+
SET tenant_id = null
50+
WHERE tenant_id = '';
51+
52+
ALTER TABLE linters ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid;
53+
54+
55+
ALTER TABLE test_runners
56+
DROP CONSTRAINT test_runners_pkey,
57+
ADD PRIMARY KEY (id),
58+
ALTER COLUMN tenant_id DROP DEFAULT,
59+
ALTER COLUMN tenant_id DROP NOT NULL;
60+
61+
UPDATE test_runners
62+
SET tenant_id = null
63+
WHERE tenant_id = '';
64+
65+
ALTER TABLE test_runners ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid;
66+
67+
COMMIT;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
BEGIN;
2+
3+
ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE varchar;
4+
5+
UPDATE data_stores
6+
SET tenant_id = ''
7+
WHERE tenant_id is null;
8+
9+
ALTER TABLE data_stores
10+
DROP CONSTRAINT data_stores_pkey,
11+
ADD PRIMARY KEY (id, tenant_id),
12+
ALTER COLUMN tenant_id SET DEFAULT '';
13+
14+
15+
ALTER TABLE demos ALTER COLUMN tenant_id TYPE varchar;
16+
17+
UPDATE demos
18+
SET tenant_id = ''
19+
WHERE tenant_id is null;
20+
21+
ALTER TABLE demos
22+
DROP CONSTRAINT demos_pkey,
23+
ADD PRIMARY KEY (id, tenant_id),
24+
ALTER COLUMN tenant_id SET DEFAULT '';
25+
26+
27+
ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE varchar;
28+
29+
UPDATE polling_profiles
30+
SET tenant_id = ''
31+
WHERE tenant_id is null;
32+
33+
ALTER TABLE polling_profiles
34+
DROP CONSTRAINT polling_profiles_pkey,
35+
ADD PRIMARY KEY (id, tenant_id),
36+
ALTER COLUMN tenant_id SET DEFAULT '';
37+
38+
39+
ALTER TABLE linters ALTER COLUMN tenant_id TYPE varchar;
40+
41+
UPDATE linters
42+
SET tenant_id = ''
43+
WHERE tenant_id is null;
44+
45+
ALTER TABLE linters
46+
DROP CONSTRAINT linters_pkey,
47+
ADD PRIMARY KEY (id, tenant_id),
48+
ALTER COLUMN tenant_id SET DEFAULT '';
49+
50+
51+
ALTER TABLE test_runners ALTER COLUMN tenant_id TYPE varchar;
52+
53+
UPDATE test_runners
54+
SET tenant_id = ''
55+
WHERE tenant_id is null;
56+
57+
ALTER TABLE test_runners
58+
DROP CONSTRAINT test_runners_pkey,
59+
ADD PRIMARY KEY (id, tenant_id),
60+
ALTER COLUMN tenant_id SET DEFAULT '';
61+
62+
COMMIT;

server/pkg/sqlutil/tenant.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params .
3535
return query + condition, append(params, *tenantID)
3636
}
3737

38+
func TenantInsert(ctx context.Context, params ...any) []any {
39+
tenantID := TenantID(ctx)
40+
if tenantID == nil {
41+
return append(params, "")
42+
}
43+
44+
return append(params, *tenantID)
45+
}
46+
3847
func TenantID(ctx context.Context) *string {
3948
tenantID := ctx.Value(middleware.TenantIDKey)
4049

0 commit comments

Comments
 (0)