|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/AmrSaber/kv/src/common" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + _ "modernc.org/sqlite" |
| 11 | +) |
| 12 | + |
| 13 | +// importCmd represents the import command |
| 14 | +var importCmd = &cobra.Command{ |
| 15 | + Use: "import <file-path>", |
| 16 | + Short: "Import database from a file", |
| 17 | + Long: `Import a database from a binary file and replace the current database. |
| 18 | +
|
| 19 | +The imported file must be a valid database file created with the 'export' command. |
| 20 | +This will completely replace the current database with the imported one. |
| 21 | +
|
| 22 | +WARNING: This operation is destructive. The current database will be backed up |
| 23 | +to <db-path>.backup before importing. |
| 24 | +
|
| 25 | +Use "-" as the file path to read from stdin (useful for piping).`, |
| 26 | + Example: ` # Import database from a file |
| 27 | + kv db import backup.db |
| 28 | +
|
| 29 | + # Import from stdin - you also get UUOC award :) |
| 30 | + cat backup.db | kv db import # Or use "-" as file name |
| 31 | +
|
| 32 | + # Import from absolute path |
| 33 | + kv db import /path/to/backup`, |
| 34 | + Args: cobra.MaximumNArgs(1), |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + sourcePath := "-" |
| 37 | + if len(args) > 0 { |
| 38 | + sourcePath = args[0] |
| 39 | + } |
| 40 | + |
| 41 | + // Handle stdin |
| 42 | + if sourcePath == "-" { |
| 43 | + // Create temporary file for stdin content |
| 44 | + tmpFile, err := os.CreateTemp("", "kv-import-*") |
| 45 | + common.FailOn(err) |
| 46 | + defer func() { _ = os.Remove(tmpFile.Name()) }() |
| 47 | + |
| 48 | + // Copy stdin to temp file |
| 49 | + _, err = io.Copy(tmpFile, os.Stdin) |
| 50 | + common.FailOn(err) |
| 51 | + err = tmpFile.Close() |
| 52 | + common.FailOn(err) |
| 53 | + |
| 54 | + sourcePath = tmpFile.Name() |
| 55 | + } else { |
| 56 | + sourcePath = common.NormalizePath(sourcePath) |
| 57 | + |
| 58 | + // Check if source file exists |
| 59 | + if _, err := os.Stat(sourcePath); os.IsNotExist(err) { |
| 60 | + common.Fail("File does not exist: %s", sourcePath) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Validate source is a valid SQLite database |
| 65 | + if err := common.ValidateSqliteFile(sourcePath); err != nil { |
| 66 | + common.Fail("Invalid database file: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Get destination path |
| 70 | + destPath := common.GetDBPath() |
| 71 | + backupPath := destPath + ".backup" |
| 72 | + |
| 73 | + // Vacuum current database to commit all WAL changes to main file |
| 74 | + db := common.GetDB() |
| 75 | + _, err := db.Exec("VACUUM") |
| 76 | + common.FailOn(err) |
| 77 | + |
| 78 | + // Close database connection |
| 79 | + common.CloseDB() |
| 80 | + |
| 81 | + // Remove old backup if exists |
| 82 | + _ = os.Remove(backupPath) |
| 83 | + |
| 84 | + // Backup current database if it exists |
| 85 | + if _, err := os.Stat(destPath); err == nil { |
| 86 | + err = os.Rename(destPath, backupPath) |
| 87 | + if err != nil { |
| 88 | + common.Fail("Failed to backup current database: %v", err) |
| 89 | + } |
| 90 | + fmt.Printf("Current database backed up to: %s\n", backupPath) |
| 91 | + } |
| 92 | + |
| 93 | + // Remove WAL files (should be empty after VACUUM, but remove just in case) |
| 94 | + _ = os.Remove(destPath + "-wal") |
| 95 | + _ = os.Remove(destPath + "-shm") |
| 96 | + |
| 97 | + // Copy source to destination |
| 98 | + err = common.CopyFile(sourcePath, destPath) |
| 99 | + if err != nil { |
| 100 | + if _, err := os.Stat(backupPath); err == nil { |
| 101 | + _ = os.Remove(destPath) |
| 102 | + _ = os.Rename(backupPath, destPath) |
| 103 | + } |
| 104 | + |
| 105 | + common.Fail("Failed to import database: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Reopen database (migrations will run automatically) |
| 109 | + common.GetDB() |
| 110 | + |
| 111 | + fmt.Println("Database imported successfully") |
| 112 | + }, |
| 113 | +} |
| 114 | + |
| 115 | +func init() { |
| 116 | + dbCmd.AddCommand(importCmd) |
| 117 | +} |
0 commit comments