forked from opengaussexamples/examples
166 lines
3.8 KiB
Go
166 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"gitee.com/chentanyang/ogx"
|
|
"gitee.com/chentanyang/ogx/dialect/ogdialect"
|
|
"gitee.com/chentanyang/ogx/example/migrate/migrations"
|
|
"gitee.com/chentanyang/ogx/extra/ogxdebug"
|
|
"gitee.com/chentanyang/ogx/migrate"
|
|
"github.com/urfave/cli/v2"
|
|
|
|
_ "gitee.com/opengauss/openGauss-connector-go-pq"
|
|
)
|
|
|
|
func main() {
|
|
connStr := "host=192.168.20.40 port=26000 user=cuih password=Gauss@123 dbname=test sslmode=disable"
|
|
opengaussdb, err := sql.Open("opengauss", connStr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
db := ogx.NewDB(opengaussdb, ogdialect.New())
|
|
db.AddQueryHook(ogxdebug.NewQueryHook(
|
|
ogxdebug.WithEnabled(false),
|
|
ogxdebug.FromEnv(""),
|
|
))
|
|
|
|
app := &cli.App{
|
|
Name: "ogx",
|
|
|
|
Commands: []*cli.Command{
|
|
newDBCommand(migrate.NewMigrator(db, migrations.Migrations)),
|
|
},
|
|
}
|
|
if err := app.Run(os.Args); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func newDBCommand(migrator *migrate.Migrator) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "db",
|
|
Usage: "database migrations",
|
|
Subcommands: []*cli.Command{
|
|
{
|
|
Name: "init",
|
|
Usage: "create migration tables",
|
|
Action: func(c *cli.Context) error {
|
|
return migrator.Init(c.Context)
|
|
},
|
|
},
|
|
{
|
|
Name: "migrate",
|
|
Usage: "migrate database",
|
|
Action: func(c *cli.Context) error {
|
|
group, err := migrator.Migrate(c.Context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if group.IsZero() {
|
|
fmt.Printf("there are no new migrations to run (database is up to date)\n")
|
|
return nil
|
|
}
|
|
fmt.Printf("migrated to %s\n", group)
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "rollback",
|
|
Usage: "rollback the last migration group",
|
|
Action: func(c *cli.Context) error {
|
|
group, err := migrator.Rollback(c.Context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if group.IsZero() {
|
|
fmt.Printf("there are no groups to roll back\n")
|
|
return nil
|
|
}
|
|
fmt.Printf("rolled back %s\n", group)
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "lock",
|
|
Usage: "lock migrations",
|
|
Action: func(c *cli.Context) error {
|
|
return migrator.Lock(c.Context)
|
|
},
|
|
},
|
|
{
|
|
Name: "unlock",
|
|
Usage: "unlock migrations",
|
|
Action: func(c *cli.Context) error {
|
|
return migrator.Unlock(c.Context)
|
|
},
|
|
},
|
|
{
|
|
Name: "create_go",
|
|
Usage: "create Go migration",
|
|
Action: func(c *cli.Context) error {
|
|
name := strings.Join(c.Args().Slice(), "_")
|
|
mf, err := migrator.CreateGoMigration(c.Context, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path)
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "create_sql",
|
|
Usage: "create up and down SQL migrations",
|
|
Action: func(c *cli.Context) error {
|
|
name := strings.Join(c.Args().Slice(), "_")
|
|
files, err := migrator.CreateSQLMigrations(c.Context, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, mf := range files {
|
|
fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "status",
|
|
Usage: "print migrations status",
|
|
Action: func(c *cli.Context) error {
|
|
ms, err := migrator.MigrationsWithStatus(c.Context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("migrations: %s\n", ms)
|
|
fmt.Printf("unapplied migrations: %s\n", ms.Unapplied())
|
|
fmt.Printf("last migration group: %s\n", ms.LastGroup())
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "mark_applied",
|
|
Usage: "mark migrations as applied without actually running them",
|
|
Action: func(c *cli.Context) error {
|
|
group, err := migrator.Migrate(c.Context, migrate.WithNopMigration())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if group.IsZero() {
|
|
fmt.Printf("there are no new migrations to mark as applied\n")
|
|
return nil
|
|
}
|
|
fmt.Printf("marked as applied %s\n", group)
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|