From f8f3273f189939bbdeaed4daa7b9bab7f41a2418 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Fri, 7 Apr 2023 00:53:42 +0200 Subject: [PATCH] remove useless function migration.test() --- .../fr/postgresjson/migration/Function.kt | 10 ------- .../{Migrations.kt => MigrationExecutor.kt} | 29 +++++++++---------- .../postgresjson/migration/MigrationScript.kt | 14 ++------- .../kotlin/fr/postgresjson/MigrationTest.kt | 28 +++++++++--------- 4 files changed, 30 insertions(+), 51 deletions(-) rename src/main/kotlin/fr/postgresjson/migration/{Migrations.kt => MigrationExecutor.kt} (93%) diff --git a/src/main/kotlin/fr/postgresjson/migration/Function.kt b/src/main/kotlin/fr/postgresjson/migration/Function.kt index 367ee8a..1527174 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Function.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Function.kt @@ -77,16 +77,6 @@ data class Function( } } - override fun test(): Status { - connection.inTransaction { - up() - down() - sendQuery("ROLLBACK") - } - - return Status.OK - } - fun copy(): Function = this .copy(up = up, down = down, connection = connection, executedAt = executedAt) .also { it.doExecute = this.doExecute } diff --git a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt b/src/main/kotlin/fr/postgresjson/migration/MigrationExecutor.kt similarity index 93% rename from src/main/kotlin/fr/postgresjson/migration/Migrations.kt rename to src/main/kotlin/fr/postgresjson/migration/MigrationExecutor.kt index 4e7cd8c..754b093 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/MigrationExecutor.kt @@ -26,13 +26,12 @@ interface Migration { var doExecute: Action? fun up(): Status fun down(): Status - fun test(): Status enum class Status(val i: Int) { OK(2), UP_FAIL(0), DOWN_FAIL(1) } enum class Action { OK, UP, DOWN } } -class Migrations private constructor( +class MigrationExecutor private constructor( private val connection: Connection, private val migrationsScripts: MutableMap = mutableMapOf(), private val functions: MutableMap = mutableMapOf() @@ -52,8 +51,8 @@ class Migrations private constructor( migrationsScripts.clear() functions.clear() - getMigrationFromDB() - getMigrationFromDirectory(directories) + addMigrationFromDB() + addMigrationFromDirectory(directories) migrationsScripts.forEach { (_, query) -> if (query.doExecute === null) { @@ -73,7 +72,7 @@ class Migrations private constructor( /** * Get all migration from DB */ - private fun getMigrationFromDB() { + private fun addMigrationFromDB() { this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let { connection.execute(it, object : TypeReference>() {}) ?.map { function -> @@ -92,16 +91,16 @@ class Migrations private constructor( /** * Get all migration from multiples Directories */ - private fun getMigrationFromDirectory(directory: List) { - directory.forEach { - getMigrationFromDirectory(it) + private fun addMigrationFromDirectory(directories: List) { + directories.forEach { + addMigrationFromDirectory(it) } } /** * Get all migration from Directory */ - private fun getMigrationFromDirectory(directory: URI) { + private fun addMigrationFromDirectory(directory: URI) { val downs: MutableMap = mutableMapOf() directory.searchSqlFiles().apply { @@ -131,7 +130,7 @@ class Migrations private constructor( internal class DownMigrationNotDefined(path: String, cause: FileNotFoundException? = null) : Throwable("The file $path was not found", cause) - fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): Migrations { + fun addFunction(newDefinition: DefinitionFunction, callback: (Function) -> Unit = {}): MigrationExecutor { val currentFunction = functions[newDefinition.name] if (currentFunction === null || currentFunction `is different from` newDefinition) { val oldDefinition = functions[newDefinition.name]?.up ?: newDefinition @@ -147,15 +146,15 @@ class Migrations private constructor( return this } - fun addFunction(sql: String): Migrations { + fun addFunction(sql: String): MigrationExecutor { addFunction(DefinitionFunction(sql)) return this } - fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): Migrations = + fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): MigrationExecutor = addMigrationScript(up.name, up.script, down.script, callback) - fun addMigrationScript(name: String, up: String, down: String, callback: (MigrationScript) -> Unit = {}): Migrations { + fun addMigrationScript(name: String, up: String, down: String, callback: (MigrationScript) -> Unit = {}): MigrationExecutor { if (migrationsScripts[name] === null) { migrationsScripts[name] = MigrationScript(name, up, down, connection).apply { doExecute = Action.UP @@ -296,7 +295,7 @@ class Migrations private constructor( return list.toMap() } - private fun copy(): Migrations { + private fun copy(): MigrationExecutor { val queriesCopy = migrationsScripts.map { it.key to it.value.copy() }.toMap().toMutableMap() @@ -305,7 +304,7 @@ class Migrations private constructor( it.key to it.value.copy() }.toMap().toMutableMap() - return Migrations(connection, queriesCopy, functionsCopy) + return MigrationExecutor(connection, queriesCopy, functionsCopy) } fun status(): Map { diff --git a/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt b/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt index 6a66cd2..ebfb5ba 100644 --- a/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt +++ b/src/main/kotlin/fr/postgresjson/migration/MigrationScript.kt @@ -19,8 +19,8 @@ data class MigrationScript( return try { connection.sendQuery(up) - this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let { - connection.execute(it, listOf(name, up, down))?.let { query -> + this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let { sqlScript -> + connection.execute(sqlScript, listOf(name, up, down))?.let { query -> executedAt = query.executedAt doExecute = Action.OK } ?: error("No migration executed") @@ -42,16 +42,6 @@ data class MigrationScript( return Status.OK } - override fun test(): Status { - connection.inTransaction { - up() - down() - sendQuery("ROLLBACK") - } - - return Status.OK - } - fun copy(): MigrationScript { return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also { it.doExecute = this.doExecute diff --git a/src/test/kotlin/fr/postgresjson/MigrationTest.kt b/src/test/kotlin/fr/postgresjson/MigrationTest.kt index 6fac4fb..4f7b984 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -3,7 +3,7 @@ package fr.postgresjson import fr.postgresjson.connexion.Requester import fr.postgresjson.connexion.execute import fr.postgresjson.migration.Migration -import fr.postgresjson.migration.Migrations +import fr.postgresjson.migration.MigrationExecutor import org.amshove.kluent.invoking import org.amshove.kluent.`should be equal to` import org.amshove.kluent.`should contain` @@ -20,7 +20,7 @@ class MigrationTest : TestAbstract() { @Test fun `run up query`() { val resources = this::class.java.getResource("/sql/migrations")!!.toURI() - val m = Migrations(connection, resources) + val m = MigrationExecutor(connection, resources) m.up().apply { this `should contain` Pair("1", Migration.Status.OK) size `should be equal to` 1 @@ -33,14 +33,14 @@ class MigrationTest : TestAbstract() { fun `migration up Query should throw error if no down`() { val resources = this::class.java.getResource("/sql/migration_without_down")!!.toURI() invoking { - Migrations(resources, connection) - } shouldThrow Migrations.DownMigrationNotDefined::class + MigrationExecutor(resources, connection) + } shouldThrow MigrationExecutor.DownMigrationNotDefined::class } @Test fun `run forced down query`() { val resources = this::class.java.getResource("/sql/migrations")!!.toURI() - val m = Migrations(resources, connection) + val m = MigrationExecutor(resources, connection) repeat(3) { m.down(true).apply { this `should contain` Pair("1", Migration.Status.OK) @@ -52,10 +52,10 @@ class MigrationTest : TestAbstract() { @Test fun `run dry migrations`() { val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { runDry().size `should be equal to` 2 } - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { runDry().size `should be equal to` 2 } } @@ -63,7 +63,7 @@ class MigrationTest : TestAbstract() { @Test fun `run dry migrations launch twice`() { val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { runDry().size `should be equal to` 2 runDry().size `should be equal to` 2 } @@ -72,7 +72,7 @@ class MigrationTest : TestAbstract() { @Test fun `run migrations`() { val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { run().apply { size `should be equal to` 1 } @@ -83,12 +83,12 @@ class MigrationTest : TestAbstract() { fun `run migrations force down`() { val resources = this::class.java.getResource("/sql/real_migrations")!!.toURI() val resourcesFunctions = this::class.java.getResource("/sql/function/Test")!!.toURI() - Migrations(listOf(resources, resourcesFunctions), connection).apply { + MigrationExecutor(listOf(resources, resourcesFunctions), connection).apply { up().apply { size `should be equal to` 6 } } - Migrations(listOf(resources, resourcesFunctions), connection).apply { + MigrationExecutor(listOf(resources, resourcesFunctions), connection).apply { forceAllDown().apply { size `should be equal to` 6 } @@ -98,7 +98,7 @@ class MigrationTest : TestAbstract() { @Test fun `run functions migrations`() { val resources = this::class.java.getResource("/sql/function/Test")!!.toURI() - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { run().size `should be equal to` 5 } @@ -114,7 +114,7 @@ class MigrationTest : TestAbstract() { @Test fun `run functions migrations and drop if exist`() { val resources = this::class.java.getResource("/sql/function/Test1")!!.toURI() - Migrations(resources, connection).apply { + MigrationExecutor(resources, connection).apply { run().size `should be equal to` 1 } @@ -126,7 +126,7 @@ class MigrationTest : TestAbstract() { Assertions.assertEquals(objTest.name, "test") val resources2 = this::class.java.getResource("/sql/function/Test2")!!.toURI() - Migrations(resources2, connection).apply { + MigrationExecutor(resources2, connection).apply { run().size `should be equal to` 1 } }