Skip to content

Commit

Permalink
:file: delete dialector
Browse files Browse the repository at this point in the history
  • Loading branch information
iChemy committed Jul 15, 2024
1 parent 04328c0 commit 89564e3
Showing 1 changed file with 17 additions and 41 deletions.
58 changes: 17 additions & 41 deletions infra/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,24 @@ func (repo *GormRepository) Setup(host, user, password, database, port, key, log
}
tokenKey = []byte(key)

d := dialector{
Dialector: mysql.New(mysql.Config{
DSNConfig: &gomysql.Config{
User: user,
Passwd: password,
Net: "tcp",
Addr: host + ":" + port,
DBName: database,
Loc: loc,
AllowNativePasswords: true,
ParseTime: true,
},
DefaultStringSize: 256, // default size for string fields
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: false, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
}),
}

var err error
repo.db, err = gorm.Open(d, &gorm.Config{
repo.db, err = gorm.Open(mysql.New(mysql.Config{
DSNConfig: &gomysql.Config{
User: user,
Passwd: password,
Net: "tcp",
Addr: host + ":" + port,
DBName: database,
Loc: loc,
AllowNativePasswords: true,
ParseTime: true,
},
DefaultStringSize: 256, // default size for string fields
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: false, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
}), &gorm.Config{
Logger: logger.Default.LogMode(loglevel),
})
if err != nil {
Expand All @@ -67,23 +63,3 @@ func (repo *GormRepository) Setup(host, user, password, database, port, key, log

return migration.Migrate(repo.db, tables)
}

// dialector with custom error handling
type dialector struct {
gorm.Dialector
gorm.SavePointerDialectorInterface
}

var (
_ gorm.Dialector = (*dialector)(nil)
_ gorm.ErrorTranslator = (*dialector)(nil)
)

// override Translate(err error) error
func (d dialector) Translate(err error) error {
if translater, ok := d.Dialector.(gorm.ErrorTranslator); ok {
err = translater.Translate(err)
}

return defaultErrorHandling(err)
}

0 comments on commit 89564e3

Please sign in to comment.