Created
November 19, 2020 06:27
-
-
Save aasumitro/90c0be7e5091770e58017da03c912a7b to your computer and use it in GitHub Desktop.
gorm 2 create db if not exitst
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 infrastructure | |
| import ( | |
| "fmt" | |
| "gorm.io/driver/mysql" | |
| "gorm.io/gorm" | |
| "gorm2.0/utils" | |
| ) | |
| //Setup Models: initializaing the mysql database | |
| func GetDatabaseInstance() *gorm.DB { | |
| get := utils.GetEnvWithKey | |
| USER := get("DB_USER") | |
| PASS := get("DB_PASS") | |
| HOST := get("DB_HOST") | |
| PORT := get("DB_PORT") | |
| DBNAME := get("DB_NAME") | |
| createDBDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", USER, PASS, HOST, PORT) | |
| database, err := gorm.Open(mysql.Open(createDBDsn), &gorm.Config{}) | |
| _ = database.Exec("CREATE DATABASE IF NOT EXISTS " + DBNAME + ";") | |
| dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", USER, PASS, HOST, PORT, DBNAME) | |
| db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | |
| if err != nil { | |
| panic(err.Error()) | |
| } | |
| return db | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment