Init: bootstrap go module with basic framework
This commit is contained in:
15
cmd/migrates/init.go
Normal file
15
cmd/migrates/init.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package migrates
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize migration table",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
return migrator.Init(ctx)
|
||||
},
|
||||
}
|
||||
13
cmd/migrates/lock.go
Normal file
13
cmd/migrates/lock.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package migrates
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var lockCmd = &cobra.Command{
|
||||
Use: "lock",
|
||||
Short: "Lock migrations",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return migrator.Lock(cmd.Context())
|
||||
},
|
||||
}
|
||||
33
cmd/migrates/rollback.go
Normal file
33
cmd/migrates/rollback.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package migrates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rollbackCmd = &cobra.Command{
|
||||
Use: "rollback",
|
||||
Short: "Rollback from last migration group",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
if err := migrator.Lock(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
defer migrator.Unlock(ctx)
|
||||
|
||||
group, err := migrator.Rollback(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if group.IsZero() {
|
||||
fmt.Printf("there are no groups to roll back\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("roll back from %s\n", group)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
44
cmd/migrates/root.go
Normal file
44
cmd/migrates/root.go
Normal file
@@ -0,0 +1,44 @@
|
||||
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")
|
||||
}
|
||||
11
cmd/migrates/unlock.go
Normal file
11
cmd/migrates/unlock.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package migrates
|
||||
|
||||
import "github.com/spf13/cobra"
|
||||
|
||||
var unlockCmd = &cobra.Command{
|
||||
Use: "unlock",
|
||||
Short: "Unlock migrations",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return migrator.Unlock(cmd.Context())
|
||||
},
|
||||
}
|
||||
33
cmd/migrates/up.go
Normal file
33
cmd/migrates/up.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package migrates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var upCmd = &cobra.Command{
|
||||
Use: "up",
|
||||
Short: "migrate up",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
if err := migrator.Lock(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
defer migrator.Unlock(ctx)
|
||||
|
||||
group, err := migrator.Migrate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if group.IsZero() {
|
||||
fmt.Printf("there are no new migrations to run\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Printf("migrated to %s\n", group)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user