diff --git a/cmd/.DS_Store b/cmd/.DS_Store new file mode 100644 index 0000000..3d01f9e Binary files /dev/null and b/cmd/.DS_Store differ diff --git a/cmd/bun/main.go b/cmd/bun/main.go new file mode 100644 index 0000000..c377dd9 --- /dev/null +++ b/cmd/bun/main.go @@ -0,0 +1,235 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/go-bun/bun-starter-kit/bunapp" + "github.com/go-bun/bun-starter-kit/cmd/bun/migrations" + "github.com/go-bun/bun/migrate" + + cli "github.com/urfave/cli/v2" +) + +func main() { + app := &cli.App{ + Name: "bun", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "env", + Value: "dev", + Usage: "environment", + }, + }, + Commands: []*cli.Command{ + newDBCommand(migrations.Migrations), + }, + } + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } +} +func newDBCommand(migrations *migrate.Migrations) *cli.Command { + return &cli.Command{ + Name: "db", + Usage: "manage database migrations", + Subcommands: []*cli.Command{ + { + Name: "init", + Usage: "create migration tables", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + return migrator.Init(ctx) + }, + }, + { + Name: "migrate", + Usage: "migrate database", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + group, err := migrator.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 + }, + }, + { + Name: "rollback", + Usage: "rollback the last migration group", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + group, err := migrator.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 + }, + }, + { + Name: "lock", + Usage: "lock migrations", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + return migrator.Lock(ctx) + }, + }, + { + Name: "unlock", + Usage: "unlock migrations", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + return migrator.Unlock(ctx) + }, + }, + { + Name: "create_go", + Usage: "create Go migration", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + 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 + }, + }, + { + Name: "create_sql", + Usage: "create up and down SQL migrations", + Action: func(c *cli.Context) error { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + name := strings.Join(c.Args().Slice(), "_") + files, err := migrator.CreateSQLMigrations(ctx, 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 { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + ms, err := migrator.MigrationsWithStatus(ctx) + 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 { + ctx, app, err := bunapp.StartCLI(c) + if err != nil { + return err + } + defer app.Stop() + + migrator := migrate.NewMigrator(app.DB(), migrations) + + group, err := migrator.Migrate(ctx, migrate.WithNopMigration()) + if err != nil { + return err + } + + if group.ID == 0 { + fmt.Printf("there are no new migrations to mark as applied\n") + return nil + } + + fmt.Printf("marked as applied %s\n", group) + return nil + }, + }, + }, + } +} + +func isServerClosed(err error) bool { + return err.Error() == "http: Server closed" +} diff --git a/cmd/bun/migrations/20210505110026_init.go b/cmd/bun/migrations/20210505110026_init.go new file mode 100644 index 0000000..26b0d18 --- /dev/null +++ b/cmd/bun/migrations/20210505110026_init.go @@ -0,0 +1,19 @@ +package migrations + +import ( + "aim-oscar/models" + "context" + "os" + + "github.com/uptrace/bun" + "github.com/uptrace/bun/dbfixture" +) + +func init() { + Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error { + db.RegisterModel((*models.User)(nil), (*models.Message)(nil), (*models.Buddy)(nil), (*models.EmailVerification)(nil)) + + fixture := dbfixture.New(db, dbfixture.WithRecreateTables()) + return fixture.Load(context.Background(), os.DirFS("./"), "init_fixtures.yml") + }, nil) +} diff --git a/cmd/bun/migrations/20210505110847_sql.up.sql b/cmd/bun/migrations/20210505110847_sql.up.sql new file mode 100644 index 0000000..e40ae41 --- /dev/null +++ b/cmd/bun/migrations/20210505110847_sql.up.sql @@ -0,0 +1,5 @@ +SELECT 1 + +--bun:split + +SELECT 2 diff --git a/models/fixtures.yml b/cmd/bun/migrations/init_fixtures.yml similarity index 100% rename from models/fixtures.yml rename to cmd/bun/migrations/init_fixtures.yml diff --git a/cmd/bun/migrations/main.go b/cmd/bun/migrations/main.go new file mode 100644 index 0000000..7dfd5e0 --- /dev/null +++ b/cmd/bun/migrations/main.go @@ -0,0 +1,18 @@ +package migrations + +import ( + "embed" + + "github.com/uptrace/bun/migrate" +) + +var Migrations = migrate.NewMigrations() + +//go:embed *.sql +var sqlMigrations embed.FS + +func init() { + if err := Migrations.Discover(sqlMigrations); err != nil { + panic(err) + } +} diff --git a/main.go b/main.go index 65923d3..e4c6c44 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ import ( "github.com/pkg/errors" "github.com/uptrace/bun" - "github.com/uptrace/bun/dbfixture" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/driver/pgdriver" "github.com/uptrace/bun/extra/bundebug" @@ -128,13 +127,6 @@ func main() { // Register our DB models db.RegisterModel((*models.User)(nil), (*models.Message)(nil), (*models.Buddy)(nil), (*models.EmailVerification)(nil)) - // dev: load in fixtures to test against - fixture := dbfixture.New(db, dbfixture.WithRecreateTables()) - err := fixture.Load(context.Background(), os.DirFS("./models"), "fixtures.yml") - if err != nil { - panic(err) - } - listener, err := net.Listen("tcp", OSCAR_ADDRESS) if err != nil { fmt.Println("Error listening: ", err.Error())