Skip to content

Commit a971343

Browse files
committed
Fix nil pointer dereference in skip_parser with managed database
The skip_parser feature was failing with a nil pointer dereference when used with managed databases because parseCatalog() was trying to use the parser and catalog, which are nil when skip_parser is enabled. ## Root Cause When skip_parser is enabled: 1. Parser and catalog are set to nil in NewCompiler 2. parseCatalog() was unconditionally calling c.parser.Parse() (line 46) 3. This caused a nil pointer dereference However, parseCatalog() still needs to be called even in skip_parser mode because: - The schema SQL text needs to be stored in c.schema - The database analyzer needs c.schema to pass to PostgreSQL ## Fix Modified parseCatalog() to check if skip_parser is enabled: - If skip_parser: Read schema files and store in c.schema, skip parsing - If normal mode: Parse schemas and update catalog as before Also reverted the change in generate.go that was skipping ParseCatalog entirely, since we always need to call it (it now handles skip_parser internally). ## Testing This fixes the panic in the managed-db context test: - TestReplay/managed-db/skip_parser/postgresql/pgx/v5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent cc41ace commit a971343

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

internal/cmd/generate.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,28 +305,20 @@ func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.C
305305
fmt.Fprintf(stderr, "error creating compiler: %s\n", err)
306306
return nil, true
307307
}
308-
309-
// Check if skip_parser is enabled
310-
skipParser := sql.Analyzer.SkipParser != nil && *sql.Analyzer.SkipParser
311-
312-
// Skip catalog parsing if skip_parser is enabled
313-
if !skipParser {
314-
if err := c.ParseCatalog(sql.Schema); err != nil {
315-
fmt.Fprintf(stderr, "# package %s\n", name)
316-
if parserErr, ok := err.(*multierr.Error); ok {
317-
for _, fileErr := range parserErr.Errs() {
318-
printFileErr(stderr, dir, fileErr)
319-
}
320-
} else {
321-
fmt.Fprintf(stderr, "error parsing schema: %s\n", err)
308+
if err := c.ParseCatalog(sql.Schema); err != nil {
309+
fmt.Fprintf(stderr, "# package %s\n", name)
310+
if parserErr, ok := err.(*multierr.Error); ok {
311+
for _, fileErr := range parserErr.Errs() {
312+
printFileErr(stderr, dir, fileErr)
322313
}
323-
return nil, true
324-
}
325-
if parserOpts.Debug.DumpCatalog {
326-
debug.Dump(c.Catalog())
314+
} else {
315+
fmt.Fprintf(stderr, "error parsing schema: %s\n", err)
327316
}
317+
return nil, true
318+
}
319+
if parserOpts.Debug.DumpCatalog {
320+
debug.Dump(c.Catalog())
328321
}
329-
330322
if err := c.ParseQueries(sql.Queries, parserOpts); err != nil {
331323
fmt.Fprintf(stderr, "# package %s\n", name)
332324
if parserErr, ok := err.(*multierr.Error); ok {

internal/compiler/compile.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ func (c *Compiler) parseCatalog(schemas []string) error {
3434
if err != nil {
3535
return err
3636
}
37+
38+
// Check if we're in skip_parser mode
39+
skipParser := c.conf.Analyzer.SkipParser != nil && *c.conf.Analyzer.SkipParser
40+
41+
// If skip_parser is enabled, just read schema files without parsing
42+
if skipParser {
43+
for _, filename := range files {
44+
blob, err := os.ReadFile(filename)
45+
if err != nil {
46+
return fmt.Errorf("reading schema file %s: %w", filename, err)
47+
}
48+
contents := migrations.RemoveRollbackStatements(string(blob))
49+
c.schema = append(c.schema, contents)
50+
}
51+
return nil
52+
}
53+
54+
// Normal path: parse and update catalog
3755
merr := multierr.New()
3856
for _, filename := range files {
3957
blob, err := os.ReadFile(filename)

0 commit comments

Comments
 (0)