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:
parent
e269b37b0e
commit
78874566a4
@ -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
|
||||||
|
@ -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)
|
||||||
|
@ -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{})
|
||||||
|
Loading…
Reference in New Issue
Block a user