From c3f08ecf5f805510f5fd4f3d0cf8e87dde364179 Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Mon, 12 Feb 2018 23:20:46 +0000 Subject: [PATCH] ENT-1517: Move MigrationHelpers object into internal package. (#457) --- .../corda/core/{schemas => internal}/MigrationHelpers.kt | 8 +++++--- .../corda/node/services/persistence/MigrationExporter.kt | 4 ++-- .../corda/nodeapi/internal/persistence/SchemaMigration.kt | 6 +++--- .../src/main/kotlin/com/r3/corda/dbmigration/Launcher.kt | 8 ++++---- 4 files changed, 14 insertions(+), 12 deletions(-) rename core/src/main/kotlin/net/corda/core/{schemas => internal}/MigrationHelpers.kt (81%) diff --git a/core/src/main/kotlin/net/corda/core/schemas/MigrationHelpers.kt b/core/src/main/kotlin/net/corda/core/internal/MigrationHelpers.kt similarity index 81% rename from core/src/main/kotlin/net/corda/core/schemas/MigrationHelpers.kt rename to core/src/main/kotlin/net/corda/core/internal/MigrationHelpers.kt index 2df2746a3a..a6a7eb1a9b 100644 --- a/core/src/main/kotlin/net/corda/core/schemas/MigrationHelpers.kt +++ b/core/src/main/kotlin/net/corda/core/internal/MigrationHelpers.kt @@ -1,3 +1,5 @@ +package net.corda.core.internal + import com.google.common.base.CaseFormat import net.corda.core.schemas.MappedSchema @@ -13,12 +15,12 @@ object MigrationHelpers { if (declaredMigration == null) { // try to apply the naming convention and find the migration file in the classpath val resource = migrationResourceNameForSchema(schema) - return possibleMigrationExtensions.map { "${resource}${it}" }.firstOrNull { + return possibleMigrationExtensions.map { "$resource$it" }.firstOrNull { 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 @@ -26,6 +28,6 @@ object MigrationHelpers { fun migrationResourceNameForSchema(schema: MappedSchema): String { val name: String = schema::class.simpleName!! val fileName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name) - return "${MIGRATION_PREFIX}/${fileName}.${CHANGELOG_NAME}" + return "$MIGRATION_PREFIX/$fileName.$CHANGELOG_NAME" } } diff --git a/node-api/src/main/kotlin/net/corda/node/services/persistence/MigrationExporter.kt b/node-api/src/main/kotlin/net/corda/node/services/persistence/MigrationExporter.kt index c7f82a469d..f2cfbbb4fb 100644 --- a/node-api/src/main/kotlin/net/corda/node/services/persistence/MigrationExporter.kt +++ b/node-api/src/main/kotlin/net/corda/node/services/persistence/MigrationExporter.kt @@ -1,7 +1,7 @@ package net.corda.node.services.persistence -import MigrationHelpers.migrationResourceNameForSchema import net.corda.core.identity.AbstractParty +import net.corda.core.internal.MigrationHelpers.migrationResourceNameForSchema import net.corda.core.internal.objectOrNewInstance import net.corda.core.schemas.MappedSchema import net.corda.nodeapi.internal.persistence.CordaPersistence @@ -53,7 +53,7 @@ class MigrationExporter(val parent: Path, val datasourceProperties: Properties, createNewFile() appendText(LIQUIBASE_HEADER) 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") } diff --git a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/SchemaMigration.kt b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/SchemaMigration.kt index c967b80e81..11833580f6 100644 --- a/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/SchemaMigration.kt +++ b/node-api/src/main/kotlin/net/corda/nodeapi/internal/persistence/SchemaMigration.kt @@ -1,6 +1,5 @@ package net.corda.nodeapi.internal.persistence -import MigrationHelpers.getMigrationResource import com.fasterxml.jackson.databind.ObjectMapper import liquibase.Contexts import liquibase.LabelExpression @@ -11,6 +10,7 @@ import liquibase.database.core.MSSQLDatabase import liquibase.database.jvm.JdbcConnection import liquibase.lockservice.LockServiceFactory import liquibase.resource.ClassLoaderResourceAccessor +import net.corda.core.internal.MigrationHelpers.getMigrationResource import net.corda.core.schemas.MappedSchema import net.corda.core.utilities.contextLogger import java.io.* @@ -31,7 +31,7 @@ class SchemaMigration( * Main entry point to the schema migration. * Called during node startup. */ - fun nodeStartup(): Unit { + fun nodeStartup() { when { databaseConfig.runMigration -> runMigration() 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 */ - fun forceReleaseMigrationLock(): Unit { + fun forceReleaseMigrationLock() { dataSource.connection.use { connection -> LockServiceFactory.getInstance().getLockService(getLiquibaseDatabase(JdbcConnection(connection))).forceReleaseLock() } diff --git a/tools/dbmigration/src/main/kotlin/com/r3/corda/dbmigration/Launcher.kt b/tools/dbmigration/src/main/kotlin/com/r3/corda/dbmigration/Launcher.kt index d853a612a9..f9f57aa8d0 100644 --- a/tools/dbmigration/src/main/kotlin/com/r3/corda/dbmigration/Launcher.kt +++ b/tools/dbmigration/src/main/kotlin/com/r3/corda/dbmigration/Launcher.kt @@ -2,7 +2,6 @@ package com.r3.corda.dbmigration -import MigrationHelpers import com.typesafe.config.ConfigFactory import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource @@ -10,6 +9,7 @@ import joptsimple.OptionException import joptsimple.OptionParser import joptsimple.OptionSet import joptsimple.util.EnumConverter +import net.corda.core.internal.MigrationHelpers import net.corda.core.internal.copyTo import net.corda.core.internal.div import net.corda.core.schemas.MappedSchema @@ -147,20 +147,20 @@ private fun handleCommand(options: OptionSet, baseDirectory: Path, configFile: P } } options.has(RUN_MIGRATION) -> { - logger.info("Running the database migration on ${baseDirectory}") + logger.info("Running the database migration on $baseDirectory") runMigrationCommand { it.runMigration() } } options.has(CREATE_MIGRATION_CORDAPP) && (mode == Mode.NODE) -> { 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 { runWithDataSource(config) { MigrationExporter(baseDirectory, config.dataSourceProperties, classLoader, it).generateMigrationForCorDapp(schemaClass) } } catch (e: Exception) { e.printStackTrace() - errorAndExit("Could not generate migration for ${schemaClass}: ${e.message}") + errorAndExit("Could not generate migration for $schemaClass: ${e.message}") } }