From 80164f7d19d3c71729bd77ba2de2a44aa1e1d3b8 Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Wed, 13 Sep 2023 15:11:56 -0600 Subject: [PATCH 1/7] wip --- server/datastore/datastore_repository.go | 3 ++- server/migrations/33_add_composite_pkey.down.sql | 3 +++ server/migrations/33_add_composite_pkey.up.sql | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 server/migrations/33_add_composite_pkey.down.sql create mode 100644 server/migrations/33_add_composite_pkey.up.sql diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index d5f0df2d28..a9ed1c8b0e 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) } 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..a9d31a009e --- /dev/null +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -0,0 +1,3 @@ +ALTER TABLE data_stores +DROP CONSTRAINT data_stores_pkey, +ADD PRIMARY KEY (id); \ 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..50be1e0988 --- /dev/null +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -0,0 +1,3 @@ +ALTER TABLE data_stores +DROP CONSTRAINT data_stores_pkey, +ADD PRIMARY KEY (id, tenant_id); \ No newline at end of file From 8ef6441737ab53cb74594a35cedcfcc1d3a6e191 Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Wed, 13 Sep 2023 16:13:31 -0600 Subject: [PATCH 2/7] wip --- server/datastore/datastore_repository.go | 9 ++++++--- server/migrations/33_add_composite_pkey.down.sql | 3 --- server/migrations/33_add_composite_pkey.up.sql | 3 --- server/pkg/sqlutil/tenant.go | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 9 deletions(-) delete mode 100644 server/migrations/33_add_composite_pkey.down.sql delete mode 100644 server/migrations/33_add_composite_pkey.up.sql diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index a9ed1c8b0e..2e6f143154 100644 --- a/server/datastore/datastore_repository.go +++ b/server/datastore/datastore_repository.go @@ -91,7 +91,7 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore } defer tx.Rollback() - query, params := sqlutil.Tenant(ctx, deleteQuery, DataStoreSingleID) + query, params := sqlutil.TenantWithReplacedID(ctx, deleteQuery, DataStoreSingleID) _, err = tx.ExecContext(ctx, query, params...) if err != nil { return DataStore{}, fmt.Errorf("datastore repository sql exec delete: %w", err) @@ -103,6 +103,9 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore } tenantID := sqlutil.TenantID(ctx) + if tenantID != nil { + dataStore.ID = id.ID(*tenantID) + } _, err = tx.ExecContext(ctx, insertQuery, dataStore.ID, @@ -132,7 +135,7 @@ func (r *Repository) Delete(ctx context.Context, id id.ID) error { } defer tx.Rollback() - query, params := sqlutil.Tenant(ctx, deleteQuery, id) + query, params := sqlutil.TenantWithReplacedID(ctx, deleteQuery, id) _, err = tx.ExecContext(ctx, query, params...) if err != nil { return fmt.Errorf("datastore repository sql exec delete: %w", err) @@ -167,7 +170,7 @@ func (r *Repository) Current(ctx context.Context) (DataStore, error) { } func (r *Repository) Get(ctx context.Context, id id.ID) (DataStore, error) { - query, params := sqlutil.Tenant(ctx, getQuery, id) + query, params := sqlutil.TenantWithReplacedID(ctx, getQuery, id) row := r.db.QueryRowContext(ctx, query, params...) dataStore, err := r.readRow(row) diff --git a/server/migrations/33_add_composite_pkey.down.sql b/server/migrations/33_add_composite_pkey.down.sql deleted file mode 100644 index a9d31a009e..0000000000 --- a/server/migrations/33_add_composite_pkey.down.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE data_stores -DROP CONSTRAINT data_stores_pkey, -ADD PRIMARY KEY (id); \ 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 deleted file mode 100644 index 50be1e0988..0000000000 --- a/server/migrations/33_add_composite_pkey.up.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE data_stores -DROP CONSTRAINT data_stores_pkey, -ADD PRIMARY KEY (id, tenant_id); \ No newline at end of file diff --git a/server/pkg/sqlutil/tenant.go b/server/pkg/sqlutil/tenant.go index 5ee968827b..eb9d69ad0e 100644 --- a/server/pkg/sqlutil/tenant.go +++ b/server/pkg/sqlutil/tenant.go @@ -35,6 +35,20 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params . return query + condition, append(params, *tenantID) } +func TenantWithReplacedID(ctx context.Context, query string, params ...any) (string, []any) { + tenantID := TenantID(ctx) + if tenantID == nil { + return query, params + } + + prefix := getQueryPrefix(query) + paramNumber := len(params) + 1 + condition := fmt.Sprintf(" %s tenant_id = $%d", prefix, paramNumber) + + var newParams []any + return query + condition, append(newParams, *tenantID, *tenantID) +} + func TenantID(ctx context.Context) *string { tenantID := ctx.Value(middleware.TenantIDKey) From 9826e1aeb93653de9df049f5aedb5c9a8d32594a Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Wed, 13 Sep 2023 16:47:57 -0600 Subject: [PATCH 3/7] wip --- server/pkg/sqlutil/tenant.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/pkg/sqlutil/tenant.go b/server/pkg/sqlutil/tenant.go index eb9d69ad0e..2a96e3a06e 100644 --- a/server/pkg/sqlutil/tenant.go +++ b/server/pkg/sqlutil/tenant.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/kubeshop/tracetest/server/http/middleware" + "github.com/kubeshop/tracetest/server/pkg/id" ) func Tenant(ctx context.Context, query string, params ...any) (string, []any) { @@ -35,18 +36,17 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params . return query + condition, append(params, *tenantID) } -func TenantWithReplacedID(ctx context.Context, query string, params ...any) (string, []any) { +func TenantWithReplacedID(ctx context.Context, query string, id id.ID) (string, []any) { + var params []any tenantID := TenantID(ctx) if tenantID == nil { - return query, params + return query, append(params, id) } prefix := getQueryPrefix(query) - paramNumber := len(params) + 1 - condition := fmt.Sprintf(" %s tenant_id = $%d", prefix, paramNumber) + condition := fmt.Sprintf(" %s tenant_id = $%d", prefix, 2) - var newParams []any - return query + condition, append(newParams, *tenantID, *tenantID) + return query + condition, append(params, *tenantID, *tenantID) } func TenantID(ctx context.Context) *string { From 3666cf6f77e043cc36d2071b3243742b7941680f Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Thu, 14 Sep 2023 15:05:51 -0600 Subject: [PATCH 4/7] wip --- server/datastore/datastore_repository.go | 25 +++++++------------ .../migrations/33_add_composite_pkey.down.sql | 5 ++++ .../migrations/33_add_composite_pkey.up.sql | 4 +++ server/pkg/sqlutil/tenant.go | 13 +++++----- 4 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 server/migrations/33_add_composite_pkey.down.sql create mode 100644 server/migrations/33_add_composite_pkey.up.sql diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index 2e6f143154..847c3a4dc8 100644 --- a/server/datastore/datastore_repository.go +++ b/server/datastore/datastore_repository.go @@ -34,9 +34,9 @@ INSERT INTO data_stores ( "type", "is_default", "values", - "created_at", - "tenant_id" -) VALUES ($1, $2, $3, $4, $5, $6, $7)` + "created_at" + %s +) VALUES ($1, $2, $3, $4, $5, $6 %s)` const deleteQuery = `DELETE FROM data_stores WHERE "id" = $1` @@ -91,7 +91,7 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore } defer tx.Rollback() - query, params := sqlutil.TenantWithReplacedID(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) @@ -102,20 +102,13 @@ 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) - if tenantID != nil { - dataStore.ID = id.ID(*tenantID) - } - - _, err = tx.ExecContext(ctx, insertQuery, - dataStore.ID, + query, params = sqlutil.TenantInsert(ctx, insertQuery, dataStore.ID, dataStore.Name, dataStore.Type, dataStore.Default, valuesJSON, - dataStore.CreatedAt, - tenantID, - ) + dataStore.CreatedAt) + _, err = tx.ExecContext(ctx, query, params...) if err != nil { return DataStore{}, fmt.Errorf("datastore repository sql exec create: %w", err) } @@ -135,7 +128,7 @@ func (r *Repository) Delete(ctx context.Context, id id.ID) error { } defer tx.Rollback() - query, params := sqlutil.TenantWithReplacedID(ctx, deleteQuery, id) + query, params := sqlutil.Tenant(ctx, deleteQuery, id) _, err = tx.ExecContext(ctx, query, params...) if err != nil { return fmt.Errorf("datastore repository sql exec delete: %w", err) @@ -170,7 +163,7 @@ func (r *Repository) Current(ctx context.Context) (DataStore, error) { } func (r *Repository) Get(ctx context.Context, id id.ID) (DataStore, error) { - query, params := sqlutil.TenantWithReplacedID(ctx, getQuery, id) + query, params := sqlutil.Tenant(ctx, getQuery, id) row := r.db.QueryRowContext(ctx, query, params...) dataStore, err := r.readRow(row) 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..e93a044022 --- /dev/null +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -0,0 +1,5 @@ +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; \ 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..2cf49f9d12 --- /dev/null +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -0,0 +1,4 @@ +ALTER TABLE data_stores +DROP CONSTRAINT data_stores_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT '00000000-0000-0000-0000-000000000000'; \ No newline at end of file diff --git a/server/pkg/sqlutil/tenant.go b/server/pkg/sqlutil/tenant.go index 2a96e3a06e..f2de60643e 100644 --- a/server/pkg/sqlutil/tenant.go +++ b/server/pkg/sqlutil/tenant.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/kubeshop/tracetest/server/http/middleware" - "github.com/kubeshop/tracetest/server/pkg/id" ) func Tenant(ctx context.Context, query string, params ...any) (string, []any) { @@ -36,17 +35,17 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params . return query + condition, append(params, *tenantID) } -func TenantWithReplacedID(ctx context.Context, query string, id id.ID) (string, []any) { - var params []any +func TenantInsert(ctx context.Context, query string, params ...any) (string, []any) { tenantID := TenantID(ctx) if tenantID == nil { - return query, append(params, id) + return fmt.Sprintf(query, "", ""), params } - prefix := getQueryPrefix(query) - condition := fmt.Sprintf(" %s tenant_id = $%d", prefix, 2) + paramNumber := len(params) + 1 + paramValue := fmt.Sprintf(", $%d", paramNumber) + formattedQuery := fmt.Sprintf(query, ", tenant_id", paramValue) - return query + condition, append(params, *tenantID, *tenantID) + return formattedQuery, append(params, *tenantID) } func TenantID(ctx context.Context) *string { From 97a1d9ffae301f829cad26a2f2407e57fa6ee567 Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Fri, 15 Sep 2023 12:11:44 -0600 Subject: [PATCH 5/7] wip --- server/datastore/datastore_repository.go | 6 ++- .../polling_profile_repository.go | 11 +++-- .../testrunner/testrunner_repository.go | 15 ++++--- server/linter/analyzer/analyzer_repository.go | 16 +++---- .../migrations/33_add_composite_pkey.down.sql | 43 ++++++++++++++++++- .../migrations/33_add_composite_pkey.up.sql | 40 ++++++++++++++++- 6 files changed, 105 insertions(+), 26 deletions(-) diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index 847c3a4dc8..c09624e9d2 100644 --- a/server/datastore/datastore_repository.go +++ b/server/datastore/datastore_repository.go @@ -102,12 +102,14 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore return DataStore{}, fmt.Errorf("could not marshal values field configuration: %w", err) } - query, params = sqlutil.TenantInsert(ctx, insertQuery, dataStore.ID, + query, params = sqlutil.TenantInsert(ctx, insertQuery, + dataStore.ID, dataStore.Name, dataStore.Type, dataStore.Default, valuesJSON, - dataStore.CreatedAt) + dataStore.CreatedAt, + ) _, err = tx.ExecContext(ctx, query, 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..f0217079cd 100644 --- a/server/executor/pollingprofile/polling_profile_repository.go +++ b/server/executor/pollingprofile/polling_profile_repository.go @@ -36,10 +36,10 @@ const ( "name", "default", "strategy", - "periodic", - "tenant_id" + "periodic" + %s ) - VALUES ($1, $2, $3, $4, $5, $6)` + VALUES ($1, $2, $3, $4, $5 %s)` deleteQuery = `DELETE FROM polling_profiles` ) @@ -68,15 +68,14 @@ func (r *Repository) Update(ctx context.Context, updated PollingProfile) (Pollin } } - tenantID := sqlutil.TenantID(ctx) - _, err = tx.ExecContext(ctx, insertQuery, + query, params = sqlutil.TenantInsert(ctx, insertQuery, updated.ID, updated.Name, updated.Default, updated.Strategy, periodicJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, query, 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..8eaa7db0ec 100644 --- a/server/executor/testrunner/testrunner_repository.go +++ b/server/executor/testrunner/testrunner_repository.go @@ -34,10 +34,10 @@ const ( INSERT INTO test_runners( "id", "name", - "required_gates", - "tenant_id" + "required_gates" + %s ) - VALUES ($1, $2, $3, $4)` + VALUES ($1, $2, $3 %s)` deleteQuery = `DELETE FROM test_runners` ) @@ -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, + query, params = sqlutil.TenantInsert(ctx, insertQuery, updated.ID, updated.Name, requiredGatesJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, query, 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..b395e70b30 100644 --- a/server/linter/analyzer/analyzer_repository.go +++ b/server/linter/analyzer/analyzer_repository.go @@ -28,9 +28,9 @@ const ( "name", "enabled", "minimum_score", - "plugins", - "tenant_id" - ) VALUES ($1, $2, $3, $4, $5, $6)` + "plugins" + %s + ) VALUES ($1, $2, $3, $4, $5 %s)` getQuery = ` SELECT @@ -90,17 +90,14 @@ func (r *Repository) Update(ctx context.Context, linter Linter) (Linter, error) } } - tenantID := sqlutil.TenantID(ctx) - _, err = tx.ExecContext( - ctx, - insertQuery, + query, params = sqlutil.TenantInsert(ctx, insertQuery, updated.ID, updated.Name, updated.Enabled, updated.MinimumScore, pluginsJSON, - tenantID, ) + _, err = tx.ExecContext(ctx, query, 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 index e93a044022..835b881177 100644 --- a/server/migrations/33_add_composite_pkey.down.sql +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -1,5 +1,46 @@ +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; \ No newline at end of file +ALTER COLUMN tenant_id DROP NOT NULL; + +UPDATE data_stores +SET tenant_id = null +WHERE tenant_id = '00000000-0000-0000-0000-000000000000'; + + +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 = '00000000-0000-0000-0000-000000000000'; + + +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 = '00000000-0000-0000-0000-000000000000'; + + +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 = '00000000-0000-0000-0000-000000000000'; + +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 index 2cf49f9d12..0118859af7 100644 --- a/server/migrations/33_add_composite_pkey.up.sql +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -1,4 +1,42 @@ +BEGIN; + +UPDATE data_stores +SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; \ No newline at end of file +ALTER COLUMN tenant_id SET DEFAULT '00000000-0000-0000-0000-000000000000'; + + +UPDATE polling_profiles +SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; + + +UPDATE linters +SET tenant_id = '00000000-0000-0000-0000-000000000000' +WHERE tenant_id is null; + +ALTER TABLE linters +DROP CONSTRAINT linters_pkey, +ADD PRIMARY KEY (id, tenant_id), +ALTER COLUMN tenant_id SET DEFAULT '00000000-0000-0000-0000-000000000000'; + + +UPDATE test_runners +SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; + +COMMIT; \ No newline at end of file From f7eea002de7a18261cc130b9217d0987644cc33e Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Mon, 18 Sep 2023 08:35:00 -0600 Subject: [PATCH 6/7] wip --- server/datastore/datastore_repository.go | 10 ++++---- .../polling_profile_repository.go | 10 ++++---- .../testrunner/testrunner_repository.go | 10 ++++---- server/linter/analyzer/analyzer_repository.go | 10 ++++---- .../migrations/33_add_composite_pkey.down.sql | 16 +++++++++---- .../migrations/33_add_composite_pkey.up.sql | 24 ++++++++++++------- server/pkg/sqlutil/tenant.go | 10 +++----- 7 files changed, 51 insertions(+), 39 deletions(-) diff --git a/server/datastore/datastore_repository.go b/server/datastore/datastore_repository.go index c09624e9d2..eb7424fb00 100644 --- a/server/datastore/datastore_repository.go +++ b/server/datastore/datastore_repository.go @@ -34,9 +34,9 @@ INSERT INTO data_stores ( "type", "is_default", "values", - "created_at" - %s -) VALUES ($1, $2, $3, $4, $5, $6 %s)` + "created_at", + "tenant_id" +) VALUES ($1, $2, $3, $4, $5, $6, $7)` const deleteQuery = `DELETE FROM data_stores WHERE "id" = $1` @@ -102,7 +102,7 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore return DataStore{}, fmt.Errorf("could not marshal values field configuration: %w", err) } - query, params = sqlutil.TenantInsert(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, dataStore.ID, dataStore.Name, dataStore.Type, @@ -110,7 +110,7 @@ func (r *Repository) Update(ctx context.Context, dataStore DataStore) (DataStore valuesJSON, dataStore.CreatedAt, ) - _, err = tx.ExecContext(ctx, query, params...) + _, 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 f0217079cd..8ae50d60e6 100644 --- a/server/executor/pollingprofile/polling_profile_repository.go +++ b/server/executor/pollingprofile/polling_profile_repository.go @@ -36,10 +36,10 @@ const ( "name", "default", "strategy", - "periodic" - %s + "periodic", + "tenant_id" ) - VALUES ($1, $2, $3, $4, $5 %s)` + VALUES ($1, $2, $3, $4, $5, $6)` deleteQuery = `DELETE FROM polling_profiles` ) @@ -68,14 +68,14 @@ func (r *Repository) Update(ctx context.Context, updated PollingProfile) (Pollin } } - query, params = sqlutil.TenantInsert(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, updated.Default, updated.Strategy, periodicJSON, ) - _, err = tx.ExecContext(ctx, query, params...) + _, 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 8eaa7db0ec..cc41b10a6f 100644 --- a/server/executor/testrunner/testrunner_repository.go +++ b/server/executor/testrunner/testrunner_repository.go @@ -34,10 +34,10 @@ const ( INSERT INTO test_runners( "id", "name", - "required_gates" - %s + "required_gates", + "tenant_id" ) - VALUES ($1, $2, $3 %s)` + VALUES ($1, $2, $3, $4)` deleteQuery = `DELETE FROM test_runners` ) @@ -66,12 +66,12 @@ func (r *Repository) Update(ctx context.Context, updated TestRunner) (TestRunner } } - query, params = sqlutil.TenantInsert(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, requiredGatesJSON, ) - _, err = tx.ExecContext(ctx, query, params...) + _, 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 b395e70b30..d0eb5586a5 100644 --- a/server/linter/analyzer/analyzer_repository.go +++ b/server/linter/analyzer/analyzer_repository.go @@ -28,9 +28,9 @@ const ( "name", "enabled", "minimum_score", - "plugins" - %s - ) VALUES ($1, $2, $3, $4, $5 %s)` + "plugins", + "tenant_id" + ) VALUES ($1, $2, $3, $4, $5, $6)` getQuery = ` SELECT @@ -90,14 +90,14 @@ func (r *Repository) Update(ctx context.Context, linter Linter) (Linter, error) } } - query, params = sqlutil.TenantInsert(ctx, insertQuery, + params = sqlutil.TenantInsert(ctx, updated.ID, updated.Name, updated.Enabled, updated.MinimumScore, pluginsJSON, ) - _, err = tx.ExecContext(ctx, query, params...) + _, err = tx.ExecContext(ctx, insertQuery, params...) if err != nil { return Linter{}, fmt.Errorf("sql exec insert: %w", err) } diff --git a/server/migrations/33_add_composite_pkey.down.sql b/server/migrations/33_add_composite_pkey.down.sql index 835b881177..036b726aed 100644 --- a/server/migrations/33_add_composite_pkey.down.sql +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -8,7 +8,9 @@ ALTER COLUMN tenant_id DROP NOT NULL; UPDATE data_stores SET tenant_id = null -WHERE tenant_id = '00000000-0000-0000-0000-000000000000'; +WHERE tenant_id = ''; + +ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; ALTER TABLE polling_profiles @@ -19,7 +21,9 @@ ALTER COLUMN tenant_id DROP NOT NULL; UPDATE polling_profiles SET tenant_id = null -WHERE tenant_id = '00000000-0000-0000-0000-000000000000'; +WHERE tenant_id = ''; + +ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; ALTER TABLE linters @@ -30,7 +34,9 @@ ALTER COLUMN tenant_id DROP NOT NULL; UPDATE linters SET tenant_id = null -WHERE tenant_id = '00000000-0000-0000-0000-000000000000'; +WHERE tenant_id = ''; + +ALTER TABLE linters ALTER COLUMN tenant_id TYPE uuid using tenant_id::uuid; ALTER TABLE test_runners @@ -41,6 +47,8 @@ ALTER COLUMN tenant_id DROP NOT NULL; UPDATE test_runners SET tenant_id = null -WHERE tenant_id = '00000000-0000-0000-0000-000000000000'; +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 index 0118859af7..d81d5686cf 100644 --- a/server/migrations/33_add_composite_pkey.up.sql +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -1,42 +1,50 @@ BEGIN; +ALTER TABLE data_stores ALTER COLUMN tenant_id TYPE varchar; + UPDATE data_stores -SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; +ALTER COLUMN tenant_id SET DEFAULT ''; + +ALTER TABLE polling_profiles ALTER COLUMN tenant_id TYPE varchar; UPDATE polling_profiles -SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; +ALTER COLUMN tenant_id SET DEFAULT ''; +ALTER TABLE linters ALTER COLUMN tenant_id TYPE varchar; + UPDATE linters -SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; +ALTER COLUMN tenant_id SET DEFAULT ''; + +ALTER TABLE test_runners ALTER COLUMN tenant_id TYPE varchar; UPDATE test_runners -SET tenant_id = '00000000-0000-0000-0000-000000000000' +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 '00000000-0000-0000-0000-000000000000'; +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 f2de60643e..2ac8dd290c 100644 --- a/server/pkg/sqlutil/tenant.go +++ b/server/pkg/sqlutil/tenant.go @@ -35,17 +35,13 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params . return query + condition, append(params, *tenantID) } -func TenantInsert(ctx context.Context, query string, params ...any) (string, []any) { +func TenantInsert(ctx context.Context, params ...any) []any { tenantID := TenantID(ctx) if tenantID == nil { - return fmt.Sprintf(query, "", ""), params + return append(params, "") } - paramNumber := len(params) + 1 - paramValue := fmt.Sprintf(", $%d", paramNumber) - formattedQuery := fmt.Sprintf(query, ", tenant_id", paramValue) - - return formattedQuery, append(params, *tenantID) + return append(params, *tenantID) } func TenantID(ctx context.Context) *string { From be46e2a5a38932e19232ab4e860301ef4bd70b37 Mon Sep 17 00:00:00 2001 From: Jorge Padilla Date: Mon, 18 Sep 2023 09:12:52 -0600 Subject: [PATCH 7/7] fix demos --- server/config/demo/demo_repository.go | 6 ++---- server/migrations/33_add_composite_pkey.down.sql | 13 +++++++++++++ server/migrations/33_add_composite_pkey.up.sql | 12 ++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) 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/migrations/33_add_composite_pkey.down.sql b/server/migrations/33_add_composite_pkey.down.sql index 036b726aed..cf8eb08799 100644 --- a/server/migrations/33_add_composite_pkey.down.sql +++ b/server/migrations/33_add_composite_pkey.down.sql @@ -13,6 +13,19 @@ 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), diff --git a/server/migrations/33_add_composite_pkey.up.sql b/server/migrations/33_add_composite_pkey.up.sql index d81d5686cf..ecebab6d72 100644 --- a/server/migrations/33_add_composite_pkey.up.sql +++ b/server/migrations/33_add_composite_pkey.up.sql @@ -12,6 +12,18 @@ 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