feature #9: add lock on execute migration
This commit is contained in:
@@ -159,7 +159,13 @@ class Migrations(directory: File, private val connection: Connection) {
|
||||
}
|
||||
}
|
||||
|
||||
fun up(): Map<String, Status> {
|
||||
private fun lock() {
|
||||
File(this::class.java.getResource("/sql/migration/lockMigrationTables.sql").toURI()).let {
|
||||
connection.connect().sendQuery(it.readText()).join()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun up(): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = 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<String, Status> {
|
||||
internal fun down(force: Boolean = false): Map<String, Status> {
|
||||
val list: MutableMap<String, Status> = 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<Pair<String, Direction>, Status> {
|
||||
val list: MutableMap<Pair<String, Direction>, 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<Pair<String, Direction>, Status> {
|
||||
val list: MutableMap<Pair<String, Direction>, Status> = mutableMapOf()
|
||||
connection.connect().apply {
|
||||
|
||||
5
src/main/resources/sql/migration/lockMigrationTables.sql
Normal file
5
src/main/resources/sql/migration/lockMigrationTables.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
LOCK TABLE
|
||||
migration.history,
|
||||
migration.functions
|
||||
IN ACCESS EXCLUSIVE MODE
|
||||
NOWAIT
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user