Fix null migration resource (#210)

throw exception when migration not set for MappedSchema
This commit is contained in:
Tudor Malene 2017-12-19 14:06:55 +00:00 committed by GitHub
parent 3ab1a06db6
commit dd59b06c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 10 deletions

View File

@ -38,15 +38,16 @@ interface QueryableState : ContractState {
* @param mappedTypes The JPA entity classes that the ORM layer needs to be configure with for this schema.
*/
open class MappedSchema(schemaFamily: Class<*>,
val version: Int,
val mappedTypes: Iterable<Class<*>>) {
val version: Int,
val mappedTypes: Iterable<Class<*>>) {
val name: String = schemaFamily.name
/**
* Points to a classpath resource containing the database changes for the [mappedTypes]
*/
protected open val migrationResource: String? = null
internal fun getMigrationResource(): String = migrationResource!!
internal fun getMigrationResource(): String = migrationResource ?: throw IllegalStateException("Migration resource not set for schema $this")
override fun toString(): String = "${this.javaClass.simpleName}(name=$name, version=$version)"
}

View File

@ -28,18 +28,18 @@ class SchemaMigration(val schemas: Set<MappedSchema>, val dataSource: DataSource
dataSource.connection.use { connection ->
//collect all changelog file referenced in the included schemas
val changelogList = schemas.map { mappedSchema ->
getMigrationResource(mappedSchema).let {
"${MIGRATION_PREFIX}/${it}.xml"
}
}
//create a resourse accessor that aggregates the changelogs included in the schemas into one dynamic stream
val customResourceAccessor = object : ClassLoaderResourceAccessor() {
override fun getResourcesAsStream(path: String): Set<InputStream> {
if (path == dynamicInclude) {
//collect all changelog file referenced in the included schemas
val changelogList = schemas.map { mappedSchema ->
getMigrationResource(mappedSchema).let {
"${MIGRATION_PREFIX}/${it}.xml"
}
}
//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)) })