Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc committed Sep 18, 2023
1 parent 97a1d9f commit f7eea00
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 39 deletions.
10 changes: 5 additions & 5 deletions server/datastore/datastore_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -102,15 +102,15 @@ 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,
dataStore.Default,
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)
}
Expand Down
10 changes: 5 additions & 5 deletions server/executor/pollingprofile/polling_profile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)

Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions server/executor/testrunner/testrunner_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)

Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions server/linter/analyzer/analyzer_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 12 additions & 4 deletions server/migrations/33_add_composite_pkey.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
24 changes: 16 additions & 8 deletions server/migrations/33_add_composite_pkey.up.sql
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 3 additions & 7 deletions server/pkg/sqlutil/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f7eea00

Please sign in to comment.