Skip to content

Commit

Permalink
fix(server): take tenant_id into account for test run events (#3482)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren authored Dec 27, 2023
1 parent d57fa67 commit 0652b3d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
8 changes: 8 additions & 0 deletions server/migrations/37_test_run_events_tenant.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BEGIN;

ALTER TABLE
test_run_events
DROP
COLUMN tenant_id;

COMMIT;
11 changes: 11 additions & 0 deletions server/migrations/37_test_run_events_tenant.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
BEGIN;

ALTER TABLE
test_run_events
ADD
COLUMN tenant_id varchar
;

CREATE INDEX idx_test_run_events_tenant_id ON test_run_events(tenant_id);

COMMIT;
43 changes: 26 additions & 17 deletions server/testdb/test_run_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/pkg/sqlutil"
)

const insertTestRunEventQuery = `
Expand All @@ -22,23 +23,30 @@ const insertTestRunEventQuery = `
"created_at",
"data_store_connection",
"polling",
"outputs"
"outputs",
"tenant_id"
) VALUES (
$1, -- test_id
$2, -- run_id
$3, -- type
$4, -- stage
$5, -- title
$6, -- description
$7, -- created_at
$8, -- data_store_connection
$9, -- polling
$10 -- outputs
$1, -- test_id
$2, -- run_id
$3, -- type
$4, -- stage
$5, -- title
$6, -- description
$7, -- created_at
$8, -- data_store_connection
$9, -- polling
$10, -- outputs
$11 -- tenant_id
)
RETURNING "id"
`

func (td *postgresDB) CreateTestRunEvent(ctx context.Context, event model.TestRunEvent) error {
stmt, err := td.db.Prepare(insertTestRunEventQuery)
if err != nil {
return fmt.Errorf("sql prepare: %w", err)
}
defer stmt.Close()

dataStoreConnectionJSON, err := json.Marshal(event.DataStoreConnection)
if err != nil {
return fmt.Errorf("could not marshal data store connection into JSON: %w", err)
Expand All @@ -58,9 +66,7 @@ func (td *postgresDB) CreateTestRunEvent(ctx context.Context, event model.TestRu
event.CreatedAt = time.Now()
}

err = td.db.QueryRowContext(
ctx,
insertTestRunEventQuery,
params := sqlutil.TenantInsert(ctx,
event.TestID,
event.RunID,
event.Type,
Expand All @@ -71,7 +77,9 @@ func (td *postgresDB) CreateTestRunEvent(ctx context.Context, event model.TestRu
dataStoreConnectionJSON,
pollingJSON,
outputsJSON,
).Scan(&event.ID)
)

_, err = stmt.ExecContext(ctx, params...)

if err != nil {
return fmt.Errorf("could not insert event into database: %w", err)
Expand All @@ -97,7 +105,8 @@ const getTestRunEventsQuery = `
`

func (td *postgresDB) GetTestRunEvents(ctx context.Context, testID id.ID, runID int) ([]model.TestRunEvent, error) {
rows, err := td.db.QueryContext(ctx, getTestRunEventsQuery, testID, runID)
query, params := sqlutil.Tenant(ctx, getTestRunEventsQuery, testID, runID)
rows, err := td.db.QueryContext(ctx, query, params...)
if err != nil {
return []model.TestRunEvent{}, fmt.Errorf("could not query test runs: %w", err)
}
Expand Down

0 comments on commit 0652b3d

Please sign in to comment.