mirror of
https://github.com/corda/corda.git
synced 2025-01-15 01:10:33 +00:00
ENT-1517: Move MigrationHelpers object into internal package. (#457)
This commit is contained in:
parent
ef45900fda
commit
c3f08ecf5f
@ -1,3 +1,5 @@
|
|||||||
|
package net.corda.core.internal
|
||||||
|
|
||||||
import com.google.common.base.CaseFormat
|
import com.google.common.base.CaseFormat
|
||||||
import net.corda.core.schemas.MappedSchema
|
import net.corda.core.schemas.MappedSchema
|
||||||
|
|
||||||
@ -13,12 +15,12 @@ object MigrationHelpers {
|
|||||||
if (declaredMigration == null) {
|
if (declaredMigration == null) {
|
||||||
// try to apply the naming convention and find the migration file in the classpath
|
// try to apply the naming convention and find the migration file in the classpath
|
||||||
val resource = migrationResourceNameForSchema(schema)
|
val resource = migrationResourceNameForSchema(schema)
|
||||||
return possibleMigrationExtensions.map { "${resource}${it}" }.firstOrNull {
|
return possibleMigrationExtensions.map { "$resource$it" }.firstOrNull {
|
||||||
classLoader.getResource(it) != null
|
classLoader.getResource(it) != null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "${MIGRATION_PREFIX}/${declaredMigration}.${DEFAULT_MIGRATION_EXTENSION}"
|
return "$MIGRATION_PREFIX/$declaredMigration.$DEFAULT_MIGRATION_EXTENSION"
|
||||||
}
|
}
|
||||||
|
|
||||||
// SchemaName will be transformed from camel case to lower_hyphen
|
// SchemaName will be transformed from camel case to lower_hyphen
|
||||||
@ -26,6 +28,6 @@ object MigrationHelpers {
|
|||||||
fun migrationResourceNameForSchema(schema: MappedSchema): String {
|
fun migrationResourceNameForSchema(schema: MappedSchema): String {
|
||||||
val name: String = schema::class.simpleName!!
|
val name: String = schema::class.simpleName!!
|
||||||
val fileName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name)
|
val fileName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name)
|
||||||
return "${MIGRATION_PREFIX}/${fileName}.${CHANGELOG_NAME}"
|
return "$MIGRATION_PREFIX/$fileName.$CHANGELOG_NAME"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package net.corda.node.services.persistence
|
package net.corda.node.services.persistence
|
||||||
|
|
||||||
import MigrationHelpers.migrationResourceNameForSchema
|
|
||||||
import net.corda.core.identity.AbstractParty
|
import net.corda.core.identity.AbstractParty
|
||||||
|
import net.corda.core.internal.MigrationHelpers.migrationResourceNameForSchema
|
||||||
import net.corda.core.internal.objectOrNewInstance
|
import net.corda.core.internal.objectOrNewInstance
|
||||||
import net.corda.core.schemas.MappedSchema
|
import net.corda.core.schemas.MappedSchema
|
||||||
import net.corda.nodeapi.internal.persistence.CordaPersistence
|
import net.corda.nodeapi.internal.persistence.CordaPersistence
|
||||||
@ -53,7 +53,7 @@ class MigrationExporter(val parent: Path, val datasourceProperties: Properties,
|
|||||||
createNewFile()
|
createNewFile()
|
||||||
appendText(LIQUIBASE_HEADER)
|
appendText(LIQUIBASE_HEADER)
|
||||||
appendText("\n\n")
|
appendText("\n\n")
|
||||||
appendText("--changeset ${CORDA_USER}:initial_schema_for_${mappedSchema::class.simpleName!!}")
|
appendText("--changeset $CORDA_USER:initial_schema_for_${mappedSchema::class.simpleName!!}")
|
||||||
appendText("\n")
|
appendText("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.corda.nodeapi.internal.persistence
|
package net.corda.nodeapi.internal.persistence
|
||||||
|
|
||||||
import MigrationHelpers.getMigrationResource
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import liquibase.Contexts
|
import liquibase.Contexts
|
||||||
import liquibase.LabelExpression
|
import liquibase.LabelExpression
|
||||||
@ -11,6 +10,7 @@ import liquibase.database.core.MSSQLDatabase
|
|||||||
import liquibase.database.jvm.JdbcConnection
|
import liquibase.database.jvm.JdbcConnection
|
||||||
import liquibase.lockservice.LockServiceFactory
|
import liquibase.lockservice.LockServiceFactory
|
||||||
import liquibase.resource.ClassLoaderResourceAccessor
|
import liquibase.resource.ClassLoaderResourceAccessor
|
||||||
|
import net.corda.core.internal.MigrationHelpers.getMigrationResource
|
||||||
import net.corda.core.schemas.MappedSchema
|
import net.corda.core.schemas.MappedSchema
|
||||||
import net.corda.core.utilities.contextLogger
|
import net.corda.core.utilities.contextLogger
|
||||||
import java.io.*
|
import java.io.*
|
||||||
@ -31,7 +31,7 @@ class SchemaMigration(
|
|||||||
* Main entry point to the schema migration.
|
* Main entry point to the schema migration.
|
||||||
* Called during node startup.
|
* Called during node startup.
|
||||||
*/
|
*/
|
||||||
fun nodeStartup(): Unit {
|
fun nodeStartup() {
|
||||||
when {
|
when {
|
||||||
databaseConfig.runMigration -> runMigration()
|
databaseConfig.runMigration -> runMigration()
|
||||||
failOnMigrationMissing -> checkState()
|
failOnMigrationMissing -> checkState()
|
||||||
@ -56,7 +56,7 @@ class SchemaMigration(
|
|||||||
/**
|
/**
|
||||||
* can be used from an external tool to release the lock in case something went terribly wrong
|
* can be used from an external tool to release the lock in case something went terribly wrong
|
||||||
*/
|
*/
|
||||||
fun forceReleaseMigrationLock(): Unit {
|
fun forceReleaseMigrationLock() {
|
||||||
dataSource.connection.use { connection ->
|
dataSource.connection.use { connection ->
|
||||||
LockServiceFactory.getInstance().getLockService(getLiquibaseDatabase(JdbcConnection(connection))).forceReleaseLock()
|
LockServiceFactory.getInstance().getLockService(getLiquibaseDatabase(JdbcConnection(connection))).forceReleaseLock()
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
package com.r3.corda.dbmigration
|
package com.r3.corda.dbmigration
|
||||||
|
|
||||||
import MigrationHelpers
|
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
import com.zaxxer.hikari.HikariConfig
|
import com.zaxxer.hikari.HikariConfig
|
||||||
import com.zaxxer.hikari.HikariDataSource
|
import com.zaxxer.hikari.HikariDataSource
|
||||||
@ -10,6 +9,7 @@ import joptsimple.OptionException
|
|||||||
import joptsimple.OptionParser
|
import joptsimple.OptionParser
|
||||||
import joptsimple.OptionSet
|
import joptsimple.OptionSet
|
||||||
import joptsimple.util.EnumConverter
|
import joptsimple.util.EnumConverter
|
||||||
|
import net.corda.core.internal.MigrationHelpers
|
||||||
import net.corda.core.internal.copyTo
|
import net.corda.core.internal.copyTo
|
||||||
import net.corda.core.internal.div
|
import net.corda.core.internal.div
|
||||||
import net.corda.core.schemas.MappedSchema
|
import net.corda.core.schemas.MappedSchema
|
||||||
@ -147,20 +147,20 @@ private fun handleCommand(options: OptionSet, baseDirectory: Path, configFile: P
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
options.has(RUN_MIGRATION) -> {
|
options.has(RUN_MIGRATION) -> {
|
||||||
logger.info("Running the database migration on ${baseDirectory}")
|
logger.info("Running the database migration on $baseDirectory")
|
||||||
runMigrationCommand { it.runMigration() }
|
runMigrationCommand { it.runMigration() }
|
||||||
}
|
}
|
||||||
options.has(CREATE_MIGRATION_CORDAPP) && (mode == Mode.NODE) -> {
|
options.has(CREATE_MIGRATION_CORDAPP) && (mode == Mode.NODE) -> {
|
||||||
|
|
||||||
fun generateMigrationFileForSchema(schemaClass: String) {
|
fun generateMigrationFileForSchema(schemaClass: String) {
|
||||||
logger.info("Creating database migration files for schema: ${schemaClass} into ${baseDirectory / "migration"}")
|
logger.info("Creating database migration files for schema: $schemaClass into ${baseDirectory / "migration"}")
|
||||||
try {
|
try {
|
||||||
runWithDataSource(config) {
|
runWithDataSource(config) {
|
||||||
MigrationExporter(baseDirectory, config.dataSourceProperties, classLoader, it).generateMigrationForCorDapp(schemaClass)
|
MigrationExporter(baseDirectory, config.dataSourceProperties, classLoader, it).generateMigrationForCorDapp(schemaClass)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
errorAndExit("Could not generate migration for ${schemaClass}: ${e.message}")
|
errorAndExit("Could not generate migration for $schemaClass: ${e.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user