Skip to content
Merged
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
1 change: 1 addition & 0 deletions internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (c *Compiler) parseCatalog(schemas []string) error {
continue
}
contents := migrations.RemoveRollbackStatements(string(blob))
contents = migrations.RemovePsqlMetaCommands(contents)
c.schema = append(c.schema, contents)

// In database-only mode, we parse the schema to validate syntax
Expand Down
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/pg_dump/schema.sql
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 Expand Up @@ -83,6 +85,8 @@ ALTER TABLE ONLY public.authors
ADD CONSTRAINT authors_pkey PRIMARY KEY (id);


\unrestrict auwherpfqaiuwrhgp

--
-- PostgreSQL database dump complete
--
Expand Down
23 changes: 23 additions & 0 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package migrations

import (
"bufio"
"regexp"
"strings"
)

// psqlMetaCommand matches a psql meta-command (a line that begins with a
// backslash followed by a command name). pg_dump emits these starting with
// PostgreSQL 17.6 / 16.10 / 15.14 / 14.19 / 13.22 (e.g. `\restrict KEY` and
// `\unrestrict KEY`), and sqlc's SQL parsers cannot handle them.
var psqlMetaCommand = regexp.MustCompile(`^\\[A-Za-z!?;][^\n]*$`)

// Remove all lines after a rollback comment.
//
// goose: -- +goose Down
Expand Down Expand Up @@ -33,6 +40,22 @@ func RemoveRollbackStatements(contents string) string {
return strings.Join(lines, "\n")
}

// RemovePsqlMetaCommands strips psql meta-command lines (e.g. `\restrict KEY`,
// `\unrestrict KEY`, `\connect foo`) from SQL input. These are emitted by
// pg_dump but are not valid SQL, so they must be removed before parsing.
func RemovePsqlMetaCommands(contents string) string {
s := bufio.NewScanner(strings.NewReader(contents))
var lines []string
for s.Scan() {
line := s.Text()
if psqlMetaCommand.MatchString(line) {
continue
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}

func IsDown(filename string) bool {
// Remove golang-migrate rollback files.
return strings.HasSuffix(filename, ".down.sql")
Expand Down
17 changes: 17 additions & 0 deletions internal/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const outputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);`

const inputPsqlMeta = `\restrict auwherpfqaiuwrhgp

CREATE TABLE foo (id int);

\unrestrict auwherpfqaiuwrhgp
`

const outputPsqlMeta = `
CREATE TABLE foo (id int);
`

func TestRemoveRollback(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
t.Errorf("goose migration mismatch:\n%s", diff)
Expand All @@ -71,6 +82,12 @@ func TestRemoveRollback(t *testing.T) {
}
}

func TestRemovePsqlMetaCommands(t *testing.T) {
if diff := cmp.Diff(outputPsqlMeta, RemovePsqlMetaCommands(inputPsqlMeta)); diff != "" {
t.Errorf("psql meta-command mismatch:\n%s", diff)
}
}

func TestRemoveGolangMigrateRollback(t *testing.T) {
filenames := map[string]bool{
// make sure we let through golang-migrate files that aren't rollbacks
Expand Down
Loading