Liquibase works for a single db user with multi schema setup (par of ENT-1275) (#233)

* Override Liquibase default schema by one from the node configuration (database.schema) if they are different. This allows database tables be created within a correct schema when no default schema is set at database level.
* Pass in the databaseConfig.schema for network manager (for Liquibase schema migration).
This commit is contained in:
szymonsztuka
2017-12-20 17:17:17 +00:00
committed by GitHub
parent c288dcec20
commit d9574338bc
7 changed files with 168 additions and 5 deletions

View File

@ -52,6 +52,10 @@ class HibernateConfiguration(
.setProperty("hibernate.hbm2ddl.auto", "validate")
.setProperty("hibernate.connection.isolation", databaseConfig.transactionIsolationLevel.jdbcValue.toString())
databaseConfig.schema?.apply {
config.setProperty("hibernate.default_schema", databaseConfig.schema)
}
schemas.forEach { schema ->
// TODO: require mechanism to set schemaOptions (databaseSchema, tablePrefix) which are not global to session
schema.mappedTypes.forEach { config.addAnnotatedClass(it) }

View File

@ -10,12 +10,17 @@ import liquibase.database.jvm.JdbcConnection
import liquibase.resource.ClassLoaderResourceAccessor
import net.corda.core.schemas.MappedSchema
import net.corda.core.schemas.getMigrationResource
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) {
class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource, private val schemaName: String? = null) {
companion object {
private val logger = contextLogger()
}
fun generateMigrationScript(outputFile: File) = doRunMigration(PrintWriter(outputFile))
@ -55,6 +60,20 @@ class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource
val liquibase = Liquibase(dynamicInclude, customResourceAccessor, getLiquibaseDatabase(JdbcConnection(connection)))
if (!schemaName.isNullOrBlank()) {
if (liquibase.database.defaultSchemaName != schemaName) {
logger.debug("defaultSchemaName=${liquibase.database.defaultSchemaName} changed to $schemaName")
liquibase.database.defaultSchemaName = schemaName
}
if (liquibase.database.liquibaseSchemaName != schemaName) {
logger.debug("liquibaseSchemaName=${liquibase.database.liquibaseSchemaName} changed to $schemaName")
liquibase.database.liquibaseSchemaName = schemaName
}
}
logger.info("defaultSchemaName=${liquibase.database.defaultSchemaName}")
logger.info("liquibaseSchemaName=${liquibase.database.liquibaseSchemaName}")
logger.info("outputDefaultSchema=${liquibase.database.outputDefaultSchema}")
if (outputWriter != null) {
liquibase.update(Contexts(), outputWriter)
} else {