11package cmd
22
33import (
4+ "errors"
5+ "fmt"
6+ "os"
7+
8+ "github.com/semaphoreui/semaphore/db/factory"
9+ "github.com/semaphoreui/semaphore/db_migration"
410 "github.com/semaphoreui/semaphore/util"
511 "github.com/spf13/cobra"
612)
713
814var migrationArgs struct {
9- undoTo string
10- applyTo string
15+ undoTo string
16+ applyTo string
17+ fromBoltDb string
1118}
1219
1320func init () {
1421 migrateCmd .PersistentFlags ().StringVar (& migrationArgs .undoTo , "undo-to" , "" , "Undo to specific version" )
1522 migrateCmd .PersistentFlags ().StringVar (& migrationArgs .applyTo , "apply-to" , "" , "Apply to specific version" )
16-
23+ migrateCmd . PersistentFlags (). StringVar ( & migrationArgs . fromBoltDb , "from-boltdb" , "" , "Path to boltDB data file" )
1724 rootCmd .AddCommand (migrateCmd )
1825}
1926
@@ -22,23 +29,80 @@ var migrateCmd = &cobra.Command{
2229 Short : "Execute migrations" ,
2330 Run : func (cmd * cobra.Command , args []string ) {
2431
25- var undoTo , applyTo * string
26-
2732 if migrationArgs .undoTo != "" && migrationArgs .applyTo != "" {
2833 panic ("Cannot specify both --undo-to and --apply-to" )
34+ } else if migrationArgs .undoTo != "" || migrationArgs .applyTo != "" {
35+ var undoTo , applyTo * string
36+
37+ if migrationArgs .undoTo != "" {
38+ undoTo = & migrationArgs .undoTo
39+ }
40+
41+ if migrationArgs .applyTo != "" {
42+ applyTo = & migrationArgs .applyTo
43+ }
44+
45+ store := createStoreWithMigrationVersion ("migrate" , undoTo , applyTo )
46+
47+ defer store .Close ("migrate" )
48+ util .Config .PrintDbInfo ()
2949 }
3050
31- if migrationArgs .undoTo != "" {
32- undoTo = & migrationArgs .undoTo
51+ if migrationArgs .fromBoltDb != "" {
52+ migrateBoltDb ( migrationArgs .fromBoltDb )
3353 }
54+ },
55+ }
3456
35- if migrationArgs .applyTo != "" {
36- applyTo = & migrationArgs .applyTo
57+ func migrateBoltDb (boltDbPath string ) {
58+
59+ boltCfg := util.DbConfig {
60+ Dialect : util .DbDriverBolt ,
61+ Hostname : boltDbPath ,
62+ }
63+
64+ if boltCfg .Dialect != util .DbDriverBolt {
65+ fmt .Printf ("Error: Source database must be BoltDB (dialect: %s)\n " , boltCfg .Dialect )
66+ return
67+ }
68+
69+ _ , err := os .Stat (boltDbPath )
70+ if err != nil {
71+ if errors .Is (err , os .ErrNotExist ) {
72+ fmt .Println ("File does not exist" )
73+ } else {
74+ fmt .Printf ("Error: %v\n " , err )
3775 }
76+ return
77+ }
3878
39- store := createStoreWithMigrationVersion ("migrate" , undoTo , applyTo )
79+ boltStore := factory .CreateStoreWithConfig (boltCfg )
80+ boltStore .Connect ("" )
4081
41- defer store .Close ("migrate" )
42- util .Config .PrintDbInfo ()
43- },
82+ // 2. Create SQL Store
83+ cfg , _ := util .ConfigInitNew ("" , true , true )
84+ sqlCfg , err := cfg .GetDBConfig ()
85+
86+ if err != nil {
87+ fmt .Printf ("Error reading SQL DB config: %v\n " , err )
88+ return
89+ }
90+
91+ if sqlCfg .Dialect == util .DbDriverBolt {
92+ fmt .Println ("Error: Destination database must be a SQL database" )
93+ return
94+ }
95+
96+ sqlStore := factory .CreateStoreWithConfig (sqlCfg )
97+ sqlStore .Connect ("" )
98+
99+ // 3. Connect and migrate
100+ fmt .Println ("Starting migration..." )
101+ err = db_migration .Migrate (boltStore , sqlStore )
102+ if err != nil {
103+ fmt .Printf ("Migration failed: %v\n " , err )
104+ return
105+ }
106+
107+ fmt .Println ("Migration finished successfully." )
44108}
0 commit comments