Skip to content

Commit 9e2a740

Browse files
committed
fix: print notices from migration statements
1 parent 722c814 commit 9e2a740

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

pkg/migration/file.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package migration
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/sha256"
67
"encoding/hex"
78
"io"
89
"io/fs"
910
"path/filepath"
1011
"regexp"
12+
"strings"
1113

1214
"github.com/go-errors/errors"
1315
"github.com/jackc/pgconn"
@@ -86,7 +88,24 @@ func (m *MigrationFile) ExecBatch(ctx context.Context, conn *pgx.Conn) error {
8688
if i < len(m.Statements) {
8789
stat = m.Statements[i]
8890
}
89-
return errors.Errorf("%w\nAt statement %d: %s", err, i, stat)
91+
var pgErr *pgconn.PgError
92+
if errors.As(err, &pgErr) {
93+
lines := strings.Split(stat, "\n")
94+
pos := int(pgErr.Position)
95+
for j, r := range lines {
96+
if c := len(r); pos > c {
97+
pos -= c + 1
98+
continue
99+
}
100+
if pos > 0 {
101+
caret := append(bytes.Repeat([]byte{' '}, pos-1), '^')
102+
lines = append(lines[:j+1], string(caret))
103+
}
104+
break
105+
}
106+
stat = strings.Join(lines, "\n")
107+
}
108+
return errors.Errorf("%w\nAt statement %d:\n%s", err, i, stat)
90109
}
91110
return nil
92111
}

pkg/migration/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
const (
13-
SET_LOCK_TIMEOUT = "SET LOCAL lock_timeout = '4s'"
13+
SET_LOCK_TIMEOUT = "SET lock_timeout = '4s'"
1414
CREATE_VERSION_SCHEMA = "CREATE SCHEMA IF NOT EXISTS supabase_migrations"
1515
CREATE_VERSION_TABLE = "CREATE TABLE IF NOT EXISTS supabase_migrations.schema_migrations (version text NOT NULL PRIMARY KEY)"
1616
ADD_STATEMENTS_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS statements text[]"

pkg/pgxv5/connect.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package pgxv5
22

33
import (
44
"context"
5+
"fmt"
6+
"os"
57

68
"github.com/go-errors/errors"
9+
"github.com/jackc/pgconn"
710
"github.com/jackc/pgx/v4"
811
)
912

@@ -14,6 +17,9 @@ func Connect(ctx context.Context, connString string, options ...func(*pgx.ConnCo
1417
if err != nil {
1518
return nil, errors.Errorf("failed to parse connection string: %w", err)
1619
}
20+
config.OnNotice = func(pc *pgconn.PgConn, n *pgconn.Notice) {
21+
fmt.Fprintf(os.Stderr, "NOTICE (%s): %s\n", n.Code, n.Message)
22+
}
1723
// Apply config overrides
1824
for _, op := range options {
1925
op(config)

0 commit comments

Comments
 (0)