[ENT-1339]: for h2, allow schemas without migrations to run (#294) (#305)

* [ENT-1339]: for h2, allow schemas without migrations to run (#294)

* [ENT-1339]: for h2, allow schemas without migrations to run

* [ENT-1339]: fix various migration issues and change author name

* [ENT-1339]: add naming convention for migrations

* [ENT-1339]: change naming convention to use hyphens

* [ENT-1339]: change mapping of participants to be able to control the table name

* [ENT-1339]: change FK names to <=30 for oracle 11g compatibility

* [ENT-1339]: cmd line argument for migrations made consistent

* [ENT-1339]: revert abstract state superclasses

* Update db integration test setup - new tables.

* Update db integration test setup - new tables.

* [ENT-1339]: remove final from participants to allow table name config

* [ENT-1339]: shortened pk

* [ENT-1339]: revert constructor

* [ENT-1339]: change getMigrationResource api to Nullable
This commit is contained in:
Tudor Malene
2018-01-09 14:44:17 +00:00
committed by GitHub
parent a65db712c7
commit 0c646ff662
51 changed files with 400 additions and 773 deletions

View File

@ -229,3 +229,5 @@ fun parserTransactionIsolationLevel(property: String?): Int =
Connection.TRANSACTION_REPEATABLE_READ
}
}
fun isH2Database(jdbcUrl: String) = jdbcUrl.startsWith("jdbc:h2:")

View File

@ -47,7 +47,7 @@ class HibernateConfiguration(
val metadataSources = MetadataSources(serviceRegistry)
val config = Configuration(metadataSources).setProperty("hibernate.connection.provider_class", NodeDatabaseConnectionProvider::class.java.name)
.setProperty("hibernate.hbm2ddl.auto", "validate")
.setProperty("hibernate.hbm2ddl.auto", if (isH2Database(jdbcUrl)) "update" else "validate")
.setProperty("hibernate.connection.isolation", databaseConfig.transactionIsolationLevel.jdbcValue.toString())
databaseConfig.schema?.apply {
@ -86,8 +86,7 @@ class HibernateConfiguration(
try {
mbeanServer.registerMBean(statisticsMBean, statsName)
}
catch (e: Exception) {
} catch (e: Exception) {
logger.warn(e.message)
}
}

View File

@ -14,9 +14,7 @@ import net.corda.core.utilities.contextLogger
import java.io.*
import javax.sql.DataSource
private const val MIGRATION_PREFIX = "migration"
class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource, private val schemaName: String? = null) {
class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource, val failOnMigrationMissing: Boolean, private val schemaName: String? = null) {
companion object {
private val logger = contextLogger()
@ -33,10 +31,17 @@ class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource
dataSource.connection.use { connection ->
//collect all changelog file referenced in the included schemas
// collect all changelog file referenced in the included schemas
// for backward compatibility reasons, when failOnMigrationMissing=false, we don't manage CorDapps via Liquibase but use the hibernate hbm2ddl=update
val changelogList = schemas.map { mappedSchema ->
getMigrationResource(mappedSchema).let {
"${MIGRATION_PREFIX}/${it}.xml"
val resource = getMigrationResource(mappedSchema)
when {
resource != null -> resource
failOnMigrationMissing -> throw IllegalStateException("No migration defined for schema: ${mappedSchema.name} v${mappedSchema.version}")
else -> {
logger.warn("No migration defined for schema: ${mappedSchema.name} v${mappedSchema.version}")
null
}
}
}
@ -46,7 +51,7 @@ class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource
if (path == dynamicInclude) {
//create a map in liquibase format including all migration files
val includeAllFiles = mapOf("databaseChangeLog" to changelogList.map { file -> mapOf("include" to mapOf("file" to file)) })
val includeAllFiles = mapOf("databaseChangeLog" to changelogList.filter { it != null }.map { file -> mapOf("include" to mapOf("file" to file)) })
// transform it to json
val includeAllFilesJson = ObjectMapper().writeValueAsBytes(includeAllFiles)