Skip to content

Commit 110f058

Browse files
committed
Fix norawsql lint: move PRAGMA foreign_keys into dbtest helper
Extract EnableForeignKeys helper into dbtest package (which is in the norawsql allow-list) and add CreateBaseDBWithFK to testutil for reuse.
1 parent ba9c83b commit 110f058

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

packages/db/pkg/dbtest/linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ func GetTestDB(ctx context.Context) (*sql.DB, error) {
3232
return db, nil
3333
}
3434

35+
// EnableForeignKeys enables SQLite foreign key enforcement on db. It must be
36+
// called after SetMaxOpenConns(1) so the PRAGMA applies to every connection.
37+
// PRAGMA is a SQLite connection directive — there is no sqlc-generated equivalent.
38+
func EnableForeignKeys(ctx context.Context, db *sql.DB) error {
39+
_, err := db.ExecContext(ctx, "PRAGMA foreign_keys = ON")
40+
return err
41+
}
42+
3543
func GetTestPreparedQueries(ctx context.Context) (*gen.Queries, error) {
3644
// Generate unique database name for this test to ensure isolation
3745
uniqueName := ulid.Make().String()

packages/db/pkg/dbtest/windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func GetTestDB(ctx context.Context) (*sql.DB, error) {
1313
return nil, errors.New("not implemented")
1414
}
1515

16+
// EnableForeignKeys is not implemented on Windows.
17+
func EnableForeignKeys(_ context.Context, _ *sql.DB) error {
18+
return errors.New("not implemented")
19+
}
20+
1621
func GetTestPreparedQueries(ctx context.Context) (*gen.Queries, error) {
1722
return nil, errors.New("not implemented")
1823
}

packages/server/internal/api/rauthadapter/rauthadapter_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ func newHandler(t *testing.T) (AuthAdapterRPC, func()) {
2525

2626
// newHandlerWithFK is like newHandler but enables SQLite FK enforcement so that
2727
// ON DELETE CASCADE constraints fire. Tests that rely on DB-level cascade must
28-
// use this variant. PRAGMA foreign_keys is per-connection; SetMaxOpenConns(1)
29-
// ensures it applies to every query on the returned DB.
28+
// use this variant.
3029
func newHandlerWithFK(t *testing.T) (AuthAdapterRPC, func()) {
3130
t.Helper()
32-
base := testutil.CreateBaseDB(context.Background(), t)
33-
base.DB.SetMaxOpenConns(1)
34-
_, err := base.DB.ExecContext(context.Background(), "PRAGMA foreign_keys = ON")
35-
if err != nil {
36-
t.Fatal(err)
37-
}
31+
base := testutil.CreateBaseDBWithFK(context.Background(), t)
3832
adapter := authadapter.New(base.Queries, base.DB)
3933
h := New(AuthAdapterRPCDeps{Adapter: adapter})
4034
return h, base.Close

packages/server/pkg/testutil/testutil.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ func CreateBaseDB(ctx context.Context, t *testing.T) *BaseDBQueries {
4848
return &BaseDBQueries{Queries: queries, t: t, ctx: ctx, DB: db}
4949
}
5050

51+
// CreateBaseDBWithFK is like CreateBaseDB but enables SQLite foreign-key
52+
// enforcement (PRAGMA foreign_keys = ON) so that ON DELETE CASCADE constraints
53+
// fire. The connection pool is limited to one connection so the PRAGMA applies
54+
// to every query executed on the returned DB.
55+
func CreateBaseDBWithFK(ctx context.Context, t *testing.T) *BaseDBQueries {
56+
t.Helper()
57+
base := CreateBaseDB(ctx, t)
58+
base.DB.SetMaxOpenConns(1)
59+
if err := dbtest.EnableForeignKeys(ctx, base.DB); err != nil {
60+
t.Fatal(err)
61+
}
62+
return base
63+
}
64+
5165
func (c BaseDBQueries) GetBaseServices() BaseTestServices {
5266
queries := c.Queries
5367

0 commit comments

Comments
 (0)