Skip to content

Commit 27bc2d8

Browse files
kyleconroyclaude
andcommitted
core: move catalog SQL into a catalogdef package, export Schema
Extract schema.sql and query.sql from internal/core (and catalogdb) into a dedicated internal/core/catalogdef package that owns the SQL defining sqlc's catalog and embeds the schema (catalogdef.Schema). sqlc reads its schema and queries from catalogdef and still generates the querier into catalogdb, keeping SQL sources separate from generated code. core/catalog.go now builds its in-memory catalog from catalogdef.Schema instead of embedding schema.sql itself, so the DDL used at runtime and the schema fed to codegen are one source of truth. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XTGxNHW6v1S1YyC9FDSgrK
1 parent afea9bc commit 27bc2d8

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

internal/core/catalog.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@ package core
88

99
import (
1010
"database/sql"
11-
_ "embed"
1211
"fmt"
1312

1413
"github.com/sqlc-dev/sqlc/internal/core/catalogdb"
14+
"github.com/sqlc-dev/sqlc/internal/core/catalogdef"
1515

1616
_ "modernc.org/sqlite"
1717
)
1818

19-
// The catalogdb package is generated by running sqlc against this
20-
// package's own schema.sql and catalogdb/query.sql — sqlc analyzing
21-
// sqlc's catalog. Regenerate with `go generate ./internal/core/...`.
19+
// The catalogdb package is generated by running sqlc against the SQL in
20+
// catalogdef (schema.sql + query.sql) — sqlc analyzing sqlc's catalog.
21+
// Regenerate with `go generate ./internal/core/...`.
2222
//
2323
//go:generate go run github.com/sqlc-dev/sqlc/cmd/sqlc generate
2424

25-
//go:embed schema.sql
26-
var ddl string
27-
2825
// Catalog is an in-memory SQLite database that stores schema metadata in
2926
// sql_* tables modeled after PostgreSQL's system catalogs.
3027
//
31-
// Queries against the sql_* tables are increasingly served by the
32-
// sqlc-generated catalogdb.Queries (see catalogdb/query.sql) rather than
33-
// hand-written SQL — sqlc analyzing sqlc's own catalog. Methods not yet
34-
// migrated fall back to the raw db handle.
28+
// Queries against the sql_* tables are served by the sqlc-generated
29+
// catalogdb.Queries (compiled from catalogdef/query.sql) — sqlc analyzing
30+
// sqlc's own catalog.
3531
type Catalog struct {
3632
db *sql.DB
3733
q *catalogdb.Queries
@@ -55,7 +51,7 @@ func New(opts ...Option) (*Catalog, error) {
5551
if err != nil {
5652
return nil, fmt.Errorf("core: open catalog: %w", err)
5753
}
58-
if _, err := db.Exec(ddl); err != nil {
54+
if _, err := db.Exec(catalogdef.Schema); err != nil {
5955
db.Close()
6056
return nil, fmt.Errorf("core: init schema: %w", err)
6157
}

internal/core/catalogdef/embed.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Package catalogdef holds the SQL that defines sqlc's internal catalog:
2+
// schema.sql (the sql_* table definitions) and query.sql (the queries
3+
// against them). Both are the input to sqlc's own code generation — sqlc
4+
// analyzing sqlc's catalog — and schema.sql is embedded here so the core
5+
// package builds its in-memory catalog from the same source of truth that
6+
// codegen reads.
7+
package catalogdef
8+
9+
import _ "embed"
10+
11+
// Schema is the DDL that creates the sql_* catalog tables.
12+
//
13+
//go:embed schema.sql
14+
var Schema string

internal/core/sqlc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"sql": [
44
{
55
"engine": "sqlite",
6-
"schema": "schema.sql",
7-
"queries": "catalogdb/query.sql",
6+
"schema": "catalogdef/schema.sql",
7+
"queries": "catalogdef/query.sql",
88
"gen": {
99
"go": {
1010
"package": "catalogdb",

0 commit comments

Comments
 (0)