diff --git a/database/migrations/20241202153119_create_queries_table.up.sql b/database/migrations/20241202153119_create_queries_table.up.sql index 0081c33e..18ab6203 100644 --- a/database/migrations/20241202153119_create_queries_table.up.sql +++ b/database/migrations/20241202153119_create_queries_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE queries ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), latestVersion int not null default 1, activeVersion int not null default 1, type queryType not null diff --git a/database/migrations/20241218183030_create_collectors_table.up.sql b/database/migrations/20241218183030_create_collectors_table.up.sql index 76d1d854..b5b95ae3 100644 --- a/database/migrations/20241218183030_create_collectors_table.up.sql +++ b/database/migrations/20241218183030_create_collectors_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE collectors ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), jobId uuid not null, minCleanVersion int not null default 1, minTextVersion int not null default 1, diff --git a/database/migrations/20241218183541_create_collector_queries_table.up.sql b/database/migrations/20241218183541_create_collector_queries_table.up.sql index 3b56bd0a..48aa61f8 100644 --- a/database/migrations/20241218183541_create_collector_queries_table.up.sql +++ b/database/migrations/20241218183541_create_collector_queries_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE collectorQueries ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), collectorId uuid not null, name varchar(255) not null, queryId uuid not null, diff --git a/database/migrations/20241219130217_create_result_table.up.sql b/database/migrations/20241219130217_create_result_table.up.sql index 7230add9..a44152c4 100644 --- a/database/migrations/20241219130217_create_result_table.up.sql +++ b/database/migrations/20241219130217_create_result_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE results ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), queryId uuid not null, documentId uuid not null, value TEXT not null, diff --git a/database/migrations/20241219140508_create_required_query_table.up.sql b/database/migrations/20241219140508_create_required_query_table.up.sql index 4fd525fe..6105256b 100644 --- a/database/migrations/20241219140508_create_required_query_table.up.sql +++ b/database/migrations/20241219140508_create_required_query_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE requiredQueries ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), queryId uuid not null, requiredQueryId uuid not null, addedVersion int not null, diff --git a/database/migrations/20241223113819_create_queryconfig_table.up.sql b/database/migrations/20241223113819_create_queryconfig_table.up.sql index 069b425d..0e56425e 100644 --- a/database/migrations/20241223113819_create_queryconfig_table.up.sql +++ b/database/migrations/20241223113819_create_queryconfig_table.up.sql @@ -1,5 +1,5 @@ CREATE TABLE queryConfigs ( - id uuid primary key, + id uuid primary key DEFAULT gen_random_uuid(), queryId uuid not null, config jsonb not null, addedVersion int not null, diff --git a/database/queries/result.sql b/database/queries/result.sql index 07c67bf1..20cdeeb4 100644 --- a/database/queries/result.sql +++ b/database/queries/result.sql @@ -4,5 +4,5 @@ SELECT id, queryId, queryVersion FROM results where documentId = $1 and cleanVer -- name: ListResultValuesByID :many SELECT id, queryId, value FROM results where id = ANY($1); --- name: SetResult :exec -INSERT INTO results (id, queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6, $7); \ No newline at end of file +-- name: SetResult :one +INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id; \ No newline at end of file diff --git a/internal/database/repository/result.sql.go b/internal/database/repository/result.sql.go index fcacc232..438f902c 100644 --- a/internal/database/repository/result.sql.go +++ b/internal/database/repository/result.sql.go @@ -77,12 +77,11 @@ func (q *Queries) ListResultsByDocumentID(ctx context.Context, arg ListResultsBy return items, nil } -const setResult = `-- name: SetResult :exec -INSERT INTO results (id, queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6, $7) +const setResult = `-- name: SetResult :one +INSERT INTO results (queryId, documentId, value, cleanVersion, textVersion, queryVersion) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id ` type SetResultParams struct { - ID pgtype.UUID Queryid pgtype.UUID Documentid pgtype.UUID Value string @@ -91,9 +90,8 @@ type SetResultParams struct { Queryversion int32 } -func (q *Queries) SetResult(ctx context.Context, arg SetResultParams) error { - _, err := q.db.Exec(ctx, setResult, - arg.ID, +func (q *Queries) SetResult(ctx context.Context, arg SetResultParams) (pgtype.UUID, error) { + row := q.db.QueryRow(ctx, setResult, arg.Queryid, arg.Documentid, arg.Value, @@ -101,5 +99,7 @@ func (q *Queries) SetResult(ctx context.Context, arg SetResultParams) error { arg.Textversion, arg.Queryversion, ) - return err + var id pgtype.UUID + err := row.Scan(&id) + return id, err } diff --git a/internal/result/service.go b/internal/result/service.go index b0ce5922..75c67fc6 100644 --- a/internal/result/service.go +++ b/internal/result/service.go @@ -28,10 +28,7 @@ type ResultStore struct { } func Store(ctx context.Context, db *repository.Queries, res *ResultStore) (uuid.UUID, error) { - id := uuid.New() - - err := db.SetResult(ctx, repository.SetResultParams{ - ID: database.MustToDBUUID(id), + dbId, err := db.SetResult(ctx, repository.SetResultParams{ Queryid: database.MustToDBUUID(res.QueryID), Documentid: database.MustToDBUUID(res.DocumentID), Value: res.Value, @@ -43,5 +40,7 @@ func Store(ctx context.Context, db *repository.Queries, res *ResultStore) (uuid. return uuid.Nil, err } + id := database.MustToUUID(dbId) + return id, nil }