feature #9: add lock on execute migration
This commit is contained in:
1
.idea/.gitignore
generated
vendored
1
.idea/.gitignore
generated
vendored
@@ -6,3 +6,4 @@
|
|||||||
/compiler.xml
|
/compiler.xml
|
||||||
/uiDesigner.xml
|
/uiDesigner.xml
|
||||||
/dataSources.xml
|
/dataSources.xml
|
||||||
|
/sonarlint/
|
||||||
@@ -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()
|
val list: MutableMap<String, Status> = mutableMapOf()
|
||||||
queries.forEach {
|
queries.forEach {
|
||||||
it.value.let { query ->
|
it.value.let { query ->
|
||||||
@@ -184,7 +190,7 @@ class Migrations(directory: File, private val connection: Connection) {
|
|||||||
return list.toMap()
|
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()
|
val list: MutableMap<String, Status> = mutableMapOf()
|
||||||
queries.forEach {
|
queries.forEach {
|
||||||
it.value.let { query ->
|
it.value.let { query ->
|
||||||
@@ -209,6 +215,23 @@ class Migrations(directory: File, private val connection: Connection) {
|
|||||||
return list.toMap()
|
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> {
|
fun test(): 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 {
|
||||||
|
|||||||
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() {
|
fun upQuery() {
|
||||||
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().let {
|
m.up().apply {
|
||||||
it `should contain` Pair("1", Migration.Status.OK)
|
this `should contain` Pair("1", Migration.Status.OK)
|
||||||
it.size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
}
|
}
|
||||||
|
|
||||||
m.up().size `should be equal to` 0
|
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 resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
||||||
val m = Migrations(resources, getConnextion())
|
val m = Migrations(resources, getConnextion())
|
||||||
repeat(3) {
|
repeat(3) {
|
||||||
m.down(true).let {
|
m.down(true).apply {
|
||||||
it `should contain` Pair("1", Migration.Status.OK)
|
this `should contain` Pair("1", Migration.Status.OK)
|
||||||
it.size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,4 +54,14 @@ class MigrationTest(): TestAbstract() {
|
|||||||
test().size `should be equal to` 2
|
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