diff --git a/.idea/.gitignore b/.idea/.gitignore index 8e30d9e..3364827 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -5,4 +5,5 @@ /dataSources.local.xml /compiler.xml /uiDesigner.xml -/dataSources.xml \ No newline at end of file +/dataSources.xml +/sonarlint/ \ 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 68b57d7..962340f 100644 --- a/src/main/kotlin/fr/postgresjson/migration/Migrations.kt +++ b/src/main/kotlin/fr/postgresjson/migration/Migrations.kt @@ -159,7 +159,13 @@ class Migrations(directory: File, private val connection: Connection) { } } - fun up(): Map { + private fun lock() { + File(this::class.java.getResource("/sql/migration/lockMigrationTables.sql").toURI()).let { + connection.connect().sendQuery(it.readText()).join() + } + } + + internal fun up(): Map { val list: MutableMap = mutableMapOf() queries.forEach { it.value.let { query -> @@ -184,7 +190,7 @@ class Migrations(directory: File, private val connection: Connection) { return list.toMap() } - fun down(force: Boolean = false): Map { + internal fun down(force: Boolean = false): Map { val list: MutableMap = mutableMapOf() queries.forEach { it.value.let { query -> @@ -209,6 +215,23 @@ class Migrations(directory: File, private val connection: Connection) { return list.toMap() } + fun run(): Map, Status> { + val list: MutableMap, Status> = mutableMapOf() + connection.connect().apply { + sendQuery("BEGIN").join() + lock() + up().map { + list[Pair(it.key, Direction.UP)] = it.value + } + down(true).map { + list[Pair(it.key, Direction.DOWN)] = it.value + } + sendQuery("COMMIT").join() + } + + return list.toMap() + } + fun test(): Map, Status> { val list: MutableMap, Status> = mutableMapOf() connection.connect().apply { diff --git a/src/main/resources/sql/migration/lockMigrationTables.sql b/src/main/resources/sql/migration/lockMigrationTables.sql new file mode 100644 index 0000000..45b16d9 --- /dev/null +++ b/src/main/resources/sql/migration/lockMigrationTables.sql @@ -0,0 +1,5 @@ +LOCK TABLE + migration.history, + migration.functions +IN ACCESS EXCLUSIVE MODE +NOWAIT \ 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 af59604..0b5bc4b 100644 --- a/src/test/kotlin/fr/postgresjson/MigrationTest.kt +++ b/src/test/kotlin/fr/postgresjson/MigrationTest.kt @@ -16,9 +16,9 @@ class MigrationTest(): TestAbstract() { fun upQuery() { val resources = File(this::class.java.getResource("/sql/migrations").toURI()) val m = Migrations(resources, getConnextion()) - m.up().let { - it `should contain` Pair("1", Migration.Status.OK) - it.size `should be equal to` 1 + m.up().apply { + this `should contain` Pair("1", Migration.Status.OK) + size `should be equal to` 1 } m.up().size `should be equal to` 0 @@ -37,9 +37,9 @@ class MigrationTest(): TestAbstract() { val resources = File(this::class.java.getResource("/sql/migrations").toURI()) val m = Migrations(resources, getConnextion()) repeat(3) { - m.down(true).let { - it `should contain` Pair("1", Migration.Status.OK) - it.size `should be equal to` 1 + m.down(true).apply { + this `should contain` Pair("1", Migration.Status.OK) + size `should be equal to` 1 } } } @@ -54,4 +54,14 @@ class MigrationTest(): TestAbstract() { test().size `should be equal to` 2 } } + + @Test + fun `test run migrations`() { + val resources = File(this::class.java.getResource("/sql/real_migrations").toURI()) + Migrations(resources, getConnextion()).apply { + run().apply { + size `should be equal to` 2 + } + } + } } \ No newline at end of file