Add migrate command for initializing databases

This commit is contained in:
Tim Van Baak 2025-01-16 21:30:41 -08:00
parent 96ab254812
commit 43fb2c3917
3 changed files with 82 additions and 14 deletions

54
cmd/migrate.go Normal file
View File

@ -0,0 +1,54 @@
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)
}
}

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"fmt"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -19,7 +20,25 @@ func Execute() {
} }
} }
var dbPath string
func init() { func init() {
// Disable the automatic help command // Disable the automatic help command
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
// All commands need to operate on a database
rootCmd.PersistentFlags().StringVarP(&dbPath, "db", "d", "", "Path to the intake sqlite database")
}
func getDbPath() string {
if dbPath != "" {
return dbPath
}
env := os.Getenv("INTAKE_DB")
if env != "" {
return env
}
fmt.Println("error: No database specified. Either --db or INTAKE_DB must be set.")
os.Exit(1)
return ""
} }

View File

@ -51,8 +51,8 @@ func InitDatabase(db *sql.DB) error {
return err return err
} }
// Get the names of existing migrations that haven't been applied yet. // Get a map of migration names to whether the migration has been applied.
func GetPendingMigrations(db *sql.DB) ([]string, error) { func GetPendingMigrations(db *sql.DB) (map[string]bool, error) {
allMigrations, err := migrations.ReadDir("sql") allMigrations, err := migrations.ReadDir("sql")
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,14 +73,7 @@ func GetPendingMigrations(db *sql.DB) ([]string, error) {
complete[name] = true complete[name] = true
} }
var pending []string return complete, nil
for name, isComplete := range complete {
if !isComplete {
pending = append(pending, name)
}
}
return pending, nil
} }
// Apply a migration by name. // Apply a migration by name.
@ -104,10 +97,12 @@ func MigrateDatabase(db *sql.DB) error {
if err != nil { if err != nil {
return err return err
} }
for _, name := range pending { for name, complete := range pending {
err = ApplyMigration(db, name) if !complete {
if err != nil { err = ApplyMigration(db, name)
return err if err != nil {
return err
}
} }
} }
return nil return nil