Fix: Strip file extensions from target database names to prevent double extensions
- Created stripFileExtensions() helper that loops until all extensions removed - Applied to both --target flag values and extracted archive names - Handles cases like .sql.gz.sql.gz by repeatedly stripping until clean - Updated both cmd/restore.go and internal/tui/archive_browser.go - Ensures database names never contain .sql, .dump, .tar.gz etc extensions
This commit is contained in:
@@ -200,6 +200,10 @@ func runRestoreSingle(cmd *cobra.Command, args []string) error {
|
||||
if targetDB == "" {
|
||||
return fmt.Errorf("cannot determine database name, please specify --target")
|
||||
}
|
||||
} else {
|
||||
// If target was explicitly provided, also strip common file extensions
|
||||
// in case user included them in the target name
|
||||
targetDB = stripFileExtensions(targetDB)
|
||||
}
|
||||
|
||||
// Safety checks
|
||||
@@ -445,16 +449,30 @@ type archiveInfo struct {
|
||||
DBName string
|
||||
}
|
||||
|
||||
// stripFileExtensions removes common backup file extensions from a name
|
||||
func stripFileExtensions(name string) string {
|
||||
// Remove extensions (handle double extensions like .sql.gz.sql.gz)
|
||||
for {
|
||||
oldName := name
|
||||
name = strings.TrimSuffix(name, ".tar.gz")
|
||||
name = strings.TrimSuffix(name, ".dump.gz")
|
||||
name = strings.TrimSuffix(name, ".sql.gz")
|
||||
name = strings.TrimSuffix(name, ".dump")
|
||||
name = strings.TrimSuffix(name, ".sql")
|
||||
// If no change, we're done
|
||||
if name == oldName {
|
||||
break
|
||||
}
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// extractDBNameFromArchive extracts database name from archive filename
|
||||
func extractDBNameFromArchive(filename string) string {
|
||||
base := filepath.Base(filename)
|
||||
|
||||
// Remove extensions
|
||||
base = strings.TrimSuffix(base, ".tar.gz")
|
||||
base = strings.TrimSuffix(base, ".dump.gz")
|
||||
base = strings.TrimSuffix(base, ".sql.gz")
|
||||
base = strings.TrimSuffix(base, ".dump")
|
||||
base = strings.TrimSuffix(base, ".sql")
|
||||
base = stripFileExtensions(base)
|
||||
|
||||
// Remove timestamp patterns (YYYYMMDD_HHMMSS)
|
||||
parts := strings.Split(base, "_")
|
||||
|
||||
Reference in New Issue
Block a user