Fix migrations of Functions

This commit is contained in:
2020-05-11 01:39:36 +02:00
parent 377459f430
commit 55b547db75
6 changed files with 24 additions and 18 deletions

View File

@@ -0,0 +1,65 @@
package fr.postgresjson.migration
import fr.postgresjson.connexion.Connection
import fr.postgresjson.entity.mutable.Entity
import fr.postgresjson.migration.Migration.Action
import java.util.*
data class MigrationScript(
val name: String,
val up: String,
val down: String,
private val connection: Connection,
override var executedAt: Date? = null
) : Migration, Entity<String?>(name) {
override var doExecute: Action? = null
override fun up(): Migration.Status {
connection.sendQuery(up)
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
connection.selectOne<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
executedAt = query.executedAt
doExecute = Action.OK
}
}
return Migration.Status.OK
}
override fun down(): Migration.Status {
connection.sendQuery(down)
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
connection.exec(it, listOf(name))
}
return Migration.Status.OK
}
override fun test(): Migration.Status {
connection.inTransaction {
up()
down()
it.sendQuery("ROLLBACK")
}.join()
return Migration.Status.OK // TODO
}
override fun status(): Migration.Status {
connection.inTransaction {
up()
down()
it.sendQuery("ROLLBACK")
}.join()
return Migration.Status.OK // TODO
}
fun copy(): MigrationScript {
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
it.doExecute = this.doExecute
}
}
}