Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor(schema): share apply-time schema loading
Extract the repeated apply-time schema glob/read/preprocess/warn loop into a shared helper so command setup and PostgreSQL sqltest seeding use the same preprocessing path as the migrations work evolves.

Changes:
- Add `schemautil.LoadSchemasForApply()` to expand schema globs, read files, run `PreprocessSchemaForApply()`, and surface warnings through a caller-provided callback.
- Update `createdb`, `verify`, and managed `vet` setup to reuse the shared loader instead of open-coding the same apply-time preprocessing loop.
- Update PostgreSQL sqltest seeding helpers to reuse the shared loader for both regular and read-only database setup.
- Preserve the existing read-only PostgreSQL cache-key behavior by hashing the preprocessed DDL returned by the shared loader.
- Handle plain `pg_dump` schema replays in PostgreSQL sqltest helpers by dropping the default `public` schema before applying dumps that recreate it.

Behavioral effect:
- No intended user-visible behavior change in normal schema preprocessing.
- Apply-time callers and PostgreSQL test helpers now share one codepath, reducing drift between managed-database setup and sqltest seeding.
- Plain `pg_dump` fixtures continue to apply cleanly in PostgreSQL test databases even when the dump recreates `public`.

Testing:
- `go test ./internal/migrations ./internal/compiler ./internal/cmd/... ./internal/sqltest/... ./internal/endtoend -run 'TestReplay/base/pg_dump|TestValidSchema/endtoend-testdata/pg_dump/sqlc.json-0'`
  • Loading branch information
ignatremizov committed Apr 19, 2026
commit 98a37c88599970c8d67089c8f1727ed9dbdcd5bd
21 changes: 4 additions & 17 deletions internal/cmd/createdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"github.com/spf13/cobra"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/dbmanager"
"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/schemautil"
)

var createDBCmd = &cobra.Command{
Expand Down Expand Up @@ -76,24 +75,12 @@ func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Option
}

var ddl []string
files, err := sqlpath.Glob(queryset.Schema)
ddl, err = schemautil.LoadSchemasForApply(queryset.Schema, string(queryset.Engine), func(warning string) {
fmt.Fprintln(o.Stderr, warning)
})
if err != nil {
return err
}
for _, schema := range files {
contents, err := os.ReadFile(schema)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
ddlText, warnings, err := migrations.PreprocessSchemaForApply(string(contents), string(queryset.Engine))
if err != nil {
return err
}
for _, warning := range warnings {
fmt.Fprintln(o.Stderr, warning)
}
ddl = append(ddl, ddlText)
}

now := time.Now().UTC().UnixNano()
client := dbmanager.NewClient(conf.Servers)
Expand Down
21 changes: 4 additions & 17 deletions internal/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (

"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/dbmanager"
"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/plugin"
"github.com/sqlc-dev/sqlc/internal/quickdb"
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/schemautil"
)

