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