Skip to content

Commit 29bbb81

Browse files
committed
fix(compiler): prevent schema parse failures by ignoring psql meta commands
Resolves #4065
1 parent e18bf22 commit 29bbb81

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

internal/compiler/compile.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io"
@@ -38,6 +39,7 @@ func (c *Compiler) parseCatalog(schemas []string) error {
3839
continue
3940
}
4041
contents := migrations.RemoveRollbackStatements(string(blob))
42+
contents = removePsqlMetaCommands(contents)
4143
c.schema = append(c.schema, contents)
4244
stmts, err := c.parser.Parse(strings.NewReader(contents))
4345
if err != nil {
@@ -57,6 +59,19 @@ func (c *Compiler) parseCatalog(schemas []string) error {
5759
return nil
5860
}
5961

62+
func removePsqlMetaCommands(contents string) string {
63+
s := bufio.NewScanner(strings.NewReader(contents))
64+
var lines []string
65+
for s.Scan() {
66+
line := s.Text()
67+
if strings.HasPrefix(line, `\`) {
68+
continue
69+
}
70+
lines = append(lines, line)
71+
}
72+
return strings.Join(lines, "\n")
73+
}
74+
6075
func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
6176
var q []*Query
6277
merr := multierr.New()

internal/endtoend/testdata/pg_dump/schema.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
-- Dumped from database version 15.3 (Debian 15.3-1.pgdg120+1)
66
-- Dumped by pg_dump version 15.3
77

8+
\restrict auwherpfqaiuwrhgp
9+
810
SET statement_timeout = 0;
911
SET lock_timeout = 0;
1012
SET idle_in_transaction_session_timeout = 0;

0 commit comments

Comments
 (0)