Skip to content

Commit

Permalink
refactor the code: use the same DBsWithEnvSpecificConfig for both tes…
Browse files Browse the repository at this point in the history
…t cases and non-test code
  • Loading branch information
t83714 committed Jul 16, 2023
1 parent 0becb97 commit c8d1a65
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package au.csiro.data61.magda.registry

import scalikejdbc.config.TypesafeConfig
import scalikejdbc.config.TypesafeConfigReader
import scalikejdbc.config.EnvPrefix
import scalikejdbc.config.DBs
import com.typesafe.config.{Config, ConfigValueFactory}
import au.csiro.data61.magda.util.UrlUtils

case class DBsWithEnvSpecificConfig(configToUse: Config)
extends DBs
with TypesafeConfigReader
with TypesafeConfig
with EnvPrefix {

override val config: Config = setGlobalStatementTimeout(configToUse)

private def setGlobalStatementTimeout(config: Config): Config = {
val globalTimeOutSetting = config.getDuration(
"db-query.global-timeout",
scala.concurrent.duration.SECONDS
)
val globalTimeOutSettingMils = globalTimeOutSetting * 1000
val dbUrl = config.getString("db.default.url")
val parsedDbUrl = UrlUtils.parse(dbUrl.replaceFirst("^(?i)jdbc:", ""))
val statementTimeout = parsedDbUrl.query.paramMap.get("options").flatMap {
v =>
val cfgOpts = v.flatMap(optStr => optStr.split("-c "))
cfgOpts
.find(cfgOpt => cfgOpt.toLowerCase.startsWith("statement_timeout="))
.map(
cfgOpt => cfgOpt.replaceFirst("^(?i)statement_timeout=", "").trim
)
.filter(s => !s.isEmpty)
.map(s => s.toLong)
}
if (!statementTimeout.isEmpty) config
else {
// set statement_timeout via jdbc connection string
val newOptStr =
(s"-c statement_timeout=${globalTimeOutSettingMils}" :: parsedDbUrl.query.paramMap
.get("options")
.toVector
.flatMap(item => item)
.toList).mkString(" ")

val newDbUrl = "jdbc:" + parsedDbUrl
.replaceParams("options", newOptStr)
.toString()

config.withValue(
"db.default.url",
ConfigValueFactory.fromAnyRef(
newDbUrl
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,57 +80,6 @@ object RegistryApp extends App {
case Failure(e) => "None"
})

case class DBsWithEnvSpecificConfig(configToUse: Config)
extends DBs
with TypesafeConfigReader
with TypesafeConfig
with EnvPrefix {

override val config: Config = setGlobalStatementTimeout(configToUse)

private def setGlobalStatementTimeout(config: Config): Config = {
val globalTimeOutSetting = config.getDuration(
"db-query.global-timeout",
scala.concurrent.duration.SECONDS
)
val globalTimeOutSettingMils = globalTimeOutSetting * 1000
val dbUrl = config.getString("db.default.url")
val parsedDbUrl = UrlUtils.parse(dbUrl.replaceFirst("^(?i)jdbc:", ""))
val statementTimeout = parsedDbUrl.query.paramMap.get("options").flatMap {
v =>
val cfgOpts = v.flatMap(optStr => optStr.split("-c "))
cfgOpts
.find(cfgOpt => cfgOpt.toLowerCase.startsWith("statement_timeout="))
.map(
cfgOpt => cfgOpt.replaceFirst("^(?i)statement_timeout=", "").trim
)
.filter(s => !s.isEmpty)
.map(s => s.toLong)
}
if (!statementTimeout.isEmpty) config
else {
// set statement_timeout via jdbc connection string
val newOptStr =
(s"-c statement_timeout=${globalTimeOutSettingMils}" :: parsedDbUrl.query.paramMap
.get("options")
.toVector
.flatMap(item => item)
.toList).mkString(" ")

val newDbUrl = "jdbc:" + parsedDbUrl
.replaceParams("options", newOptStr)
.toString()

config.withValue(
"db.default.url",
ConfigValueFactory.fromAnyRef(
newDbUrl
)
)
}
}
}

DBsWithEnvSpecificConfig(config).setupAll()

val listener = system.actorOf(Props(classOf[Listener]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,6 @@ abstract class ApiSpec
materializer
)

case class DBsWithEnvSpecificConfig(configToUse: Config)
extends DBs
with TypesafeConfigReader
with TypesafeConfig
with EnvPrefix {

override val config = configToUse
}

DBsWithEnvSpecificConfig(testConfig).setupAll()

DB localTx { implicit session =>
Expand Down

0 comments on commit c8d1a65

Please sign in to comment.