Skip to content

Commit 158d201

Browse files
committed
chore: ignore schema errors as option
1 parent ecf610f commit 158d201

3 files changed

Lines changed: 47 additions & 30 deletions

File tree

cmd/snapshot.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ var (
2020
snapshotSections bool
2121
snapshotInput string
2222
snapshotClean bool
23-
snapshotBuildFixtures []string
24-
snapshotBuildSchema string
25-
snapshotBuildMigrations string
26-
snapshotBuildVerbose bool
23+
snapshotBuildFixtures []string
24+
snapshotBuildSchema string
25+
snapshotBuildMigrations string
26+
snapshotBuildVerbose bool
27+
snapshotBuildIgnoreSchemaErrs bool
2728
snapshotInfoCompare bool
2829
snapshotTagNote string
2930
snapshotTagArchive string
@@ -207,6 +208,7 @@ func init() {
207208
snapshotBuildCmd.Flags().StringVar(&snapshotBuildMigrations, "migrations", "", "Directory of SQL migrations to apply")
208209
snapshotBuildCmd.Flags().StringSliceVar(&snapshotBuildFixtures, "fixtures", nil, "Fixture names to apply")
209210
snapshotBuildCmd.Flags().BoolVarP(&snapshotBuildVerbose, "verbose", "v", false, "Print detailed progress")
211+
snapshotBuildCmd.Flags().BoolVar(&snapshotBuildIgnoreSchemaErrs, "ignore-schema-errors", false, "Continue on schema errors (e.g., missing roles)")
210212

211213
snapshotInfoCmd.Flags().BoolVar(&snapshotInfoCompare, "compare", false, "Compare stored settings with current database")
212214

@@ -548,13 +550,14 @@ func runSnapshotBuild() error {
548550
fmt.Println()
549551

550552
result, err := regresql.BuildSnapshot(cfg.PgUri, snapshotCwd, regresql.SnapshotBuildOptions{
551-
OutputPath: outputPath,
552-
Format: format,
553-
SchemaPath: schemaPath,
554-
MigrationsDir: migrationsDir,
555-
MigrationCommand: migrationCommand,
556-
Fixtures: fixtures,
557-
Verbose: snapshotBuildVerbose,
553+
OutputPath: outputPath,
554+
Format: format,
555+
SchemaPath: schemaPath,
556+
MigrationsDir: migrationsDir,
557+
MigrationCommand: migrationCommand,
558+
Fixtures: fixtures,
559+
Verbose: snapshotBuildVerbose,
560+
IgnoreSchemaErrors: snapshotBuildIgnoreSchemaErrs,
558561
})
559562
if err != nil {
560563
return err

regresql/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func DetectSnapshotFormat(path string) SnapshotFormat {
647647
}
648648

649649
ext := strings.ToLower(filepath.Ext(path))
650-
if ext == ".sql" {
650+
if ext == ".sql" || ext == ".psql" {
651651
return FormatPlain
652652
}
653653

regresql/snapshot_build.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import (
1515

1616
type (
1717
SnapshotBuildOptions struct {
18-
OutputPath string
19-
Format SnapshotFormat
20-
SchemaPath string
21-
MigrationsDir string
22-
MigrationCommand string
23-
Fixtures []string
24-
Verbose bool
18+
OutputPath string
19+
Format SnapshotFormat
20+
SchemaPath string
21+
MigrationsDir string
22+
MigrationCommand string
23+
Fixtures []string
24+
Verbose bool
25+
IgnoreSchemaErrors bool
2526
}
2627

2728
snapshotBuildResult struct {
@@ -38,10 +39,17 @@ func BuildSnapshot(basePgUri string, root string, opts SnapshotBuildOptions) (*s
3839
return nil, err
3940
}
4041

41-
// Check pg_restore is available for non-plain schema files
42-
if opts.SchemaPath != "" && DetectSnapshotFormat(opts.SchemaPath) != FormatPlain {
43-
if err := CheckPgTool("pg_restore", root); err != nil {
44-
return nil, err
42+
// Check required tools based on schema format
43+
if opts.SchemaPath != "" {
44+
format := DetectSnapshotFormat(opts.SchemaPath)
45+
if format == FormatPlain {
46+
if err := CheckPgTool("psql", root); err != nil {
47+
return nil, err
48+
}
49+
} else {
50+
if err := CheckPgTool("pg_restore", root); err != nil {
51+
return nil, err
52+
}
4553
}
4654
}
4755

@@ -83,7 +91,7 @@ func BuildSnapshot(basePgUri string, root string, opts SnapshotBuildOptions) (*s
8391
format := DetectSnapshotFormat(opts.SchemaPath)
8492
fmt.Printf("Applying schema: %s (format: %s)\n", opts.SchemaPath, format)
8593
}
86-
if err := applySchemaFile(tempDB.PgUri, opts.SchemaPath); err != nil {
94+
if err := applySchemaFile(tempDB.PgUri, opts.SchemaPath, opts.IgnoreSchemaErrors); err != nil {
8795
return nil, fmt.Errorf("schema %q: %w", opts.SchemaPath, err)
8896
}
8997
schemaHash, err = computeSchemaHash(opts.SchemaPath)
@@ -255,16 +263,22 @@ func execSQLFile(db *sql.DB, path string) error {
255263
return nil
256264
}
257265

258-
func applySchemaFile(pguri, schemaPath string) error {
266+
func applySchemaFile(pguri, schemaPath string, ignoreErrors bool) error {
259267
format := DetectSnapshotFormat(schemaPath)
260268

261269
if format == FormatPlain {
262-
db, err := OpenDB(pguri)
263-
if err != nil {
264-
return err
270+
// Use psql to handle psql meta-commands (\restrict, \set, etc.)
271+
args := []string{pguri, "-f", schemaPath}
272+
if !ignoreErrors {
273+
args = append(args, "-v", "ON_ERROR_STOP=1")
265274
}
266-
defer db.Close()
267-
return execSQLFile(db, schemaPath)
275+
cmd := exec.Command("psql", args...)
276+
cmd.Stdout = os.Stdout
277+
cmd.Stderr = os.Stderr
278+
if err := cmd.Run(); err != nil {
279+
return fmt.Errorf("psql failed: %w", err)
280+
}
281+
return nil
268282
}
269283

270284
// Custom or Directory format - use pg_restore --schema-only

0 commit comments

Comments
 (0)