fix: migrations

This commit is contained in:
2019-08-03 00:53:44 +02:00
parent 88d581c529
commit 881a335c90
6 changed files with 29 additions and 20 deletions

View File

@@ -16,8 +16,8 @@ data class Function(
override var doExecute: Action? = null
init {
if (up.name !== down.name) {
throw Exception("UP and DOWN migration must be the same")
if (up.name != down.name) {
throw Exception("UP and DOWN migration must have the same name [${up.name} !== ${down.name}]")
}
}
@@ -79,4 +79,8 @@ data class Function(
it.doExecute = this.doExecute
}
}
infix fun `is different from`(other: DefinitionFunction): Boolean {
return other.script != this.up.script
}
}

View File

@@ -122,23 +122,17 @@ data class Migrations private constructor(
internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException):
Throwable("The file $path whas not found", cause)
fun addFunction(definition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
if (functions[definition.name] === null) {
// TODO define down migration
functions[definition.name] = Function(definition, definition, connection).apply {
fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
val currentFunction = functions[newDefinition.name]
if (currentFunction === null || currentFunction `is different from` newDefinition) {
functions[newDefinition.name] = Function(newDefinition, newDefinition, connection).apply {
doExecute = Action.UP
}
} else {
functions[definition.name]!!.apply {
if (up `is same` definition) {
doExecute = Action.OK
} else {
doExecute = Action.UP
}
}
functions[newDefinition.name]?.doExecute = Action.OK
}
callback(functions[definition.name]!!)
callback(functions[newDefinition.name]!!)
return this
}