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): take tenant_id into account for test run events #3482

Merged
merged 1 commit into from
Dec 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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