Add cmd for generating code from the new models
Some checks are pending
/ test (push) Waiting to run
Some checks are pending
/ test (push) Waiting to run
This commit is contained in:
parent
67b507f4bd
commit
bf5180559f
9 changed files with 401 additions and 132 deletions
80
cmd/model-gen/main.go
Normal file
80
cmd/model-gen/main.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"time"
|
||||
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
postgresContainer "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
"github.com/testcontainers/testcontainers-go/wait"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/shared"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
)
|
||||
|
||||
const (
|
||||
dbName = "linstrom"
|
||||
dbUser = "linstrom"
|
||||
dbPass = "linstrom"
|
||||
)
|
||||
|
||||
func main() {
|
||||
other.SetupFlags()
|
||||
flag.Parse()
|
||||
other.ConfigureLogging(nil)
|
||||
|
||||
// Set up a temporary postgres container for gorm-gen to do its thing
|
||||
log.Info().Msg("Starting temporary postgres container")
|
||||
pgContainer, err := postgresContainer.Run(
|
||||
context.Background(),
|
||||
"postgres:16.4-alpine",
|
||||
postgresContainer.WithDatabase(dbName),
|
||||
postgresContainer.WithUsername(dbUser),
|
||||
postgresContainer.WithPassword(dbPass),
|
||||
testcontainers.WithWaitStrategyAndDeadline(
|
||||
time.Minute,
|
||||
wait.ForLog("database system is ready to accept connections").
|
||||
WithOccurrence(2).
|
||||
WithStartupTimeout(time.Second*5),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to setup temporary postgres container")
|
||||
}
|
||||
log.Info().Msg("Temporary postgres container started")
|
||||
defer func() {
|
||||
if err := testcontainers.TerminateContainer(pgContainer); err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to terminate temporary postgres container")
|
||||
}
|
||||
log.Info().Msg("Temporary postgres container stopped")
|
||||
}()
|
||||
db, err := gorm.Open(
|
||||
postgres.Open(pgContainer.MustConnectionString(context.Background())),
|
||||
&gorm.Config{
|
||||
PrepareStmt: false,
|
||||
Logger: shared.NewGormLogger(log.Logger),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to open connection to temporary container")
|
||||
}
|
||||
log.Info().Msg("Connected to db")
|
||||
g := gen.NewGenerator(gen.Config{
|
||||
OutPath: "./dbgen",
|
||||
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
||||
})
|
||||
g.UseDB(db)
|
||||
|
||||
log.Info().Msg("Applying basic operations on all datatypes")
|
||||
g.ApplyBasic(models.AllTypes...)
|
||||
|
||||
log.Info().Msg("Starting generation")
|
||||
g.Execute()
|
||||
log.Info().Msg("Code generation complete")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue