Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(compiler): prevent schema parse failures by ignoring psql meta commands #4082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andrewmbenton wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from andrew/fix-4065
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions internal/compiler/compile.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"bufio"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -38,6 +39,7 @@ func (c *Compiler) parseCatalog(schemas []string) error {
continue
}
contents := migrations.RemoveRollbackStatements(string(blob))
contents = removePsqlMetaCommands(contents)
c.schema = append(c.schema, contents)
stmts, err := c.parser.Parse(strings.NewReader(contents))
if err != nil {
Expand All @@ -57,6 +59,19 @@ func (c *Compiler) parseCatalog(schemas []string) error {
return nil
}

func removePsqlMetaCommands(contents string) string {
s := bufio.NewScanner(strings.NewReader(contents))
var lines []string
for s.Scan() {
line := s.Text()
if strings.HasPrefix(line, `\`) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a little aggressive to remove all lines that begin with \. The following is valid SQL, but would be broken by this fix

SELECT E'
\x61 b c
';

I don't think pg_dump would create such a statement but hand written SQL could.

I would suggest that the fix only exclude lines starting with \restrict and \unrestrict

continue
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}

func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
var q []*Query
merr := multierr.New()
Expand Down
2 changes: 2 additions & 0 deletions internal/endtoend/testdata/pg_dump/schema.sql
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
-- Dumped from database version 15.3 (Debian 15.3-1.pgdg120+1)
-- Dumped by pg_dump version 15.3

\restrict auwherpfqaiuwrhgp

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /