mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-13 11:35:40 +01:00
2020 10 12 issue 2164 (#2173)
* Make private lazy val representing Flyway config in DbManagement, expose DbManagement.info() method * Add more asserts based on new DbManagement.info() method * Add extra +1 for flyway on postgres as it seems to apply an implicit migration needed to setup flyway
This commit is contained in:
parent
112ec90286
commit
d37b563b40
2 changed files with 73 additions and 28 deletions
|
@ -4,6 +4,7 @@ import com.typesafe.config.Config
|
|||
import org.bitcoins.chain.config.ChainAppConfig
|
||||
import org.bitcoins.chain.db.ChainDbManagement
|
||||
import org.bitcoins.db.DatabaseDriver._
|
||||
import org.bitcoins.dlc.oracle.DLCOracleAppConfig
|
||||
import org.bitcoins.node.config.NodeAppConfig
|
||||
import org.bitcoins.node.db.NodeDbManagement
|
||||
import org.bitcoins.testkit.BitcoinSTestAppConfig.ProjectType
|
||||
|
@ -48,11 +49,22 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
|
|||
dbConfig(ProjectType.Chain))
|
||||
val chainDbManagement = createChainDbManagement(chainAppConfig)
|
||||
val result = chainDbManagement.migrate()
|
||||
val expected = chainAppConfig.driver match {
|
||||
case SQLite => 5
|
||||
case PostgreSQL => 4
|
||||
chainAppConfig.driver match {
|
||||
case SQLite =>
|
||||
val expected = 5
|
||||
assert(result == expected)
|
||||
val flywayInfo = chainDbManagement.info()
|
||||
assert(flywayInfo.applied().length == expected)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
case PostgreSQL =>
|
||||
val expected = 4
|
||||
assert(result == expected)
|
||||
val flywayInfo = chainDbManagement.info()
|
||||
//+1 for << Flyway Schema Creation >>
|
||||
assert(flywayInfo.applied().length == expected + 1)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
}
|
||||
assert(result == expected)
|
||||
|
||||
}
|
||||
|
||||
it must "run migrations for wallet db" in {
|
||||
|
@ -60,11 +72,23 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
|
|||
dbConfig(ProjectType.Wallet))
|
||||
val walletDbManagement = createWalletDbManagement(walletAppConfig)
|
||||
val result = walletDbManagement.migrate()
|
||||
val expected = walletAppConfig.driver match {
|
||||
case SQLite => 8
|
||||
case PostgreSQL => 6
|
||||
walletAppConfig.driver match {
|
||||
case SQLite =>
|
||||
val expected = 8
|
||||
assert(result == expected)
|
||||
val flywayInfo = walletDbManagement.info()
|
||||
assert(flywayInfo.applied().length == expected)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
case PostgreSQL =>
|
||||
val expected = 6
|
||||
assert(result == expected)
|
||||
val flywayInfo = walletDbManagement.info()
|
||||
|
||||
//+1 for << Flyway Schema Creation >>
|
||||
assert(flywayInfo.applied().length == expected + 1)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
}
|
||||
assert(result == expected)
|
||||
|
||||
}
|
||||
|
||||
it must "run migrations for node db" in {
|
||||
|
@ -72,10 +96,22 @@ class DbManagementTest extends BitcoinSAsyncTest with EmbeddedPg {
|
|||
NodeAppConfig(BitcoinSTestAppConfig.tmpDir(), dbConfig(ProjectType.Node))
|
||||
val nodeDbManagement = createNodeDbManagement(nodeAppConfig)
|
||||
val result = nodeDbManagement.migrate()
|
||||
val expected = nodeAppConfig.driver match {
|
||||
case SQLite => 2
|
||||
case PostgreSQL => 2
|
||||
nodeAppConfig.driver match {
|
||||
case SQLite =>
|
||||
val expected = 2
|
||||
assert(result == expected)
|
||||
val flywayInfo = nodeDbManagement.info()
|
||||
//+1 for << Flyway Schema Creation >>
|
||||
assert(flywayInfo.applied().length == expected)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
case PostgreSQL =>
|
||||
val expected = 2
|
||||
assert(result == expected)
|
||||
val flywayInfo = nodeDbManagement.info()
|
||||
|
||||
//+1 for << Flyway Schema Creation >>
|
||||
assert(flywayInfo.applied().length == expected + 1)
|
||||
assert(flywayInfo.pending().length == 0)
|
||||
}
|
||||
assert(result == expected)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.bitcoins.db
|
|||
|
||||
import org.bitcoins.core.util.{BitcoinSLogger, FutureUtil}
|
||||
import org.flywaydb.core.Flyway
|
||||
import org.flywaydb.core.api.FlywayException
|
||||
import org.flywaydb.core.api.{FlywayException, MigrationInfoService}
|
||||
|
||||
import scala.concurrent.{ExecutionContext, Future}
|
||||
|
||||
|
@ -12,6 +12,23 @@ trait DbManagement extends BitcoinSLogger {
|
|||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
private lazy val flyway: Flyway = {
|
||||
val module = appConfig.moduleName
|
||||
val config = {
|
||||
val conf = Flyway
|
||||
.configure()
|
||||
.locations(s"classpath:${driverName}/${module}/migration/")
|
||||
if (isPostgres) {
|
||||
conf
|
||||
.schemas(module)
|
||||
.defaultSchema(module)
|
||||
} else {
|
||||
conf
|
||||
}
|
||||
}
|
||||
config.dataSource(jdbcUrl, username, password).load
|
||||
}
|
||||
|
||||
/** Internally, slick defines the schema member as
|
||||
*
|
||||
* def schema: SchemaDescription = buildTableSchemaDescription(q.shaped.value.asInstanceOf[Table[_]])
|
||||
|
@ -101,26 +118,18 @@ trait DbManagement extends BitcoinSLogger {
|
|||
database.run(sql).map(_ => ())
|
||||
}
|
||||
|
||||
/** Returns flyway information about the state of migrations
|
||||
* @see https://flywaydb.org/documentation/command/info
|
||||
*/
|
||||
def info(): MigrationInfoService = {
|
||||
flyway.info()
|
||||
}
|
||||
|
||||
/** Executes migrations related to this database
|
||||
*
|
||||
* @see [[https://flywaydb.org/documentation/api/#programmatic-configuration-java]]
|
||||
*/
|
||||
def migrate(): Int = {
|
||||
val module = appConfig.moduleName
|
||||
val config = {
|
||||
val conf = Flyway
|
||||
.configure()
|
||||
.locations(s"classpath:${driverName}/${module}/migration/")
|
||||
if (isPostgres) {
|
||||
conf
|
||||
.schemas(module)
|
||||
.defaultSchema(module)
|
||||
} else {
|
||||
conf
|
||||
}
|
||||
}
|
||||
val flyway = config.dataSource(jdbcUrl, username, password).load
|
||||
|
||||
try {
|
||||
flyway.migrate()
|
||||
} catch {
|
||||
|
|
Loading…
Add table
Reference in a new issue