@@ -15,13 +15,14 @@ import (
1515
1616type (
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