Files
amane-tanikaze-dcbot/cmd/migrates/root.go

45 lines
1.1 KiB
Go

package migrates
import (
"database/sql"
"gitea.konchin.com/service/amane-tanikaze-dcbot/amane/migrations"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
"github.com/uptrace/bun/migrate"
)
var (
migrator *migrate.Migrator
db *bun.DB
)
var RootCmd = &cobra.Command{
Use: "migrate",
Short: "Migration related commands",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
sqldb := sql.OpenDB(pgdriver.NewConnector(
pgdriver.WithDSN(viper.GetString("pg-dsn"))))
db = bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
migrator = migrate.NewMigrator(db, migrations.Migrations)
},
}
func init() {
RootCmd.AddCommand(upCmd)
RootCmd.AddCommand(initCmd)
RootCmd.AddCommand(rollbackCmd)
RootCmd.AddCommand(lockCmd)
RootCmd.AddCommand(unlockCmd)
RootCmd.PersistentFlags().
String("pg-dsn",
"postgres://amane:amane@postgres:5432/amane?sslmode=disable",
"Postgres connection string")
}