Skip to content

Commit

Permalink
fix: print notices from migration statements
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Jan 31, 2025
1 parent 722c814 commit 9e2a740
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
21 changes: 20 additions & 1 deletion pkg/migration/file.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package migration

import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"io"
"io/fs"
"path/filepath"
"regexp"
"strings"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
Expand Down Expand Up @@ -86,7 +88,24 @@ func (m *MigrationFile) ExecBatch(ctx context.Context, conn *pgx.Conn) error {
if i < len(m.Statements) {
stat = m.Statements[i]
}
return errors.Errorf("%w\nAt statement %d: %s", err, i, stat)
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
lines := strings.Split(stat, "\n")
pos := int(pgErr.Position)
for j, r := range lines {
if c := len(r); pos > c {
pos -= c + 1
continue
}
if pos > 0 {
caret := append(bytes.Repeat([]byte{' '}, pos-1), '^')
lines = append(lines[:j+1], string(caret))
}
break
}
stat = strings.Join(lines, "\n")
}
return errors.Errorf("%w\nAt statement %d:\n%s", err, i, stat)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/migration/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
SET_LOCK_TIMEOUT = "SET LOCAL lock_timeout = '4s'"
SET_LOCK_TIMEOUT = "SET lock_timeout = '4s'"
CREATE_VERSION_SCHEMA = "CREATE SCHEMA IF NOT EXISTS supabase_migrations"
CREATE_VERSION_TABLE = "CREATE TABLE IF NOT EXISTS supabase_migrations.schema_migrations (version text NOT NULL PRIMARY KEY)"
ADD_STATEMENTS_COLUMN = "ALTER TABLE supabase_migrations.schema_migrations ADD COLUMN IF NOT EXISTS statements text[]"
Expand Down
6 changes: 6 additions & 0 deletions pkg/pgxv5/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package pgxv5

import (
"context"
"fmt"
"os"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)

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

0 comments on commit 9e2a740

Please sign in to comment.