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 == "" {
|
if targetDB == "" {
|
||||||
return fmt.Errorf("cannot determine database name, please specify --target")
|
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
|
// Safety checks
|
||||||
@@ -445,16 +449,30 @@ type archiveInfo struct {
|
|||||||
DBName string
|
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
|
// extractDBNameFromArchive extracts database name from archive filename
|
||||||
func extractDBNameFromArchive(filename string) string {
|
func extractDBNameFromArchive(filename string) string {
|
||||||
base := filepath.Base(filename)
|
base := filepath.Base(filename)
|
||||||
|
|
||||||
// Remove extensions
|
// Remove extensions
|
||||||
base = strings.TrimSuffix(base, ".tar.gz")
|
base = stripFileExtensions(base)
|
||||||
base = strings.TrimSuffix(base, ".dump.gz")
|
|
||||||
base = strings.TrimSuffix(base, ".sql.gz")
|
|
||||||
base = strings.TrimSuffix(base, ".dump")
|
|
||||||
base = strings.TrimSuffix(base, ".sql")
|
|
||||||
|
|
||||||
// Remove timestamp patterns (YYYYMMDD_HHMMSS)
|
// Remove timestamp patterns (YYYYMMDD_HHMMSS)
|
||||||
parts := strings.Split(base, "_")
|
parts := strings.Split(base, "_")
|
||||||
|
|||||||
@@ -359,16 +359,30 @@ func (m ArchiveBrowserModel) filterArchives(archives []ArchiveInfo) []ArchiveInf
|
|||||||
return filtered
|
return filtered
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// extractDBNameFromFilename extracts database name from archive filename
|
// extractDBNameFromFilename extracts database name from archive filename
|
||||||
func extractDBNameFromFilename(filename string) string {
|
func extractDBNameFromFilename(filename string) string {
|
||||||
base := filepath.Base(filename)
|
base := filepath.Base(filename)
|
||||||
|
|
||||||
// Remove extensions
|
// Remove extensions
|
||||||
base = strings.TrimSuffix(base, ".tar.gz")
|
base = stripFileExtensions(base)
|
||||||
base = strings.TrimSuffix(base, ".dump.gz")
|
|
||||||
base = strings.TrimSuffix(base, ".sql.gz")
|
|
||||||
base = strings.TrimSuffix(base, ".dump")
|
|
||||||
base = strings.TrimSuffix(base, ".sql")
|
|
||||||
|
|
||||||
// Remove timestamp patterns (YYYYMMDD_HHMMSS)
|
// Remove timestamp patterns (YYYYMMDD_HHMMSS)
|
||||||
parts := strings.Split(base, "_")
|
parts := strings.Split(base, "_")
|
||||||
|
|||||||
Reference in New Issue
Block a user