mirror of
https://github.com/amigan/aim-oscar-server.git
synced 2024-11-21 12:09:48 -05:00
remove fixtures
This commit is contained in:
parent
ab652e5e28
commit
2b06886058
7 changed files with 277 additions and 8 deletions
BIN
cmd/.DS_Store
vendored
Normal file
BIN
cmd/.DS_Store
vendored
Normal file
Binary file not shown.
235
cmd/bun/main.go
Normal file
235
cmd/bun/main.go
Normal file
|
@ -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"
|
||||||
|
}
|
19
cmd/bun/migrations/20210505110026_init.go
Normal file
19
cmd/bun/migrations/20210505110026_init.go
Normal file
|
@ -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)
|
||||||
|
}
|
5
cmd/bun/migrations/20210505110847_sql.up.sql
Normal file
5
cmd/bun/migrations/20210505110847_sql.up.sql
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
SELECT 1
|
||||||
|
|
||||||
|
--bun:split
|
||||||
|
|
||||||
|
SELECT 2
|
18
cmd/bun/migrations/main.go
Normal file
18
cmd/bun/migrations/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
8
main.go
8
main.go
|
@ -21,7 +21,6 @@ import (
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
"github.com/uptrace/bun/dbfixture"
|
|
||||||
"github.com/uptrace/bun/dialect/pgdialect"
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
"github.com/uptrace/bun/driver/pgdriver"
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
"github.com/uptrace/bun/extra/bundebug"
|
"github.com/uptrace/bun/extra/bundebug"
|
||||||
|
@ -128,13 +127,6 @@ func main() {
|
||||||
// Register our DB models
|
// Register our DB models
|
||||||
db.RegisterModel((*models.User)(nil), (*models.Message)(nil), (*models.Buddy)(nil), (*models.EmailVerification)(nil))
|
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)
|
listener, err := net.Listen("tcp", OSCAR_ADDRESS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error listening: ", err.Error())
|
fmt.Println("Error listening: ", err.Error())
|
||||||
|
|
Loading…
Reference in a new issue