fix: issues

This commit is contained in:
Rogee
2024-12-19 19:21:06 +08:00
parent 50ba1baece
commit 28f631a2d3
5 changed files with 1 additions and 221 deletions

View File

@@ -10,14 +10,9 @@ GOPATH:=$(shell go env GOPATH)
tidy: tidy:
@go mod tidy @go mod tidy
.PHONY: dist
dist:
@go build -ldflags=${flags} -o bin/debug/atom{{.ProjectName}}
@cp config.toml bin/debug/
.PHONY: release .PHONY: release
release: release:
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/release/{{.ProjectName}} . @CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags=${flags} -o bin/release/{{.ProjectName}} .
@cp config.toml bin/release/ @cp config.toml bin/release/
.PHONY: test .PHONY: test
@@ -27,24 +22,3 @@ test:
.PHONY: lint .PHONY: lint
lint: lint:
@golangci-lint run @golangci-lint run
.PHONY: proto
proto:
@buf generate
.PHONY: fresh
fresh:
@go run . migrate down
@go run . migrate up
.PHONY: mup
mup:
@go run . migrate up
.PHONY: mdown
mdown:
@go run . migrate down
.PHONY: model
model:
go test -run ^Test_GenModel

View File

@@ -2,8 +2,6 @@ package main
import ( import (
"{{.ModuleName}}/pkg/service/http" "{{.ModuleName}}/pkg/service/http"
"{{.ModuleName}}/pkg/service/migrate"
"{{.ModuleName}}/pkg/service/model"
"git.ipao.vip/rogeecn/atom" "git.ipao.vip/rogeecn/atom"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -13,8 +11,6 @@ func main() {
opts := []atom.Option{ opts := []atom.Option{
atom.Name("{{ .ProjectName }}"), atom.Name("{{ .ProjectName }}"),
http.Command(), http.Command(),
migrate.Command(),
model.Command(),
} }
if err := atom.Serve(opts...); err != nil { if err := atom.Serve(opts...); err != nil {

View File

@@ -3,14 +3,5 @@ package main
import ( import (
"testing" "testing"
"{{.ModuleName}}/pkg/service/model"
"git.ipao.vip/rogeecn/atom" "git.ipao.vip/rogeecn/atom"
) )
func Test_GenModel(t *testing.T) {
err := atom.Serve(model.Options()...)
if err != nil {
t.Fatal(err)
}
}

View File

@@ -1,52 +0,0 @@
package migrate
import (
"context"
"database/sql"
"{{.ModuleName}}/database"
"{{.ModuleName}}/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/pressly/goose/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.uber.org/dig"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
postgres.DefaultProvider(),
}, providers...)
}
func Command() atom.Option {
return atom.Command(
atom.Name("migrate"),
atom.Short("run migrations"),
atom.RunE(Serve),
atom.Providers(Default()),
)
}
type Migrate struct {
dig.In
DB *sql.DB
}
func Serve(cmd *cobra.Command, args []string) error {
return container.Container.Invoke(func(migrate Migrate) error {
if len(args) == 0 {
args = append(args, "up")
}
action, args := args[0], args[1:]
log.Infof("migration action: %s args: %+v", action, args)
goose.SetBaseFS(database.MigrationFS)
goose.SetTableName("migrations")
return goose.RunContext(context.Background(), action, migrate.DB, "migrations", args...)
})
}

View File

@@ -1,129 +0,0 @@
package model
import (
"database/sql"
"fmt"
"strings"
db "{{.ModuleName}}/providers/postgres"
"git.ipao.vip/rogeecn/atom"
"git.ipao.vip/rogeecn/atom/container"
"github.com/go-jet/jet/v2/generator/metadata"
"github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template"
pg "github.com/go-jet/jet/v2/postgres"
"github.com/gofiber/fiber/v3/log"
_ "github.com/lib/pq"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/dig"
)
func Default(providers ...container.ProviderContainer) container.Providers {
return append(container.Providers{
db.DefaultProvider(),
}, providers...)
}
func Options() []atom.Option {
return []atom.Option{
atom.Name("model"),
atom.Short("run model generator"),
atom.RunE(Serve),
atom.Providers(Default()),
atom.Arguments(func(cmd *cobra.Command) {
cmd.Flags().String("path", "./database/models", "generate to path")
cmd.Flags().String("transform", "./database/.transform.yaml", "transform config")
}),
}
}
func Command() atom.Option {
return atom.Command(Options()...)
}
type Migrate struct {
dig.In
DB *sql.DB
Config *db.Config
}
type Transformer struct {
Ignores []string `mapstructure:"ignores"`
Types map[string]map[string]string `mapstructure:"types"`
}
func Serve(cmd *cobra.Command, args []string) error {
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigFile(cmd.Flag("transform").Value.String())
if err := v.ReadInConfig(); err != nil {
return err
}
var conf Transformer
if err := v.Unmarshal(&conf); err != nil {
return err
}
return container.Container.Invoke(func(migrate Migrate) error {
return postgres.GenerateDSN(
migrate.Config.DSN(),
migrate.Config.Schema,
cmd.Flag("path").Value.String(),
template.Default(pg.Dialect).
UseSchema(func(schema metadata.Schema) template.Schema {
return template.
DefaultSchema(schema).
UseModel(
template.
DefaultModel().
UseTable(func(table metadata.Table) template.TableModel {
if lo.Contains(conf.Ignores, table.Name) {
table := template.DefaultTableModel(table)
table.Skip = true
return table
}
return template.DefaultTableModel(table).UseField(func(column metadata.Column) template.TableModelField {
defaultTableModelField := template.DefaultTableModelField(column)
defaultTableModelField = defaultTableModelField.UseTags(fmt.Sprintf(`json:"%s"`, column.Name))
if schema.Name != migrate.Config.Schema {
return defaultTableModelField
}
fields, ok := conf.Types[table.Name]
if !ok {
return defaultTableModelField
}
toType, ok := fields[column.Name]
if !ok {
return defaultTableModelField
}
splits := strings.Split(toType, ".")
typeName := splits[len(splits)-1]
pkgSplits := strings.Split(splits[0], "/")
typePkg := pkgSplits[len(pkgSplits)-1]
defaultTableModelField = defaultTableModelField.
UseType(template.Type{
Name: fmt.Sprintf("%s.%s", typePkg, typeName),
ImportPath: splits[0],
})
log.Infof("Convert table %s field %s type to : %s", table.Name, column.Name, toType)
return defaultTableModelField
})
}),
)
}),
)
})
}