Skip to content

Commit e911613

Browse files
committed
feat: do auto snapshot restore if snapshot is present
can be overriden using `--no-restore`
1 parent e2b3ad4 commit e911613

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

cmd/test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
testFormat string
1515
testOutputPath string
1616
testCommit bool
17+
testNoRestore bool
1718

1819
testCmd = &cobra.Command{
1920
Use: "test [flags]",
@@ -23,7 +24,7 @@ var (
2324
fmt.Print(err.Error())
2425
os.Exit(1)
2526
}
26-
regresql.Test(testCwd, testRunFilter, testFormat, testOutputPath, testCommit)
27+
regresql.Test(testCwd, testRunFilter, testFormat, testOutputPath, testCommit, testNoRestore)
2728
},
2829
}
2930
)
@@ -36,4 +37,5 @@ func init() {
3637
testCmd.Flags().StringVar(&testFormat, "format", "console", "Output format: console, pgtap, junit, json, github-actions")
3738
testCmd.Flags().StringVarP(&testOutputPath, "output", "o", "", "Output file path (default: stdout)")
3839
testCmd.Flags().BoolVar(&testCommit, "commit", false, "Commit transactions instead of rollback (use with caution)")
40+
testCmd.Flags().BoolVar(&testNoRestore, "no-restore", false, "Skip snapshot restore before test")
3941
}

regresql/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type (
3434
Migrations string `yaml:"migrations,omitempty"` // directory of SQL migrations to apply
3535
MigrationCommand string `yaml:"migration_command,omitempty"` // external command to run migrations (e.g., goose, migrate)
3636
Fixtures []string `yaml:"fixtures,omitempty"` // SQL/YAML fixture files for snapshot build
37+
AutoRestore *bool `yaml:"auto_restore,omitempty"` // restore snapshot before test (default: true if path is set)
3738
}
3839
)
3940

regresql/regresql.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ the regresql update command again to reset the expected output files.
135135

136136
// Test runs regression tests for all queries.
137137
// Each query runs in its own transaction that rolls back (unless commit is true).
138-
func Test(root, runFilter, formatName, outputPath string, commit bool) {
138+
func Test(root, runFilter, formatName, outputPath string, commit, noRestore bool) {
139139
config, err := ReadConfig(root)
140140
ignorePatterns := []string{}
141141
if err == nil {
@@ -153,6 +153,25 @@ func Test(root, runFilter, formatName, outputPath string, commit bool) {
153153
// Cache config for plan quality analysis
154154
SetGlobalConfig(config)
155155

156+
// Auto-restore snapshot before test
157+
if !noRestore && ShouldAutoRestore(config.Snapshot) {
158+
snapshotPath := GetSnapshotPath(config.Snapshot, root)
159+
if _, err := os.Stat(snapshotPath); os.IsNotExist(err) {
160+
fmt.Printf("Error: snapshot file not found: %s\n\nRun 'regresql snapshot build' to create a snapshot, or use '--no-restore' to skip\n", snapshotPath)
161+
os.Exit(1)
162+
}
163+
fmt.Printf("Restoring snapshot: %s\n", snapshotPath)
164+
opts := RestoreOptions{
165+
InputPath: snapshotPath,
166+
Clean: true,
167+
}
168+
if err := RestoreSnapshot(config.PgUri, opts); err != nil {
169+
fmt.Printf("Error: failed to restore snapshot: %s\n", err)
170+
os.Exit(1)
171+
}
172+
fmt.Println()
173+
}
174+
156175
// Validate schema hasn't changed since last snapshot build
157176
if err := ValidateSchemaHash(root); err != nil {
158177
fmt.Printf("Error: %s\n", err)

regresql/snapshot.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ func GetSnapshotsDir(root string) string {
418418
return filepath.Join(root, "snapshots")
419419
}
420420

421+
func ShouldAutoRestore(cfg *SnapshotConfig) bool {
422+
if cfg == nil || cfg.Path == "" {
423+
return false
424+
}
425+
if cfg.AutoRestore == nil {
426+
return true
427+
}
428+
return *cfg.AutoRestore
429+
}
430+
421431
// RestoreSnapshot restores a database snapshot using pg_restore or psql
422432
func RestoreSnapshot(pguri string, opts RestoreOptions) error {
423433
if _, err := os.Stat(opts.InputPath); os.IsNotExist(err) {

0 commit comments

Comments
 (0)