-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
2,203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package ent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/urfave/cli/v3" | ||
|
||
// atlas "ariga.io/atlas/sql/migrate" | ||
"entgo.io/ent/dialect" | ||
"entgo.io/ent/dialect/sql" | ||
"entgo.io/ent/dialect/sql/schema" | ||
"entgo.io/ent/entc" | ||
"entgo.io/ent/entc/gen" | ||
"github.com/pubgo/funk/recovery" | ||
// https://github.com/ent/ent/blob/master/cmd/internal/base/base.go | ||
) | ||
|
||
type params struct { | ||
Drv dialect.Driver | ||
Log log.Logger | ||
Tables []*schema.Table | ||
MigrateOptions []schema.MigrateOption | ||
} | ||
|
||
func New1() *cli.Command { | ||
return &cli.Command{ | ||
Name: "ent", | ||
Usage: "ent manager", | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "gen", | ||
Usage: "do gen query", | ||
Action: func(ctx context.Context, command *cli.Command) error { | ||
defer recovery.Exit() | ||
return entc.Generate("./ent/schema", &gen.Config{ | ||
Features: []gen.Feature{ | ||
gen.FeatureVersionedMigration, | ||
gen.FeatureUpsert, | ||
gen.FeatureSchemaConfig, | ||
gen.FeatureModifier, | ||
gen.FeatureExecQuery, | ||
}, | ||
}) | ||
}, | ||
}, | ||
|
||
{ | ||
Name: "generate migration", | ||
Usage: "automatically generate migration files for your Ent schema:", | ||
Action: func(ctx context.Context, command *cli.Command) error { | ||
// atlas migrate lint \ | ||
// --dev-url="docker://mysql/8/test" \ | ||
// --dir="file://ent/migrate/migrations" \ | ||
// --latest=1 | ||
|
||
// atlas migrate diff migration_name \ | ||
// --dev-url "docker://mysql/8/ent" | ||
// --dir "file://ent/migrate/migrations" \ | ||
// --to "ent://ent/schema" \ | ||
|
||
defer recovery.Exit() | ||
return entc.Generate("./ent/schema", &gen.Config{ | ||
Features: []gen.Feature{ | ||
gen.FeatureVersionedMigration, | ||
gen.FeatureUpsert, | ||
gen.FeatureSchemaConfig, | ||
gen.FeatureModifier, | ||
gen.FeatureExecQuery, | ||
}, | ||
}) | ||
}, | ||
}, | ||
|
||
{ | ||
Name: "apply migration", | ||
Usage: "apply the pending migration files onto the database", | ||
Action: func(ctx context.Context, command *cli.Command) error { | ||
return nil | ||
}, | ||
}, | ||
|
||
// atlas migrate hash \ | ||
// --dir "file://my/project/migrations" | ||
|
||
// atlas migrate hash --dir file://<path-to-your-migration-directory> | ||
// atlas migrate status \ | ||
// --dir "file://ent/migrate/migrations" \ | ||
// --url "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable&search_path=public" | ||
// atlas migrate lint | ||
// atlas migrate lint \ | ||
// --dev-url="docker://mysql/8/test" \ | ||
// --dir="file://ent/migrate/migrations" \ | ||
// --latest=1 | ||
// atlas migrate apply \ | ||
// --dir "file://ent/migrate/migrations" \ | ||
// --url "postgres://postgres:pass@localhost:5432/database?search_path=public&sslmode=disable" | ||
// atlas migrate validate --dir file://<path-to-your-migration-directory> | ||
// atlas migrate hash --dir file://<path-to-your-migration-directory> | ||
// atlas migrate new add_user | ||
|
||
// entproto | ||
// protoc -I=.. --go_out=.. --go-grpc_out=.. --go_opt=paths=source_relative --entgrpc_out=.. --entgrpc_opt=paths=source_relative,schema_path=../../schema --go-grpc_opt=paths=source_relative entpb/entpb.proto | ||
// https://github.com/ent/contrib/blob/master/entproto/cmd/entproto/main.go | ||
// ent new Todo | ||
// ent describe ./ent/schema | ||
// ent gen ./ent/schema | ||
// go run ariga.io/entimport/cmd/entimport -dsn "mysql://root:pass@tcp(localhost:3308)/test" -tables "users" | ||
// atlas migrate apply --dir file://ent/migrate/migrations --url mysql://root:pass@localhost:3306/db | ||
// atlas migrate apply --dir file://ent/migrate/migrations --url mysql://root:pass@localhost:3306/db --baseline 20221114165732 | ||
// atlas migrate status --dir file://ent/migrate/migrations --url postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable&search_path=public | ||
// atlas schema inspect -u "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable&search_path=public" > schema.hcl | ||
|
||
//{ | ||
// Name: "create-migration", | ||
// Usage: "create migration", | ||
// Action: func(context *cli.Context) error { | ||
// defer recovery.Exit() | ||
// ctx := context.Context | ||
// dir, err := atlas.NewLocalDir("./ent/migrate/migrations") | ||
// assert.Must(err) | ||
// | ||
// hash := assert.Must1(dir.Checksum()) | ||
// assert.Must(atlas.WriteSumFile(dir, hash)) | ||
// | ||
// assert.Must(atlas.Validate(dir)) | ||
// | ||
// // Migrate diff options. | ||
// opts := []schema.MigrateOption{ | ||
// schema.WithDir(dir), // provide migration directory | ||
// schema.WithMigrationMode(schema.ModeReplay), // provide migration mode | ||
// schema.WithDialect(dialect.MySQL), // Ent dialect to use | ||
// schema.WithFormatter(atlas.DefaultFormatter), | ||
// schema.WithDropIndex(true), | ||
// schema.WithDropColumn(true), | ||
// } | ||
// | ||
// if len(os.Args) != 2 { | ||
// log.Fatalln("migration name is required. Use: 'go run -mod=mod ent/migrate/main.go <name>'") | ||
// } | ||
// | ||
// var drv dialect.Driver | ||
// migrate, err := schema.NewMigrate(drv, opts...) | ||
// if err != nil { | ||
// return fmt.Errorf("ent/migrate: %w", err) | ||
// } | ||
// | ||
// var Tables []*schema.Table | ||
// if err := migrate.VerifyTableRange(ctx, Tables); err != nil { | ||
// log.Fatalf("failed verifyint range allocations: %v", err) | ||
// } | ||
// | ||
// return migrate.NamedDiff(ctx, "change name", Tables...) | ||
// }, | ||
//}, | ||
}, | ||
} | ||
} | ||
|
||
func Open(driverName, dataSourceName string) (*sql.Driver, error) { | ||
switch driverName { | ||
case dialect.MySQL, dialect.Postgres, dialect.SQLite: | ||
drv, err := sql.Open(driverName, dataSourceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return drv, nil | ||
default: | ||
return nil, fmt.Errorf("unsupported driver: %q", driverName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package gormclient | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pubgo/funk/assert" | ||
"github.com/pubgo/funk/config" | ||
"github.com/pubgo/funk/generic" | ||
"github.com/pubgo/funk/log" | ||
"github.com/pubgo/funk/merge" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
"gorm.io/gorm/schema" | ||
) | ||
|
||
func NewClients(conf map[string]*Config, logs log.Logger) map[string]*Client { | ||
clients := make(map[string]*Client, len(conf)) | ||
for name, c := range conf { | ||
clients[name] = New(c, logs) | ||
} | ||
return clients | ||
} | ||
|
||
func New(conf *Config, logs log.Logger) *Client { | ||
logs = logs.WithName(Name) | ||
conf = config.MergeR(generic.Ptr(DefaultCfg()), conf).Unwrap() | ||
|
||
ormCfg := merge.Copy(new(gorm.Config), conf).Unwrap() | ||
ormCfg.NowFunc = func() time.Time { return time.Now().UTC() } | ||
ormCfg.NamingStrategy = schema.NamingStrategy{TablePrefix: conf.TablePrefix} | ||
|
||
logCfg := DefaultLoggerCfg() | ||
logs.Debug().Any("config", logCfg).Msg("orm config") | ||
|
||
ormCfg.Logger = logger.New(log.NewStd(logs.WithCallerSkip(4)), logCfg) | ||
logs.Debug().Any("config", ormCfg).Msg("orm log config") | ||
|
||
factory := Get(conf.Driver) | ||
assert.If(factory == nil, "driver factory[%s] not found", conf.Driver) | ||
dialect := factory(conf.DriverCfg) | ||
|
||
db := assert.Must1(gorm.Open(dialect, ormCfg)) | ||
|
||
// 服务连接校验 | ||
sqlDB := assert.Must1(db.DB()) | ||
assert.Must(sqlDB.Ping()) | ||
|
||
if conf.MaxConnTime != 0 { | ||
sqlDB.SetConnMaxLifetime(conf.MaxConnTime) | ||
} | ||
|
||
if conf.MaxConnIdle != 0 { | ||
sqlDB.SetMaxIdleConns(conf.MaxConnIdle) | ||
} | ||
|
||
if conf.MaxConnOpen != 0 { | ||
sqlDB.SetMaxOpenConns(conf.MaxConnOpen) | ||
} | ||
|
||
return &Client{ | ||
DB: db, | ||
TablePrefix: conf.TablePrefix, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package gormclient | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/pubgo/funk/result" | ||
"github.com/pubgo/funk/vars" | ||
"gorm.io/gorm" | ||
) | ||
|
||
const Name = "orm" | ||
|
||
type Client struct { | ||
*gorm.DB | ||
TablePrefix string | ||
} | ||
|
||
func (c *Client) Ping() error { | ||
_db, err := c.DB.DB() | ||
if err != nil { | ||
return err | ||
} | ||
return _db.Ping() | ||
} | ||
|
||
func (c *Client) Vars() vars.Value { | ||
return func() interface{} { | ||
_db, err := c.DB.DB() | ||
if err != nil { | ||
return err.Error() | ||
} else { | ||
return _db.Stats() | ||
} | ||
} | ||
} | ||
|
||
func (c *Client) Close() error { | ||
db, err := c.DB.DB() | ||
if err != nil { | ||
return err | ||
} | ||
return db.Close() | ||
} | ||
|
||
func (c *Client) Stats() (r result.Result[sql.DBStats]) { | ||
db, err := c.DB.DB() | ||
if err != nil { | ||
return r.WithErr(err) | ||
} | ||
return r.WithVal(db.Stats()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gormclient | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pubgo/funk/config" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
type Config struct { | ||
TablePrefix string `yaml:"table_prefix"` | ||
Driver string `yaml:"driver"` | ||
DriverCfg config.Node `yaml:"driver_config"` | ||
SkipDefaultTransaction bool `yaml:"skip_default_transaction"` | ||
FullSaveAssociations bool `yaml:"full_save_associations"` | ||
DryRun bool `yaml:"dry_run"` | ||
PrepareStmt bool `yaml:"prepare_stmt"` | ||
DisableAutomaticPing bool `yaml:"disable_automatic_ping"` | ||
DisableForeignKeyConstraintWhenMigrating bool `yaml:"disable_foreign_key_constraint_when_migrating"` | ||
DisableNestedTransaction bool `yaml:"disable_nested_transaction"` | ||
AllowGlobalUpdate bool `yaml:"allow_global_update"` | ||
QueryFields bool `yaml:"query_fields"` | ||
CreateBatchSize int `yaml:"create_batch_size"` | ||
MaxConnTime time.Duration `yaml:"max_conn_time"` | ||
MaxConnIdle int `yaml:"max_conn_idle"` | ||
MaxConnOpen int `yaml:"max_conn_open"` | ||
} | ||
|
||
func DefaultCfg() Config { | ||
return Config{ | ||
SkipDefaultTransaction: true, | ||
MaxConnTime: time.Hour, | ||
MaxConnIdle: 10, | ||
MaxConnOpen: 100, | ||
} | ||
} | ||
|
||
func DefaultLoggerCfg() logger.Config { | ||
return logger.Config{ | ||
SlowThreshold: 200 * time.Millisecond, | ||
LogLevel: logger.Info, | ||
IgnoreRecordNotFoundError: false, | ||
Colorful: false, | ||
} | ||
} |
Oops, something went wrong.