diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 1bec35e..c5dd6f7 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,10 +1,14 @@ + \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index fe09d26..3f35da3 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -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 + } + } } \ No newline at end of file diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt index 962340f..a10efef 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -27,15 +27,15 @@ interface Migration { fun status(): Status 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) { - private val queries: MutableMap = mutableMapOf() +data class Migrations private constructor( + private val connection: Connection, + private val queries: MutableMap = mutableMapOf(), private val functions: MutableMap = 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, Status> { + fun runDry(): Map, Status> { + return this.copy().runTest() + } + + private fun runTest(): Map, Status> { val list: MutableMap, 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 { TODO("not implemented") } diff --git a/src/main/kotlin/fr/postgresjson/migration/Query.kt b/src/main/kotlin/fr/postgresjson/migration/Query.kt index 0e0d7cf..7a16b22 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Query.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Query.kt @@ -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 + } + } } \ No newline at end of file diff --git a/src/test/kotlin/fr/postgresjson/MigrationTest.kt b/src/test/kotlin/fr/postgresjson/MigrationTest.kt index 0b5bc4b..061781a 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -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 {