55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/Jaculabilis/intake/core"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var migrateCmd = &cobra.Command{
|
|
Use: "migrate",
|
|
Short: "Migrate an intake database to the latest version",
|
|
Long: `Migrate an intake database to the latest version.
|
|
|
|
Note that the database will be created if it does not exist, even with --list.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
migrate()
|
|
},
|
|
}
|
|
|
|
var migrateListOnly bool
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(migrateCmd)
|
|
|
|
migrateCmd.Flags().BoolVarP(&migrateListOnly, "list", "l", false, "Show the list of migrations")
|
|
}
|
|
|
|
func migrate() {
|
|
db, err := sql.Open("sqlite3", getDbPath())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
core.InitDatabase(db)
|
|
if migrateListOnly {
|
|
pending, err := core.GetPendingMigrations(db)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for name, complete := range pending {
|
|
if complete {
|
|
fmt.Printf("[x] %s\n", name)
|
|
} else {
|
|
fmt.Printf("[ ] %s\n", name)
|
|
}
|
|
}
|
|
} else {
|
|
core.MigrateDatabase(db)
|
|
}
|
|
}
|