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

@@ -51,7 +51,12 @@ open class Function(
class FunctionNotFound(cause: Throwable? = null): ParseException("Function not found in script", cause) class FunctionNotFound(cause: Throwable? = null): ParseException("Function not found in script", cause)
fun getDefinition(): String { fun getDefinition(): String {
return "$name (" + parameters.joinToString(", ") + ") $returns" return parameters
.filter { it.direction == Parameter.Direction.IN }
.joinToString(", ") { "${it.name} ${it.type}" }.let {
"$name ($it) $returns"
}
} }
fun getParametersIndexedByName(): Map<String, Parameter> { fun getParametersIndexedByName(): Map<String, Parameter> {
@@ -64,8 +69,8 @@ open class Function(
return other.getDefinition() == this.getDefinition() return other.getDefinition() == this.getDefinition()
} }
infix fun `is same`(other: Function): Boolean { infix fun `is different from`(other: Function): Boolean {
return other.script == this.script return other.script != this.script
} }
companion object { companion object {

View File

@@ -16,8 +16,8 @@ data class Function(
override var doExecute: Action? = null override var doExecute: Action? = null
init { init {
if (up.name !== down.name) { if (up.name != down.name) {
throw Exception("UP and DOWN migration must be the same") 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 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): internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException):
Throwable("The file $path whas not found", cause) Throwable("The file $path whas not found", cause)
fun addFunction(definition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations { fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations {
if (functions[definition.name] === null) { val currentFunction = functions[newDefinition.name]
// TODO define down migration if (currentFunction === null || currentFunction `is different from` newDefinition) {
functions[definition.name] = Function(definition, definition, connection).apply { functions[newDefinition.name] = Function(newDefinition, newDefinition, connection).apply {
doExecute = Action.UP doExecute = Action.UP
} }
} else { } else {
functions[definition.name]!!.apply { functions[newDefinition.name]?.doExecute = Action.OK
if (up `is same` definition) {
doExecute = Action.OK
} else {
doExecute = Action.UP
}
}
} }
callback(functions[definition.name]!!) callback(functions[newDefinition.name]!!)
return this return this
} }

View File

@@ -1,3 +1,3 @@
DELETE DELETE
FROM migration.functions FROM migration.functions
WHERE filename = :filename; WHERE filename = ?;

View File

@@ -4,4 +4,10 @@ VALUES (?, ?, now(), ?, ?, (
from migration.functions f2 from migration.functions f2
where filename = f2.filename where filename = f2.filename
)) ))
ON CONFLICT (filename) DO UPDATE SET
definition = excluded.definition,
up = excluded.up,
down = excluded.down,
version = excluded.version,
executed_at = now()
RETURNING to_json(f); RETURNING to_json(f);

View File

@@ -1,7 +1,7 @@
INSERT INTO migration.history as h (filename, executed_at, up, down, version) INSERT INTO migration.history as h (filename, executed_at, up, down, version)
VALUES (?, now(), ?, ?, ( VALUES (?, now(), ?, ?, (
select coalesce(max(version), 0)+1 select coalesce(max(version), 0)+1
from migration.history f2 from migration.history h2
where filename = f2.filename where h2.filename = filename
)) ))
RETURNING to_json(h); RETURNING to_json(h);