1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

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

View File

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

View File

@ -38,7 +38,7 @@ func init() {
migrator := db.Migrator() migrator := db.Migrator()
const lookupKey = "20201106-migration_cascade_constraints" const lookupKey = "20201106-migration_cascade_constraints"
if cfg.Db.Dialect == "sqlite3" { if cfg.Db.Dialect == config.SQLDialectSqlite {
// https://stackoverflow.com/a/1884893/3112139 // https://stackoverflow.com/a/1884893/3112139
// unfortunately, we can't migrate existing sqlite databases to the newly introduced cascade settings // 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 // 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 = ?" condition := "key = ?"
if cfg.Db.Dialect == "mysql" { if cfg.Db.Dialect == config.SQLDialectMysql {
condition = "`key` = ?" condition = "`key` = ?"
} }
lookupResult := db.Where(condition, lookupKey).First(&models.KeyStringValue{}) lookupResult := db.Where(condition, lookupKey).First(&models.KeyStringValue{})

View File

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

View File

@ -59,4 +59,4 @@ func (r *SummaryRepository) DeleteByUser(userId string) error {
return err return err
} }
return nil 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 "", "", errors.New("failed to parse user agent string")
} }
return groups[0][1], groups[0][2], nil return groups[0][1], groups[0][2], nil
} }