Skip to content

Commit c5e9c36

Browse files
committed
Fix race condition
1 parent 723f221 commit c5e9c36

File tree

12 files changed

+384
-186
lines changed

12 files changed

+384
-186
lines changed

Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ tasks:
177177
silent: true
178178
cmds:
179179
# We have to use ldflags to avoid the LC_DYSYMTAB linker warning.
180-
- go test -ldflags=-extldflags=-Wl,-w -race -json ./... | gotestfmt -hide "all"
180+
- go test -count 1 -ldflags=-extldflags=-Wl,-w -race -json ./... | gotestfmt -hide "all"
181181

182182
test-coverage:
183183
desc: Run registry API tests with coverage
@@ -186,7 +186,7 @@ tasks:
186186
silent: true
187187
cmds:
188188
# We have to use ldflags to avoid the LC_DYSYMTAB linker warning.
189-
- go test -ldflags=-extldflags=-Wl,-w -race -json -coverprofile=coverage.out ./... | gotestfmt -hide "all"
189+
- go test -count 1 -ldflags=-extldflags=-Wl,-w -race -json -coverprofile=coverage.out ./... | gotestfmt -hide "all"
190190
- go tool cover -func=coverage.out
191191
- echo "Generating HTML coverage report in coverage.html"
192192
- go tool cover -html=coverage.out -o coverage.html

database/migrate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestMigrations(t *testing.T) {
1313
t.Parallel()
1414

1515
ctx := context.Background()
16-
db, cleanupFunc := SetupTestDBContaienr(t, ctx)
16+
db, cleanupFunc := SetupTestDBContainer(t, ctx)
1717
t.Cleanup(cleanupFunc)
1818

1919
connString := db.Config().ConnString()

database/queries/registry.sql

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ SELECT id,
66
updated_at
77
FROM registry
88
WHERE (sqlc.narg(next)::timestamp with time zone IS NULL OR created_at > sqlc.narg(next))
9-
OR (sqlc.narg(prev)::timestamp with time zone IS NULL AND created_at < sqlc.narg(prev))
9+
AND (sqlc.narg(prev)::timestamp with time zone IS NULL OR created_at < sqlc.narg(prev))
1010
ORDER BY
1111
-- next page sorting
1212
CASE WHEN sqlc.narg(next)::timestamp with time zone IS NULL THEN created_at END ASC,
13+
CASE WHEN sqlc.narg(next)::timestamp with time zone IS NULL THEN name END ASC,
1314
-- previous page sorting
14-
CASE WHEN sqlc.narg(prev)::timestamp with time zone IS NULL THEN created_at END DESC
15+
CASE WHEN sqlc.narg(prev)::timestamp with time zone IS NULL THEN created_at END DESC,
16+
CASE WHEN sqlc.narg(prev)::timestamp with time zone IS NULL THEN name END DESC
1517
LIMIT sqlc.arg(size)::bigint;
1618

1719
-- name: GetRegistry :one
@@ -24,4 +26,14 @@ SELECT id,
2426
WHERE id = sqlc.arg(id);
2527

2628
-- name: InsertRegistry :one
27-
INSERT INTO registry (name, reg_type) VALUES ($1, $2) RETURNING id;
29+
INSERT INTO registry (
30+
name,
31+
reg_type,
32+
created_at,
33+
updated_at
34+
) VALUES (
35+
sqlc.arg(name),
36+
sqlc.arg(reg_type),
37+
sqlc.arg(created_at),
38+
sqlc.arg(updated_at)
39+
) RETURNING id;

database/queries/servers.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SELECT r.reg_type as registry_type,
1919
JOIN registry r ON s.reg_id = r.id
2020
LEFT JOIN latest_server_version l ON s.id = l.latest_server_id
2121
WHERE (sqlc.narg(next)::timestamp with time zone IS NULL OR s.created_at > sqlc.narg(next))
22-
OR (sqlc.narg(prev)::timestamp with time zone IS NULL AND s.created_at < sqlc.narg(prev))
22+
AND (sqlc.narg(prev)::timestamp with time zone IS NULL OR s.created_at < sqlc.narg(prev))
2323
ORDER BY
2424
-- next page sorting
2525
CASE WHEN sqlc.narg(next)::timestamp with time zone IS NULL THEN r.reg_type END ASC,
@@ -79,7 +79,7 @@ SELECT s.id,
7979
FROM mcp_server s
8080
WHERE s.name = sqlc.arg(name)
8181
AND ((sqlc.narg(next)::timestamp with time zone IS NULL OR s.created_at > sqlc.narg(next))
82-
OR (sqlc.narg(prev)::timestamp with time zone IS NULL AND s.created_at < sqlc.narg(prev)))
82+
AND (sqlc.narg(prev)::timestamp with time zone IS NULL OR s.created_at < sqlc.narg(prev)))
8383
ORDER BY
8484
CASE WHEN sqlc.narg(next)::timestamp with time zone IS NULL THEN s.created_at END ASC,
8585
CASE WHEN sqlc.narg(next)::timestamp with time zone IS NULL THEN s.version END DESC -- acts as tie breaker
@@ -105,8 +105,8 @@ INSERT INTO mcp_server (
105105
sqlc.arg(name),
106106
sqlc.arg(version),
107107
sqlc.arg(reg_id),
108-
CURRENT_TIMESTAMP,
109-
CURRENT_TIMESTAMP,
108+
sqlc.arg(created_at),
109+
sqlc.arg(updated_at),
110110
sqlc.narg(description),
111111
sqlc.narg(title),
112112
sqlc.narg(website),
@@ -118,7 +118,7 @@ INSERT INTO mcp_server (
118118
sqlc.narg(repository_type)
119119
) ON CONFLICT (reg_id, name, version)
120120
DO UPDATE SET
121-
updated_at = CURRENT_TIMESTAMP,
121+
updated_at = sqlc.arg(updated_at),
122122
description = sqlc.narg(description),
123123
title = sqlc.narg(title),
124124
website = sqlc.narg(website),

database/queries/sync.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ INSERT INTO registry_sync (
1818
sqlc.arg(reg_id),
1919
sqlc.arg(sync_status),
2020
sqlc.narg(error_msg),
21-
CURRENT_TIMESTAMP
21+
sqlc.arg(started_at)
2222
) RETURNING id;
2323

2424
-- name: UpdateRegistrySync :exec

database/testcontainers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ var (
2323
dbPass = "testpass"
2424
)
2525

26-
// SetupTestDBContaienr creates a Postgres container using testcontainers and returns a connection to the database
26+
// SetupTestDBContainer creates a Postgres container using testcontainers and returns a connection to the database
2727
//
2828
//nolint:revive
29-
func SetupTestDBContaienr(t *testing.T, ctx context.Context) (*pgx.Conn, func()) {
29+
func SetupTestDBContainer(t *testing.T, ctx context.Context) (*pgx.Conn, func()) {
3030
t.Helper()
3131

3232
// Start Postgres container
@@ -58,7 +58,7 @@ func SetupTestDB(t *testing.T) (*pgx.Conn, func()) {
5858
t.Helper()
5959

6060
ctx := context.Background()
61-
db, containerCleanupFunc := SetupTestDBContaienr(t, ctx)
61+
db, containerCleanupFunc := SetupTestDBContainer(t, ctx)
6262

6363
// Apply migrations
6464
err := MigrateUp(ctx, db)

internal/db/sqlc/registry.sql.go

Lines changed: 25 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)