refactoring: definition.Function
This commit is contained in:
@@ -4,12 +4,19 @@ import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
class Function(
|
||||
private val up: DefinitionFunction,
|
||||
private val down: DefinitionFunction,
|
||||
val up: DefinitionFunction,
|
||||
val down: DefinitionFunction,
|
||||
private val connection: Connection
|
||||
): Migration {
|
||||
val name = up.name
|
||||
enum class Status(i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
||||
|
||||
init {
|
||||
if (up.name !== down.name) {
|
||||
throw Exception("UP and DOWN migration must be the same")
|
||||
}
|
||||
}
|
||||
|
||||
override fun up(): Int {
|
||||
connection.exec(up.script)
|
||||
return 1
|
||||
|
||||
@@ -17,6 +17,7 @@ interface Migration {
|
||||
class Migrations(directory: File, private val connection: Connection): Migration {
|
||||
private val queries: MutableList<Query> = mutableListOf()
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
private var initialized = false
|
||||
|
||||
init {
|
||||
directory.walk().filter {
|
||||
@@ -26,15 +27,15 @@ class Migrations(directory: File, private val connection: Connection): Migration
|
||||
it.isFile
|
||||
}.forEach { file ->
|
||||
if (file.name.endsWith(".up.sql")) {
|
||||
val up = file.readText()
|
||||
val down = file.path.substring(0, file.path.size - 7).let {
|
||||
file.path.substring(0, file.path.size - 7).let {
|
||||
try {
|
||||
File("$it.down.sql").readText()
|
||||
val down = File("$it.down.sql").readText()
|
||||
val up = file.readText()
|
||||
addQuery(file.name, up, down)
|
||||
} catch (e: FileNotFoundException) {
|
||||
throw DownMigrationNotDefined("$it.down.sql", e)
|
||||
}
|
||||
}
|
||||
addQuery(up, down)
|
||||
} else if (file.name.endsWith(".down.sql")) {
|
||||
// Nothing
|
||||
} else {
|
||||
@@ -53,14 +54,14 @@ class Migrations(directory: File, private val connection: Connection): Migration
|
||||
}
|
||||
|
||||
fun addFunction(sql: String): Migrations {
|
||||
DefinitionFunction.build(sql).forEach {
|
||||
DefinitionFunction(sql).let {
|
||||
functions[it.name] = Function(it, it, connection)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(up: String, down: String): Migrations {
|
||||
queries.add(Query(up, down, connection))
|
||||
fun addQuery(name: String, up: String, down: String): Migrations {
|
||||
queries.add(Query(name, up, down, connection))
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,9 @@ package fr.postgresjson.migration
|
||||
import fr.postgresjson.connexion.Connection
|
||||
|
||||
class Query(
|
||||
private val up: String,
|
||||
private val down: String,
|
||||
val name: String,
|
||||
val up: String,
|
||||
val down: String,
|
||||
private val connection: Connection
|
||||
): Migration {
|
||||
enum class Status(i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) }
|
||||
|
||||
Reference in New Issue
Block a user