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"
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{})