-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdb_import.go
More file actions
69 lines (59 loc) · 1.83 KB
/
Copy pathdb_import.go
File metadata and controls
69 lines (59 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cmd
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
)
// Import keeps its own flag value because it delegates database creation to
// CreateDatabase; RunE copies the selected value to the shared create flag.
var importTursoDBFlag bool
func init() {
dbCmd.AddCommand(importCmd)
addGroupFlag(importCmd)
addRemoteEncryptionKeyFlag(importCmd)
addRemoteEncryptionCipherFlag(importCmd)
importCmd.Flags().BoolVar(&importTursoDBFlag, "tursodb", false, "Import into a TursoDB (MVCC) database.")
}
var importCmd = &cobra.Command{
Use: "import [filename]",
Short: "Import a SQLite database file to Turso.",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: noFilesArg,
PreRunE: func(cmd *cobra.Command, args []string) error {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if len(args) == 0 {
return errors.New("filename is required: 'turso db import <filename>'")
}
filename := args[0]
if err := checkFileExists(filename); err != nil {
return err
}
locked, err := isFileLocked(filename)
if err != nil {
return fmt.Errorf("could not check file lock: %w", err)
}
if locked {
return errors.New("database file is locked by another process (close any open connections and try again)")
}
fromFileFlag = filename
tursoDBFlag = importTursoDBFlag
name := sanitizeDatabaseName(filename)
return CreateDatabase(name)
},
}
// Sanitize a SQLite database filename to be used as a cloud database name.
func sanitizeDatabaseName(filename string) string {
base := filepath.Base(filename)
extensions := []string{".db", ".sqlite", ".sqlite3", ".sl3", ".s3db", ".db3"}
for _, ext := range extensions {
if strings.HasSuffix(strings.ToLower(base), ext) {
return base[:len(base)-len(ext)]
}
}
return base
}