You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flux-feed/cmd/migrate.go

49 lines
1.2 KiB
Go

// Package cmd migrate provides a database migration command to root cli
package cmd
import (
"database/sql"
"log"
"git.runcible.io/androiddrew/flux-feed/config"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file" //migrations
_ "github.com/mattn/go-sqlite3" //migrations
"github.com/spf13/cobra"
)
func runMigrate(_ *cobra.Command, _ []string) {
cfg := config.New()
db, err := sql.Open(cfg.DatabaseDriver, cfg.DatabaseDSN)
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatal(err)
}
log.Printf("Connected to Database: %s", cfg.DatabaseDSN)
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{})
log.Printf("Using migrations path: %s", cfg.MigrationsPath)
m, err := migrate.NewWithDatabaseInstance(
cfg.MigrationsPath, cfg.DatabaseDriver, driver)
if err != nil {
log.Fatal(err)
}
log.Printf("Migrating: %s", cfg.DatabaseDSN)
err = m.Up()
if err != nil {
log.Fatal(err)
}
}
// MigrateCmd provides database migrations command for cli
var MigrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run database migrations",
Run: runMigrate,
}