62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
/*
|
|
Package cmd
|
|
Copyright © 2024 Shane C. <shane@scaffoe.com>
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/uptrace/bun"
|
|
"github.com/uptrace/bun/dialect/pgdialect"
|
|
"github.com/uptrace/bun/migrate"
|
|
"log"
|
|
"omnibill.net/omnibill/migrations"
|
|
"omnibill.net/omnibill/shared"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// lockCmd represents the lock command
|
|
var lockCmd = &cobra.Command{
|
|
Use: "lock",
|
|
Short: "Locks DB migrations",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
dsn := shared.GetPostgresURI()
|
|
|
|
config, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
pool, err := pgxpool.NewWithConfig(context.Background(), config)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
sqldb := stdlib.OpenDBFromPool(pool)
|
|
db := bun.NewDB(sqldb, pgdialect.New())
|
|
|
|
migrator := migrate.NewMigrator(db, migrations.Migrations)
|
|
ctx := context.Background()
|
|
|
|
err = migrator.Lock(ctx)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
migrateCmd.AddCommand(lockCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// lockCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// lockCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|