diff --git a/internal/infrastructure/postgres/events_order_test.go b/internal/infrastructure/postgres/events_order_test.go index 054c255c1..2190c7c16 100644 --- a/internal/infrastructure/postgres/events_order_test.go +++ b/internal/infrastructure/postgres/events_order_test.go @@ -56,6 +56,9 @@ func TestEventInsertsCommitInIDOrder(t *testing.T) { time.Sleep(10 * time.Millisecond) } + if visible, err := st.EventsSince(t.Context(), "", 10); err != nil || len(visible) != 0 { + t.Fatalf("EventsSince while the first insert is uncommitted = %d events, %v; want none", len(visible), err) + } if err := tx.Commit(t.Context()); err != nil { t.Fatalf("Commit: %v", err) } @@ -66,4 +69,11 @@ func TestEventInsertsCommitInIDOrder(t *testing.T) { if r.id <= firstID { t.Fatalf("second id %d, want greater than first id %d", r.id, firstID) } + all, err := st.EventsSince(t.Context(), "", 10) + if err != nil { + t.Fatalf("EventsSince: %v", err) + } + if len(all) != 2 || all[0].Kind != "first" || all[1].Kind != "second" { + t.Fatalf("EventsSince after both commit = %+v, want first then second", all) + } } diff --git a/internal/infrastructure/postgres/ownership.go b/internal/infrastructure/postgres/ownership.go index fe8d55c83..6757a77be 100644 --- a/internal/infrastructure/postgres/ownership.go +++ b/internal/infrastructure/postgres/ownership.go @@ -31,7 +31,7 @@ func AcquireOwnership(ctx context.Context, pool *pgxpool.Pool, name string) (*Ow return nil, fmt.Errorf("postgres: ownership %s: %w", name, err) } var taken bool - if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock(hashtextextended('archie.ownership.' || $1, 0))", name).Scan(&taken); err != nil { + if err := conn.QueryRow(ctx, "SELECT pg_try_advisory_lock(hashtextextended('archie.' || $1 || '.owner', 0))", name).Scan(&taken); err != nil { conn.Release() return nil, fmt.Errorf("postgres: ownership %s: %w", name, err) } @@ -51,7 +51,7 @@ func (o *Ownership) Release(ctx context.Context) error { } conn := o.conn o.conn = nil - if _, err := conn.Exec(ctx, "SELECT pg_advisory_unlock(hashtextextended('archie.ownership.' || $1, 0))", o.name); err != nil { + if _, err := conn.Exec(ctx, "SELECT pg_advisory_unlock(hashtextextended('archie.' || $1 || '.owner', 0))", o.name); err != nil { return errors.Join(fmt.Errorf("postgres: release ownership %s: %w", o.name, err), conn.Hijack().Close(ctx)) } conn.Release() diff --git a/internal/infrastructure/postgres/postgresdb/events.sql.go b/internal/infrastructure/postgres/postgresdb/events.sql.go index 7256fba79..f9dc54797 100644 --- a/internal/infrastructure/postgres/postgresdb/events.sql.go +++ b/internal/infrastructure/postgres/postgresdb/events.sql.go @@ -13,7 +13,7 @@ import ( const insertEvent = `-- name: InsertEvent :one WITH append_lock AS ( - SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0)) + SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0)) ) INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data) SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13 diff --git a/internal/infrastructure/postgres/queries/events.sql b/internal/infrastructure/postgres/queries/events.sql index 377d7b467..936172496 100644 --- a/internal/infrastructure/postgres/queries/events.sql +++ b/internal/infrastructure/postgres/queries/events.sql @@ -9,7 +9,7 @@ -- the order they were drawn. The lock is inside the statement so a standalone -- insert and one inside a longer transaction both take it. WITH append_lock AS ( - SELECT pg_advisory_xact_lock(hashtextextended('archie.events.append', 0)) + SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0)) ) INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data) SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13