From 34ccc98a942098faefb5f4211b215ff9ccc7ad0e Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Mon, 9 Dec 2024 01:07:15 +0300 Subject: Начальный MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/migrator/migrate/init.go | 18 +++++++++++++ cmd/migrator/migrate/migrate.go | 31 ++++++++++++++++++++++ cmd/migrator/migrate/rollback.go | 31 ++++++++++++++++++++++ cmd/migrator/migrator.go | 54 +++++++++++++++++++++++++++++++++++++++ cmd/migrator/migrator/migrator.go | 21 +++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 cmd/migrator/migrate/init.go create mode 100644 cmd/migrator/migrate/migrate.go create mode 100644 cmd/migrator/migrate/rollback.go create mode 100644 cmd/migrator/migrator.go create mode 100644 cmd/migrator/migrator/migrator.go (limited to 'cmd/migrator') diff --git a/cmd/migrator/migrate/init.go b/cmd/migrator/migrate/init.go new file mode 100644 index 0000000..6f06c2d --- /dev/null +++ b/cmd/migrator/migrate/init.go @@ -0,0 +1,18 @@ +package migrate + +import ( + "context" + + "git.neonxp.ru/neonxp/guessr/cmd/migrator/migrator" + "git.neonxp.ru/neonxp/guessr/migrations" + "github.com/urfave/cli/v3" +) + +func Init(ctx context.Context, c *cli.Command) error { + m, err := migrator.New(c, migrations.Migrations) + if err != nil { + return err + } + + return m.Init(ctx) +} diff --git a/cmd/migrator/migrate/migrate.go b/cmd/migrator/migrate/migrate.go new file mode 100644 index 0000000..6245879 --- /dev/null +++ b/cmd/migrator/migrate/migrate.go @@ -0,0 +1,31 @@ +package migrate + +import ( + "context" + "fmt" + + "git.neonxp.ru/neonxp/guessr/cmd/migrator/migrator" + "git.neonxp.ru/neonxp/guessr/migrations" + "github.com/urfave/cli/v3" +) + +func Migrate(ctx context.Context, c *cli.Command) error { + m, err := migrator.New(c, migrations.Migrations) + if err != nil { + return err + } + + group, err := m.Migrate(ctx) + if err != nil { + return err + } + + if group.ID == 0 { + fmt.Printf("there are no new migrations to run\n") + return nil + } + + fmt.Printf("migrated to %s\n", group) + + return nil +} diff --git a/cmd/migrator/migrate/rollback.go b/cmd/migrator/migrate/rollback.go new file mode 100644 index 0000000..af56654 --- /dev/null +++ b/cmd/migrator/migrate/rollback.go @@ -0,0 +1,31 @@ +package migrate + +import ( + "context" + "fmt" + + "git.neonxp.ru/neonxp/guessr/cmd/migrator/migrator" + "git.neonxp.ru/neonxp/guessr/migrations" + "github.com/urfave/cli/v3" +) + +func Rollback(ctx context.Context, c *cli.Command) error { + m, err := migrator.New(c, migrations.Migrations) + if err != nil { + return err + } + + group, err := m.Rollback(ctx) + if err != nil { + return err + } + + if group.ID == 0 { + fmt.Printf("there are no groups to roll back\n") + return nil + } + + fmt.Printf("rolled back %s\n", group) + + return nil +} diff --git a/cmd/migrator/migrator.go b/cmd/migrator/migrator.go new file mode 100644 index 0000000..31f8dab --- /dev/null +++ b/cmd/migrator/migrator.go @@ -0,0 +1,54 @@ +package migrator + +import ( + "context" + "fmt" + "strings" + + "git.neonxp.ru/neonxp/guessr/cmd/migrator/migrate" + "git.neonxp.ru/neonxp/guessr/cmd/migrator/migrator" + "git.neonxp.ru/neonxp/guessr/migrations" + "github.com/urfave/cli/v3" +) + +var Command = &cli.Command{ + Name: "db", + Usage: "manage database migrations", + Commands: []*cli.Command{ + { + Name: "init", + Usage: "create migration tables", + Action: migrate.Init, + }, + { + Name: "migrate", + Usage: "migrate database", + Action: migrate.Migrate, + }, + { + Name: "rollback", + Usage: "rollback the last migration group", + Action: migrate.Rollback, + }, + + { + Name: "create", + Usage: "create migration", + Action: func(ctx context.Context, c *cli.Command) error { + migrator, err := migrator.New(c, migrations.Migrations) + if err != nil { + return err + } + + name := strings.Join(c.Args().Slice(), "_") + mf, err := migrator.CreateGoMigration(ctx, name) + if err != nil { + return err + } + fmt.Printf("created migration %s (%s)\n", mf.Name, mf.Path) + + return nil + }, + }, + }, +} diff --git a/cmd/migrator/migrator/migrator.go b/cmd/migrator/migrator/migrator.go new file mode 100644 index 0000000..f487e64 --- /dev/null +++ b/cmd/migrator/migrator/migrator.go @@ -0,0 +1,21 @@ +package migrator + +import ( + "git.neonxp.ru/neonxp/guessr/pkg/config" + "git.neonxp.ru/neonxp/guessr/pkg/db" + "github.com/uptrace/bun/migrate" + "github.com/urfave/cli/v3" +) + +func New(c *cli.Command, mig *migrate.Migrations) (*migrate.Migrator, error) { + cfg, err := config.New() + if err != nil { + return nil, err + } + + dbClient := db.New(cfg.DB) + + migrator := migrate.NewMigrator(dbClient, mig) + + return migrator, nil +} -- cgit v1.2.3