Skip to content

Commit 412a791

Browse files
committed
fix: suggest --clean on snapshot restore if DB is not empty
1 parent 6ed58df commit 412a791

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

cmd/snapshot.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,19 @@ func runSnapshotRestore() error {
389389
return fmt.Errorf("database connection failed: %w", err)
390390
}
391391

392+
// Check if database has existing tables and warn if --clean not specified
393+
if !snapshotClean {
394+
db, err := regresql.OpenDB(cfg.PgUri)
395+
if err == nil {
396+
defer db.Close()
397+
var tableCount int
398+
db.QueryRow("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'").Scan(&tableCount)
399+
if tableCount > 0 {
400+
return fmt.Errorf("database has %d existing table(s). Use --clean to drop them before restore, or manually clear the database", tableCount)
401+
}
402+
}
403+
}
404+
392405
inputPath := snapshotInput
393406
if inputPath == "" {
394407
inputPath = regresql.GetSnapshotPath(cfg.Snapshot, snapshotCwd)

0 commit comments

Comments
 (0)