Skip to content

Commit 256cbe4

Browse files
committed
feat: snapshot restore on update
1 parent e911613 commit 256cbe4

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

cmd/update.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var (
1212
updateCwd string
1313
updateRunFilter string
1414
updateCommit bool
15+
updateNoRestore bool
1516

1617
// updateCmd represents the update command
1718
updateCmd = &cobra.Command{
@@ -22,7 +23,7 @@ var (
2223
fmt.Print(err.Error())
2324
os.Exit(1)
2425
}
25-
regresql.Update(updateCwd, updateRunFilter, updateCommit)
26+
regresql.Update(updateCwd, updateRunFilter, updateCommit, updateNoRestore)
2627
},
2728
}
2829
)
@@ -33,4 +34,5 @@ func init() {
3334
updateCmd.Flags().StringVarP(&updateCwd, "cwd", "C", ".", "Change to Directory")
3435
updateCmd.Flags().StringVar(&updateRunFilter, "run", "", "Run only queries matching regexp (matches file names and query names)")
3536
updateCmd.Flags().BoolVar(&updateCommit, "commit", false, "Commit transactions instead of rollback (use with caution)")
37+
updateCmd.Flags().BoolVar(&updateNoRestore, "no-restore", false, "Skip snapshot restore before update")
3638
}

regresql/regresql.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ case and add a value for each parameter. `)
9292
Update updates the expected files from the queries and their parameters.
9393
Each query runs in its own transaction that rolls back (unless commit is true).
9494
*/
95-
func Update(root string, runFilter string, commit bool) {
95+
func Update(root string, runFilter string, commit, noRestore bool) {
9696
config, err := ReadConfig(root)
9797
ignorePatterns := []string{}
9898
if err == nil {
@@ -107,6 +107,8 @@ func Update(root string, runFilter string, commit bool) {
107107
os.Exit(3)
108108
}
109109

110+
autoRestore(config, root, noRestore)
111+
110112
if err := TestConnectionString(config.PgUri); err != nil {
111113
fmt.Print(err.Error())
112114
os.Exit(2)
@@ -133,6 +135,27 @@ the regresql update command again to reset the expected output files.
133135
`)
134136
}
135137

138+
func autoRestore(cfg config, root string, noRestore bool) {
139+
if noRestore || !ShouldAutoRestore(cfg.Snapshot) {
140+
return
141+
}
142+
snapshotPath := GetSnapshotPath(cfg.Snapshot, root)
143+
if _, err := os.Stat(snapshotPath); os.IsNotExist(err) {
144+
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)
145+
os.Exit(1)
146+
}
147+
fmt.Printf("Restoring snapshot: %s\n", snapshotPath)
148+
opts := RestoreOptions{
149+
InputPath: snapshotPath,
150+
Clean: true,
151+
}
152+
if err := RestoreSnapshot(cfg.PgUri, opts); err != nil {
153+
fmt.Printf("Error: failed to restore snapshot: %s\n", err)
154+
os.Exit(1)
155+
}
156+
fmt.Println()
157+
}
158+
136159
// Test runs regression tests for all queries.
137160
// Each query runs in its own transaction that rolls back (unless commit is true).
138161
func Test(root, runFilter, formatName, outputPath string, commit, noRestore bool) {
@@ -153,24 +176,7 @@ func Test(root, runFilter, formatName, outputPath string, commit, noRestore bool
153176
// Cache config for plan quality analysis
154177
SetGlobalConfig(config)
155178

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-
}
179+
autoRestore(config, root, noRestore)
174180

175181
// Validate schema hasn't changed since last snapshot build
176182
if err := ValidateSchemaHash(root); err != nil {

0 commit comments

Comments
 (0)