-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
debug-migrate-database.go
89 lines (81 loc) · 3.16 KB
/
debug-migrate-database.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cmd
import (
"fmt"
"os"
"strings"
"github.com/ddev/ddev/pkg/ddevapp"
"github.com/ddev/ddev/pkg/nodeps"
"github.com/ddev/ddev/pkg/util"
"github.com/spf13/cobra"
)
// DebugMigrateDatabase Migrates a database to a new type
var DebugMigrateDatabase = &cobra.Command{
Use: "migrate-database",
Short: "Migrate a MySQL or MariaDB database to a different dbtype:dbversion; works only with MySQL and MariaDB, not with PostgreSQL",
Example: `ddev debug migrate-database mysql:8.0
ddev debug migrate-database mariadb:10.7`,
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
app, err := ddevapp.GetActiveApp("")
if err != nil {
util.Failed("Can't find active project: %v", err)
}
newDBVersionType := args[0]
existingDBType, err := app.GetExistingDBType()
if err != nil {
util.Failed("Failed to get existing DB type/version: %v", err)
}
if newDBVersionType == existingDBType {
util.Success("Database type in the Docker volume is already %s", newDBVersionType)
return
}
if !strings.HasPrefix(newDBVersionType, nodeps.MariaDB) && !strings.HasPrefix(newDBVersionType, nodeps.MySQL) {
util.Failed("This command can only convert between MariaDB and MySQL")
}
if (nodeps.IsValidMariaDBVersion(newDBVersionType) || nodeps.IsValidMySQLVersion(newDBVersionType)) && (nodeps.IsValidMariaDBVersion(existingDBType) || nodeps.IsValidMySQLVersion(existingDBType)) {
if !util.Confirm(fmt.Sprintf("Is it OK to attempt conversion from %s to %s?\nThis will export your database, create a snapshot,\nthen destroy your current database and import into the new database type.\nIt only migrates the 'db' database", existingDBType, newDBVersionType)) {
util.Failed("migrate-database cancelled")
}
err = os.MkdirAll(app.GetConfigPath(".downloads"), 0755)
if err != nil {
util.Failed("Failed to create %s: %v", app.GetConfigPath(".downloads"), err)
}
status, _ := app.SiteStatus()
if status != ddevapp.SiteRunning {
err = app.Start()
if err != nil {
util.Failed("Failed to start %s: %v", app.Name, err)
}
}
err = app.ExportDB(app.GetConfigPath(".downloads/db.sql.gz"), "gzip", "db")
if err != nil {
util.Failed("Failed to export-db to %s: %v", app.GetConfigPath(".downloads/db.sql.gz"), err)
}
err = app.Stop(true, true)
if err != nil {
util.Failed("Failed to stop and delete %s with snapshot: %v", app.Name, err)
}
typeVals := strings.Split(newDBVersionType, ":")
app.Database.Type = typeVals[0]
app.Database.Version = typeVals[1]
err = app.WriteConfig()
if err != nil {
util.Failed("Failed to WriteConfig: %v", err)
}
err = app.Start()
if err != nil {
util.Failed("Failed to start %s: %v", app.Name, err)
}
err = app.ImportDB(app.GetConfigPath(".downloads/db.sql.gz"), "", true, false, "")
if err != nil {
util.Failed("Failed to import-db %s: %v", app.GetConfigPath(".downloads/db.sql.gz"), err)
}
util.Success("Database was converted to %s", newDBVersionType)
return
}
util.Failed("Invalid source database type (%s) or target database type (%s)", existingDBType, newDBVersionType)
},
}
func init() {
DebugCmd.AddCommand(DebugMigrateDatabase)
}