Skip to content

Commit

Permalink
Fix dev CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mgdigital committed Jan 6, 2024
1 parent c6d7e92 commit bed99fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
24 changes: 19 additions & 5 deletions internal/database/migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrations
import (
"context"
"database/sql"
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
migrationssql "github.com/bitmagnet-io/bitmagnet/migrations"
goose "github.com/pressly/goose/v3"
"go.uber.org/fx"
Expand All @@ -11,14 +12,27 @@ import (

type Params struct {
fx.In
DB *sql.DB
DB lazy.Lazy[*sql.DB]
Logger *zap.SugaredLogger
}

func New(p Params) Migrator {
initGoose(p.Logger)
return &migrator{
db: p.DB,
type Result struct {
fx.Out
Migrator lazy.Lazy[Migrator]
}

func New(p Params) Result {
return Result{
Migrator: lazy.New(func() (Migrator, error) {
db, err := p.DB.Get()
if err != nil {
return nil, err
}
initGoose(p.Logger)
return &migrator{
db: db,
}, nil
}),
}
}

Expand Down
9 changes: 7 additions & 2 deletions internal/dev/app/cmd/gormcmd/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gormcmd

import (
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/database/gen"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
Expand All @@ -9,7 +10,7 @@ import (

type Params struct {
fx.In
DB *gorm.DB
DB lazy.Lazy[*gorm.DB]
}

type Result struct {
Expand All @@ -24,7 +25,11 @@ func New(p Params) (r Result, err error) {
{
Name: "gen",
Action: func(ctx *cli.Context) error {
g := gen.BuildGenerator(p.DB)
db, err := p.DB.Get()
if err != nil {
return err
}
g := gen.BuildGenerator(db)
g.Execute()
return nil
},
Expand Down
19 changes: 14 additions & 5 deletions internal/dev/app/cmd/migratecmd/command.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package migratecmd

import (
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
"github.com/bitmagnet-io/bitmagnet/internal/database/migrations"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
)

type Params struct {
fx.In
Migrator migrations.Migrator
Migrator lazy.Lazy[migrations.Migrator]
}

type Result struct {
Expand All @@ -29,11 +30,15 @@ func New(p Params) (r Result, err error) {
},
},
Action: func(ctx *cli.Context) error {
m, err := p.Migrator.Get()
if err != nil {
return err
}
version := ctx.Int64("version")
if version == 0 {
return p.Migrator.Up(ctx.Context)
return m.Up(ctx.Context)
} else {
return p.Migrator.UpTo(ctx.Context, version)
return m.UpTo(ctx.Context, version)
}
},
},
Expand All @@ -46,11 +51,15 @@ func New(p Params) (r Result, err error) {
},
},
Action: func(ctx *cli.Context) error {
m, err := p.Migrator.Get()
if err != nil {
return err
}
version := ctx.Int64("version")
if version == 0 {
return p.Migrator.Down(ctx.Context)
return m.Down(ctx.Context)
} else {
return p.Migrator.DownTo(ctx.Context, version)
return m.DownTo(ctx.Context, version)
}
},
},
Expand Down

0 comments on commit bed99fd

Please sign in to comment.