chore: introduce constants for db dialects

chore: go fmt
This commit is contained in:
Ferdinand Mütsch 2020-11-06 17:20:26 +01:00
parent e269b37b0e
commit 78874566a4
6 changed files with 18 additions and 14 deletions

View File

@ -22,6 +22,10 @@ const (
defaultConfigPath = "config.yml"
defaultConfigPathLegacy = "config.ini"
defaultEnvConfigPathLegacy = ".env"
SQLDialectMysql = "mysql"
SQLDialectPostgres = "postgres"
SQLDialectSqlite = "sqlite3"
)
var (
@ -113,16 +117,16 @@ func (c *Config) GetFixturesFunc(dbDialect string) models.MigrationFunc {
func (c *dbConfig) GetDialector() gorm.Dialector {
switch c.Dialect {
case "mysql":
case SQLDialectMysql:
return mysql.New(mysql.Config{
DriverName: c.Dialect,
DSN: mysqlConnectionString(c),
})
case "postgres":
case SQLDialectPostgres:
return postgres.New(postgres.Config{
DSN: postgresConnectionString(c),
})
case "sqlite3":
case SQLDialectSqlite:
return sqlite.Open(sqliteConnectionString(c))
}
return nil

View File

@ -59,7 +59,7 @@ func migrateLegacyConfig() error {
}
if dbType == "" {
dbType = "sqlite3"
dbType = SQLDialectSqlite
}
dbMaxConn := cfg.Section("database").Key("max_connections").MustUint(2)

View File

@ -38,7 +38,7 @@ func init() {
migrator := db.Migrator()
const lookupKey = "20201106-migration_cascade_constraints"
if cfg.Db.Dialect == "sqlite3" {
if cfg.Db.Dialect == config.SQLDialectSqlite {
// https://stackoverflow.com/a/1884893/3112139
// unfortunately, we can't migrate existing sqlite databases to the newly introduced cascade settings
// things like deleting all summaries won't work in those cases unless an entirely new db is created
@ -52,7 +52,7 @@ func init() {
}
condition := "key = ?"
if cfg.Db.Dialect == "mysql" {
if cfg.Db.Dialect == config.SQLDialectMysql {
condition = "`key` = ?"
}
lookupResult := db.Where(condition, lookupKey).First(&models.KeyStringValue{})

View File

@ -1,12 +1,12 @@
package models
type User struct {
ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"`
Password string `json:"-"`
CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"`
LastLoggedInAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"`
BadgesEnabled bool `json:"-" gorm:"default:false; type:bool"`
ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"`
Password string `json:"-"`
CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"`
LastLoggedInAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"`
BadgesEnabled bool `json:"-" gorm:"default:false; type:bool"`
}
type Login struct {

View File

@ -59,4 +59,4 @@ func (r *SummaryRepository) DeleteByUser(userId string) error {
return err
}
return nil
}
}

View File

@ -25,4 +25,4 @@ func ParseUserAgent(ua string) (string, string, error) {
return "", "", errors.New("failed to parse user agent string")
}
return groups[0][1], groups[0][2], nil
}
}