Fix MySQL support and TUI auto-confirm mode
- Fix format detection to read database_type from .meta.json metadata file - Add ensureMySQLDatabaseExists() for MySQL/MariaDB database creation - Route database creation to correct implementation based on db type - Add TUI auto-forward in auto-confirm mode (no input required for debugging) - All TUI components now exit automatically when --auto-confirm is set - Fix status view to skip loading in auto-confirm mode
This commit is contained in:
@@ -971,6 +971,40 @@ func (e *Engine) dropDatabaseIfExists(ctx context.Context, dbName string) error
|
||||
|
||||
// ensureDatabaseExists checks if a database exists and creates it if not
|
||||
func (e *Engine) ensureDatabaseExists(ctx context.Context, dbName string) error {
|
||||
// Route to appropriate implementation based on database type
|
||||
if e.cfg.DatabaseType == "mysql" || e.cfg.DatabaseType == "mariadb" {
|
||||
return e.ensureMySQLDatabaseExists(ctx, dbName)
|
||||
}
|
||||
return e.ensurePostgresDatabaseExists(ctx, dbName)
|
||||
}
|
||||
|
||||
// ensureMySQLDatabaseExists checks if a MySQL database exists and creates it if not
|
||||
func (e *Engine) ensureMySQLDatabaseExists(ctx context.Context, dbName string) error {
|
||||
// Build mysql command
|
||||
args := []string{
|
||||
"-h", e.cfg.Host,
|
||||
"-P", fmt.Sprintf("%d", e.cfg.Port),
|
||||
"-u", e.cfg.User,
|
||||
"-e", fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", dbName),
|
||||
}
|
||||
|
||||
if e.cfg.Password != "" {
|
||||
args = append(args, fmt.Sprintf("-p%s", e.cfg.Password))
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "mysql", args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
e.log.Warn("MySQL database creation failed", "name", dbName, "error", err, "output", string(output))
|
||||
return fmt.Errorf("failed to create database '%s': %w (output: %s)", dbName, err, strings.TrimSpace(string(output)))
|
||||
}
|
||||
|
||||
e.log.Info("Successfully ensured MySQL database exists", "name", dbName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensurePostgresDatabaseExists checks if a PostgreSQL database exists and creates it if not
|
||||
func (e *Engine) ensurePostgresDatabaseExists(ctx context.Context, dbName string) error {
|
||||
// Skip creation for postgres and template databases - they should already exist
|
||||
if dbName == "postgres" || dbName == "template0" || dbName == "template1" {
|
||||
e.log.Info("Skipping create for system database (assume exists)", "name", dbName)
|
||||
|
||||
@@ -2,6 +2,7 @@ package restore
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -21,6 +22,25 @@ const (
|
||||
FormatUnknown ArchiveFormat = "Unknown"
|
||||
)
|
||||
|
||||
// backupMetadata represents the structure of .meta.json files
|
||||
type backupMetadata struct {
|
||||
DatabaseType string `json:"database_type"`
|
||||
}
|
||||
|
||||
// readMetadataDBType reads the database_type from the .meta.json file if it exists
|
||||
func readMetadataDBType(archivePath string) string {
|
||||
metaPath := archivePath + ".meta.json"
|
||||
data, err := os.ReadFile(metaPath)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var meta backupMetadata
|
||||
if err := json.Unmarshal(data, &meta); err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(meta.DatabaseType)
|
||||
}
|
||||
|
||||
// DetectArchiveFormat detects the format of a backup archive from its filename and content
|
||||
func DetectArchiveFormat(filename string) ArchiveFormat {
|
||||
lower := strings.ToLower(filename)
|
||||
@@ -54,7 +74,14 @@ func DetectArchiveFormat(filename string) ArchiveFormat {
|
||||
|
||||
// Check for compressed SQL formats
|
||||
if strings.HasSuffix(lower, ".sql.gz") {
|
||||
// Determine if MySQL or PostgreSQL based on naming convention
|
||||
// First, try to determine from metadata file
|
||||
if dbType := readMetadataDBType(filename); dbType != "" {
|
||||
if dbType == "mysql" || dbType == "mariadb" {
|
||||
return FormatMySQLSQLGz
|
||||
}
|
||||
return FormatPostgreSQLSQLGz
|
||||
}
|
||||
// Fallback: determine if MySQL or PostgreSQL based on naming convention
|
||||
if strings.Contains(lower, "mysql") || strings.Contains(lower, "mariadb") {
|
||||
return FormatMySQLSQLGz
|
||||
}
|
||||
@@ -63,7 +90,14 @@ func DetectArchiveFormat(filename string) ArchiveFormat {
|
||||
|
||||
// Check for uncompressed SQL formats
|
||||
if strings.HasSuffix(lower, ".sql") {
|
||||
// Determine if MySQL or PostgreSQL based on naming convention
|
||||
// First, try to determine from metadata file
|
||||
if dbType := readMetadataDBType(filename); dbType != "" {
|
||||
if dbType == "mysql" || dbType == "mariadb" {
|
||||
return FormatMySQLSQLGz
|
||||
}
|
||||
return FormatPostgreSQLSQL
|
||||
}
|
||||
// Fallback: determine if MySQL or PostgreSQL based on naming convention
|
||||
if strings.Contains(lower, "mysql") || strings.Contains(lower, "mariadb") {
|
||||
return FormatMySQLSQL
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user