Created
December 10, 2021 04:26
-
-
Save manakuro/f76cd0195e178fee322e65103ecaad38 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "context" | |
| "golang-clean-architecture-ent-gqlgen/config" | |
| "golang-clean-architecture-ent-gqlgen/ent" | |
| "golang-clean-architecture-ent-gqlgen/ent/migrate" | |
| "log" | |
| "entgo.io/ent/dialect" | |
| "github.com/go-sql-driver/mysql" | |
| ) | |
| func main() { | |
| config.ReadConfig(config.ReadConfigOption{}) | |
| client, err := newClient() | |
| if err != nil { | |
| log.Fatalf("failed opening mysql client: %v", err) | |
| } | |
| defer client.Close() | |
| createDBSchema(client) | |
| } | |
| func createDBSchema(client *ent.Client) { | |
| if err := client.Schema.Create( | |
| context.Background(), | |
| migrate.WithDropIndex(true), | |
| migrate.WithDropColumn(true), | |
| ); err != nil { | |
| log.Fatalf("failed creating schema resources: %v", err) | |
| } | |
| } | |
| func newDSN() string { | |
| mc := mysql.Config{ | |
| User: config.C.Database.User, | |
| Passwd: config.C.Database.Password, | |
| Net: config.C.Database.Net, | |
| Addr: config.C.Database.Addr, | |
| DBName: config.C.Database.DBName, | |
| AllowNativePasswords: config.C.Database.AllowNativePasswords, | |
| Params: map[string]string{ | |
| "parseTime": config.C.Database.Params.ParseTime, | |
| "charset": config.C.Database.Params.Charset, | |
| "loc": config.C.Database.Params.Loc, | |
| }, | |
| } | |
| return mc.FormatDSN() | |
| } | |
| func newClient() (*ent.Client, error) { | |
| var entOptions []ent.Option | |
| entOptions = append(entOptions, ent.Debug()) | |
| d := newDSN() | |
| return ent.Open(dialect.MySQL, d, entOptions...) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment