From 13f0f12c1bf64691b5dfe9ce462ee877c54ac01f Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sun, 4 Aug 2019 22:23:57 +0200 Subject: [PATCH] Fix migration if executed two or more times --- .../fr/postgresjson/migration/Migrations.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 87dfc69..423f00a 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -1,7 +1,6 @@ package fr.postgresjson.migration import com.fasterxml.jackson.core.type.TypeReference -import com.github.jasync.sql.db.util.size import fr.postgresjson.connexion.Connection import fr.postgresjson.definition.Function.FunctionNotFound import fr.postgresjson.entity.Entity @@ -39,13 +38,23 @@ data class Migrations private constructor( private val queries: MutableMap = mutableMapOf(), private val functions: MutableMap = mutableMapOf() ) { + private var directories: List = emptyList() private val logger: Logger? by LoggerDelegate() constructor(directory: File, connection: Connection): this(listOf(directory), connection) constructor(directories: List, connection: Connection): this(connection) { initDB() + this.directories = directories + reset() + } + + fun reset() { + queries.clear() + functions.clear() + getMigrationFromDB() getMigrationFromDirectory(directories) + queries.forEach { (_, query) -> if (query.doExecute === null) { query.doExecute = Action.DOWN @@ -97,11 +106,11 @@ data class Migrations private constructor( it.isFile }.forEach { file -> if (file.name.endsWith(".up.sql")) { - file.path.substring(0, file.path.size - 7).let { + file.path.substring(0, file.path.length - 7).let { try { val down = File("$it.down.sql").readText() val up = file.readText() - val name = file.name.substring(0, file.name.size - 7) + val name = file.name.substring(0, file.name.length - 7) addQuery(name, up, down) } catch (e: FileNotFoundException) { throw DownMigrationNotDefined("$it.down.sql", e) @@ -244,6 +253,7 @@ data class Migrations private constructor( sendQuery("COMMIT") } logger?.info("Migration done") + reset() return list.toMap() } @@ -264,6 +274,7 @@ data class Migrations private constructor( sendQuery("COMMIT") } logger?.info("Migration DOWN done") + reset() return list.toMap() }