func init() {
Expand Down Expand Up @@ -93,24 +92,12 @@ func Verify(ctx context.Context, dir, filename string, opts *Options) error {

// Read the schema files into memory, removing rollback statements
var ddl []string
files, err := sqlpath.Glob(current.Schema)
ddl, err = schemautil.LoadSchemasForApply(current.Schema, string(current.Engine), func(warning string) {
fmt.Fprintln(stderr, warning)
})
if err != nil {
return err
}
for _, schema := range files {
contents, err := os.ReadFile(schema)
if err != nil {
return fmt.Errorf("read file: %w", err)
}
ddlText, warnings, err := migrations.PreprocessSchemaForApply(string(contents), string(current.Engine))
if err != nil {
return err
}
for _, warning := range warnings {
fmt.Fprintln(stderr, warning)
}
ddl = append(ddl, ddlText)
}

var codegen plugin.GenerateRequest
if err := proto.Unmarshal(qs.CodegenRequest.Contents, &codegen); err != nil {
Expand Down
43 changes: 10 additions & 33 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import (
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/dbmanager"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/opts"
"github.com/sqlc-dev/sqlc/internal/plugin"
"github.com/sqlc-dev/sqlc/internal/quickdb"
"github.com/sqlc-dev/sqlc/internal/schemautil"
"github.com/sqlc-dev/sqlc/internal/shfmt"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/vet"
)

Expand Down Expand Up @@ -422,25 +421,12 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
c.Client = dbmanager.NewClient(c.Conf.Servers)
})

var ddl []string
files, err := sqlpath.Glob(s.Schema)
ddl, err := schemautil.LoadSchemasForApply(s.Schema, string(s.Engine), func(warning string) {
fmt.Fprintln(c.Stderr, warning)
})
if err != nil {
return "", cleanup, err
}
for _, schema := range files {
contents, err := os.ReadFile(schema)
if err != nil {
return "", cleanup, fmt.Errorf("read file: %w", err)
}
ddlText, warnings, err := migrations.PreprocessSchemaForApply(string(contents), string(s.Engine))
if err != nil {
return "", cleanup, err
}
for _, warning := range warnings {
fmt.Fprintln(c.Stderr, warning)
}
ddl = append(ddl, ddlText)
}

resp, err := c.Client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{
Engine: string(s.Engine),
Expand Down Expand Up @@ -547,24 +533,15 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
defer db.Close()
// For in-memory SQLite databases, apply migrations
if isInMemorySQLite(dburl) {
files, err := sqlpath.Glob(s.Schema)
ddl, err := schemautil.LoadSchemasForApply(s.Schema, string(s.Engine), func(warning string) {
fmt.Fprintln(c.Stderr, warning)
})
if err != nil {
return fmt.Errorf("schema: %w", err)
}
for _, schema := range files {
contents, err := os.ReadFile(schema)
if err != nil {
return fmt.Errorf("read schema file: %w", err)
}
ddl, warnings, err := migrations.PreprocessSchemaForApply(string(contents), string(s.Engine))
if err != nil {
return err
}
for _, warning := range warnings {
fmt.Fprintln(c.Stderr, warning)
}
if _, err := db.ExecContext(ctx, ddl); err != nil {
return fmt.Errorf("apply schema %s: %w", schema, err)
for _, stmt := range ddl {
if _, err := db.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("apply schema: %w", err)
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions internal/schemautil/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package schemautil

import (
"fmt"
"os"

"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)

// LoadSchemasForApply expands globs, preprocesses each schema in order, and
// reports any warnings through warn. The returned DDL is suitable for callers
// that will apply schema text to a live database.
func LoadSchemasForApply(globs []string, engine string, warn func(string)) ([]string, error) {
files, err := sqlpath.Glob(globs)
if err != nil {
return nil, err
}

ddl := make([]string, 0, len(files))
for _, schema := range files {
contents, err := os.ReadFile(schema)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
ddlText, warnings, err := migrations.PreprocessSchemaForApply(string(contents), engine)
if err != nil {
return nil, err
}
for _, warning := range warnings {
if warn != nil {
warn(warning)
}
}
ddl = append(ddl, ddlText)
}

return ddl, nil
}
29 changes: 11 additions & 18 deletions internal/sqltest/local/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"github.com/jackc/pgx/v5"
"golang.org/x/sync/singleflight"

migrate "github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/pgx/poolcache"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
"github.com/sqlc-dev/sqlc/internal/schemautil"
"github.com/sqlc-dev/sqlc/internal/sqltest/docker"
"github.com/sqlc-dev/sqlc/internal/sqltest/native"
)
Expand Down Expand Up @@ -60,26 +59,15 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string {
}

var seed []string
files, err := sqlpath.Glob(migrations)
h := fnv.New64()
seed, err = schemautil.LoadSchemasForApply(migrations, "postgresql", func(warning string) {
t.Log(warning)
})
if err != nil {
t.Fatal(err)
}

h := fnv.New64()
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
ddl, warnings, err := migrate.PreprocessSchemaForApply(string(blob), "postgresql")
if err != nil {
t.Fatal(err)
}
for _, ddl := range seed {
h.Write([]byte(ddl))
for _, warning := range warnings {
t.Log(warning)
}
seed = append(seed, ddl)
}

var name string
Expand Down Expand Up @@ -123,6 +111,11 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string {
if len(strings.TrimSpace(q)) == 0 {
continue
}
if strings.Contains(q, "\nCREATE SCHEMA public;\n") || strings.HasPrefix(q, "CREATE SCHEMA public;\n") {
if _, err := conn.Exec(ctx, `DROP SCHEMA IF EXISTS public CASCADE`); err != nil {
return nil, fmt.Errorf("drop public schema: %s", err)
}
}
if _, err := conn.Exec(ctx, q); err != nil {
return nil, fmt.Errorf("%s: %s", q, err)
}
Expand Down
29 changes: 15 additions & 14 deletions internal/sqltest/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"

migrate "github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/schemautil"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"

_ "github.com/lib/pq"
Expand Down Expand Up @@ -99,20 +100,20 @@ func CreatePostgreSQLDatabase(t *testing.T, name string, schema bool, migrations
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
ddl, warnings, err := migrate.PreprocessSchemaForApply(string(blob), "postgresql")
if err != nil {
t.Fatal(err)
}
for _, warning := range warnings {
t.Log(warning)
ddl, err := schemautil.LoadSchemasForApply(migrations, "postgresql", func(warning string) {
t.Log(warning)
})
if err != nil {
t.Fatal(err)
}
for i, stmt := range ddl {
if strings.Contains(stmt, "\nCREATE SCHEMA public;\n") || strings.HasPrefix(stmt, "CREATE SCHEMA public;\n") {
if _, err := sdb.Exec(`DROP SCHEMA IF EXISTS public CASCADE`); err != nil {
t.Fatalf("drop public schema: %s", err)
}
}
if _, err := sdb.Exec(ddl); err != nil {
t.Fatalf("%s: %s", filepath.Base(f), err)
if _, err := sdb.Exec(stmt); err != nil {
t.Fatalf("%s: %s", filepath.Base(files[i]), err)
}
}

Expand Down
Loading