forked from ebhomengo/niki
34 lines
609 B
Go
34 lines
609 B
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
storage "git.gocasts.ir/ebhomengo/niki/adapter/storage/models"
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/gorm/logger"
|
||
|
)
|
||
|
|
||
|
type DBMS struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
|
||
|
func GormConnect(dsn string) (DBMS, error) {
|
||
|
gorm, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||
|
Logger: logger.Default.LogMode(logger.Info),
|
||
|
})
|
||
|
if err != nil {
|
||
|
return DBMS{}, err
|
||
|
}
|
||
|
return DBMS{db: gorm}, nil
|
||
|
}
|
||
|
|
||
|
func GormAutoMigrate(db gorm.DB) error {
|
||
|
// Migrate the schema
|
||
|
err := db.AutoMigrate(
|
||
|
storage.PiggyBank{},
|
||
|
)
|
||
|
if err != nil {
|
||
|
panic(any("Storage auto migrate has error"))
|
||
|
}
|
||
|
return err
|
||
|
}
|