Skip to content

Commit

Permalink
Fix/codes (#46)
Browse files Browse the repository at this point in the history
* fix: barry 2024-08-30 11:31:12

* fix: barry 2024-08-30 11:32:15

* Refactor migrate.go command structure

* fix: barry 2024-09-11 22:32:37
  • Loading branch information
kooksee authored Sep 21, 2024
1 parent b5f6ca8 commit 31a1770
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
22 changes: 11 additions & 11 deletions core/entmigrates/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (g *GoMigrate) Create(tables []*schema.Table) error {
}

now := time.Now()
name := fmt.Sprintf("sql%d", now.UnixNano())
name := fmt.Sprintf("sql%s", now.Format("20060102150405"))
filename := fmt.Sprintf("%s.sql", name)
assert.Must(os.MkdirAll(g.config.MigrationPath, 0o755))
filename = filepath.Join(g.config.MigrationPath, filename)
Expand All @@ -127,20 +127,20 @@ func (g *GoMigrate) Create(tables []*schema.Table) error {
return err
}

mmPkg := "github.com/pubgo/chameleon/internal/components/migrates"
mmPkg := "github.com/pubgo/lava/core/entmigrates"
genFile := jen.NewFile("migrations")
genFile.HeaderComment("DO NOT EDIT, THIS FILE IS GENERATED BY ENTORMIGRATE.")
genFile.HeaderComment("DO NOT EDIT, THIS FILE IS GENERATED BY ENTORMIGRATE.")
genFile.HeaderComment("DO NOT EDIT, THIS FILE IS GENERATED BY ENTORMIGRATE.")
genFile.Line()
genFile.Anon("embed")
genFile.ImportName(mmPkg, "migrates")
genFile.ImportName(mmPkg, "entmigrates")
genFile.Commentf("//go:embed %s.sql", name)
genFile.Var().Id(fmt.Sprintf("%sData string", name))
genFile.Var().Id("_").Qual(mmPkg, "Migration")
genFile.Id(fmt.Sprintf(`
func %s() *migrates.Migration {
return &migrates.Migration{
func %s() *entmigrates.Migration {
return &entmigrates.Migration{
Name: "%s",
Description: "%s",
MigrationSql: %sData,
Expand Down Expand Up @@ -185,44 +185,44 @@ func (g *GoMigrate) Cmd() *cli.Command {
Name: "migrate",
Usage: "migrate database schema",
Commands: []*cli.Command{
&cli.Command{
{
Name: "migrate",
Usage: "migrate all migrations",
Action: func(ctx context.Context, command *cli.Command) error {
return g.Migrate()
},
},
&cli.Command{
{
Name: "migrate_to",
Usage: "migrate_to <name>",
UsageText: "migrate migrate_to <name>",
Action: func(ctx context.Context, c *cli.Command) error {
return g.MigrateTo(c.Args().First())
},
},
&cli.Command{
{
Name: "rollback",
Usage: "rollback last migration",
Action: func(ctx context.Context, command *cli.Command) error {
return g.RollbackLast()
},
},
&cli.Command{
{
Name: "rollback_to",
Usage: "rollback_to <name>",
Action: func(ctx context.Context, c *cli.Command) error {
return g.RollbackTo(c.Args().First())
},
},

&cli.Command{
{
Name: "create",
Usage: "create migration",
Action: func(ctx context.Context, command *cli.Command) error {
return g.Create(g.tables)
},
},
&cli.Command{
{
Name: "list",
Usage: "list all migrations",
Action: func(ctx context.Context, command *cli.Command) error {
Expand Down
5 changes: 2 additions & 3 deletions servers/grpcs/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/rs/xid"
"github.com/valyala/fasthttp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"

Expand Down Expand Up @@ -161,7 +160,7 @@ func handlerUnaryMiddle(middlewares map[string][]lava.Middleware) grpc.UnaryServ

if pb.Code.Code == 0 {
pb.Code.StatusCode = errorpb.Code_Internal
pb.Code.Code = int32(errutil.GrpcCodeToHTTP(codes.Code(uint32(errorpb.Code_Internal))))
pb.Code.Code = int32(errorpb.Code_Internal)
}

return nil, errutil.ConvertErr2Status(pb).Err()
Expand Down Expand Up @@ -266,7 +265,7 @@ func handlerStreamMiddle(middlewares map[string][]lava.Middleware) grpc.StreamSe
}

if pb.Code.Code == 0 {
pb.Code.Code = int32(errutil.GrpcCodeToHTTP(codes.Code(pb.Code.StatusCode)))
pb.Code.Code = int32(errorpb.Code_Internal)
pb.Code.StatusCode = errorpb.Code_Internal
}

Expand Down
6 changes: 3 additions & 3 deletions servers/grpcs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (s *serviceImpl) DixInject(
pb = &errorpb.ErrCode{
Message: err.Error(),
StatusCode: errorpb.Code_Internal,
Code: 500,
Code: int32(errorpb.Code_Internal),
Name: "lava.grpc.status",
}
} else {
Expand All @@ -280,15 +280,15 @@ func (s *serviceImpl) DixInject(
} else {
pb = &errorpb.ErrCode{
Message: sts.Message(),
Code: 500,
Code: int32(errorpb.Code(sts.Code())),
StatusCode: errorpb.Code(sts.Code()),
Name: "lava.grpc.status",
Details: sts.Proto().Details,
}
}
}

const fallback = `{"code":500, "name":"lava.grpc.status", "status_code": 13, "message": "failed to marshal error message"}`
const fallback = `{"code":13, "name":"lava.grpc.status", "status_code": 500, "message": "failed to marshal error message"}`

// skip error
if pb.StatusCode == errorpb.Code_OK {
Expand Down

0 comments on commit 31a1770

Please sign in to comment.