feature #15: fix migration test if run twice
This commit is contained in:
4
.idea/codeStyles/Project.xml
generated
4
.idea/codeStyles/Project.xml
generated
@@ -1,10 +1,14 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<JetCodeStyleSettings>
|
||||
<option name="SPACE_BEFORE_EXTEND_COLON" value="false" />
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
</JetCodeStyleSettings>
|
||||
<codeStyleSettings language="kotlin">
|
||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||
<indentOptions>
|
||||
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||
</indentOptions>
|
||||
</codeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
||||
@@ -7,7 +7,7 @@ import java.io.File
|
||||
import java.util.*
|
||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||
|
||||
class Function(
|
||||
data class Function(
|
||||
val up: DefinitionFunction,
|
||||
val down: DefinitionFunction,
|
||||
private val connection: Connection,
|
||||
@@ -26,8 +26,8 @@ class Function(
|
||||
up: String,
|
||||
down: String,
|
||||
connection: Connection,
|
||||
executedAt: Date? = null):
|
||||
this(
|
||||
executedAt: Date? = null
|
||||
): this(
|
||||
DefinitionFunction(up),
|
||||
DefinitionFunction(down),
|
||||
connection,
|
||||
@@ -74,4 +74,10 @@ class Function(
|
||||
|
||||
return Status.OK // TODO
|
||||
}
|
||||
|
||||
fun copy(): Function {
|
||||
return this.copy(up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,12 +30,12 @@ interface Migration {
|
||||
enum class Action { OK, UP, DOWN }
|
||||
}
|
||||
|
||||
class Migrations(directory: File, private val connection: Connection) {
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf()
|
||||
data class Migrations private constructor(
|
||||
private val connection: Connection,
|
||||
private val queries: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
private var initialized = false
|
||||
|
||||
init {
|
||||
) {
|
||||
constructor(directory: File, connection: Connection): this(connection) {
|
||||
initDB()
|
||||
getMigrationFromDB()
|
||||
getMigrationFromDirectory(directory)
|
||||
@@ -52,6 +52,8 @@ class Migrations(directory: File, private val connection: Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
private var initialized = false
|
||||
|
||||
/**
|
||||
* Get all migration from DB
|
||||
*/
|
||||
@@ -103,7 +105,9 @@ class Migrations(directory: File, private val connection: Connection) {
|
||||
}
|
||||
|
||||
enum class Direction { UP, DOWN }
|
||||
class DownMigrationNotDefined(path: String, cause: FileNotFoundException): Throwable("The file $path whas not found", cause)
|
||||
|
||||
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) {
|
||||
@@ -232,7 +236,11 @@ class Migrations(directory: File, private val connection: Connection) {
|
||||
return list.toMap()
|
||||
}
|
||||
|
||||
fun test(): Map<Pair<String, Direction>, Status> {
|
||||
fun runDry(): Map<Pair<String, Direction>, Status> {
|
||||
return this.copy().runTest()
|
||||
}
|
||||
|
||||
private fun runTest(): Map<Pair<String, Direction>, Status> {
|
||||
val list: MutableMap<Pair<String, Direction>, Status> = mutableMapOf()
|
||||
connection.connect().apply {
|
||||
sendQuery("BEGIN").join()
|
||||
@@ -248,6 +256,18 @@ class Migrations(directory: File, private val connection: Connection) {
|
||||
return list.toMap()
|
||||
}
|
||||
|
||||
fun copy(): Migrations {
|
||||
val queriesCopy = queries.map {
|
||||
it.key to it.value.copy()
|
||||
}.toMap().toMutableMap()
|
||||
|
||||
val functionsCopy = functions.map {
|
||||
it.key to it.value.copy()
|
||||
}.toMap().toMutableMap()
|
||||
|
||||
return Migrations(connection, queriesCopy, functionsCopy)
|
||||
}
|
||||
|
||||
fun status(): Map<String, Int> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import fr.postgresjson.migration.Migration.Action
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
class Query(
|
||||
data class Query(
|
||||
val name: String,
|
||||
val up: String,
|
||||
val down: String,
|
||||
@@ -57,4 +57,10 @@ class Query(
|
||||
|
||||
return Migration.Status.OK // TODO
|
||||
}
|
||||
|
||||
fun copy(): Query {
|
||||
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import java.io.File
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class MigrationTest(): TestAbstract() {
|
||||
@Test
|
||||
fun upQuery() {
|
||||
fun `run up query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, getConnextion())
|
||||
m.up().apply {
|
||||
@@ -33,7 +33,7 @@ class MigrationTest(): TestAbstract() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun downQuery() {
|
||||
fun `run forced down query`() {
|
||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||
val m = Migrations(resources, getConnextion())
|
||||
repeat(3) {
|
||||
@@ -45,18 +45,27 @@ class MigrationTest(): TestAbstract() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test up and down migrations`() {
|
||||
fun `run dry migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
test().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
test().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test run migrations`() {
|
||||
fun `run dry migrations launch twice`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
runDry().size `should be equal to` 2
|
||||
runDry().size `should be equal to` 2
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `run migrations`() {
|
||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||
Migrations(resources, getConnextion()).apply {
|
||||
run().apply {
|
||||
|
||||
Reference in New Issue
Block a user