fix: resource dir
This commit is contained in:
@@ -87,21 +87,11 @@ class Requester(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initRequester(req: Requester): Requester {
|
private fun initRequester(req: Requester): Requester {
|
||||||
if (queriesDirectory === null) {
|
if (queriesDirectory !== null) {
|
||||||
val resource = this::class.java.getResource("/sql/query")
|
|
||||||
if (resource !== null) {
|
|
||||||
req.addQuery(File(resource.toURI()))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
req.addQuery(queriesDirectory)
|
req.addQuery(queriesDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (functionsDirectory === null) {
|
if (functionsDirectory !== null) {
|
||||||
val resource = this::class.java.getResource("/sql/function")
|
|
||||||
if (resource !== null) {
|
|
||||||
req.addFunction(File(resource.toURI()))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
req.addFunction(functionsDirectory)
|
req.addFunction(functionsDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package fr.postgresjson.migration
|
|||||||
import fr.postgresjson.connexion.Connection
|
import fr.postgresjson.connexion.Connection
|
||||||
import fr.postgresjson.migration.Migration.Action
|
import fr.postgresjson.migration.Migration.Action
|
||||||
import fr.postgresjson.migration.Migration.Status
|
import fr.postgresjson.migration.Migration.Status
|
||||||
import java.io.File
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import fr.postgresjson.definition.Function as DefinitionFunction
|
import fr.postgresjson.definition.Function as DefinitionFunction
|
||||||
|
|
||||||
@@ -35,10 +34,10 @@ data class Function(
|
|||||||
)
|
)
|
||||||
|
|
||||||
override fun up(): Status {
|
override fun up(): Status {
|
||||||
connection.exec(up.script)
|
connection.sendQuery(up.script)
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/insertFunction.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/insertFunction.sql")!!.readText().let {
|
||||||
connection.selectOne<MigrationEntity>(it.readText(), listOf(up))?.let { function ->
|
connection.selectOne<MigrationEntity>(it, listOf(up))?.let { function ->
|
||||||
executedAt = function.executedAt
|
executedAt = function.executedAt
|
||||||
doExecute = Action.OK
|
doExecute = Action.OK
|
||||||
}
|
}
|
||||||
@@ -47,10 +46,10 @@ data class Function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun down(): Status {
|
override fun down(): Status {
|
||||||
connection.exec(down.script)
|
connection.sendQuery(down.script)
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/deleteFunction.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
|
||||||
connection.exec(it.readText(), listOf(down))
|
connection.exec(it, listOf(down))
|
||||||
}
|
}
|
||||||
return Status.OK
|
return Status.OK
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package fr.postgresjson.migration
|
package fr.postgresjson.migration
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference
|
||||||
import com.github.jasync.sql.db.util.size
|
import com.github.jasync.sql.db.util.size
|
||||||
import fr.postgresjson.connexion.Connection
|
import fr.postgresjson.connexion.Connection
|
||||||
|
import fr.postgresjson.definition.Function.FunctionNotFound
|
||||||
import fr.postgresjson.entity.Entity
|
import fr.postgresjson.entity.Entity
|
||||||
import fr.postgresjson.migration.Migration.Action
|
import fr.postgresjson.migration.Migration.Action
|
||||||
import fr.postgresjson.migration.Migration.Status
|
import fr.postgresjson.migration.Migration.Status
|
||||||
@@ -58,15 +60,15 @@ data class Migrations private constructor(
|
|||||||
* Get all migration from DB
|
* Get all migration from DB
|
||||||
*/
|
*/
|
||||||
private fun getMigrationFromDB() {
|
private fun getMigrationFromDB() {
|
||||||
File(this::class.java.getResource("/sql/migration/findAllFunction.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
|
||||||
connection.select<MigrationEntity>(it.readText())
|
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||||
.map { function ->
|
.map { function ->
|
||||||
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/findAllHistory.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||||
connection.select<MigrationEntity>(it.readText())
|
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
|
||||||
.map { query ->
|
.map { query ->
|
||||||
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||||
}
|
}
|
||||||
@@ -78,9 +80,6 @@ data class Migrations private constructor(
|
|||||||
*/
|
*/
|
||||||
private fun getMigrationFromDirectory(directory: File) {
|
private fun getMigrationFromDirectory(directory: File) {
|
||||||
directory.walk().filter {
|
directory.walk().filter {
|
||||||
it.isDirectory
|
|
||||||
}.forEach { subDirectory ->
|
|
||||||
subDirectory.walk().filter {
|
|
||||||
it.isFile
|
it.isFile
|
||||||
}.forEach { file ->
|
}.forEach { file ->
|
||||||
if (file.name.endsWith(".up.sql")) {
|
if (file.name.endsWith(".up.sql")) {
|
||||||
@@ -98,7 +97,10 @@ data class Migrations private constructor(
|
|||||||
// Nothing
|
// Nothing
|
||||||
} else {
|
} else {
|
||||||
val fileContent = file.readText()
|
val fileContent = file.readText()
|
||||||
|
try {
|
||||||
addFunction(fileContent)
|
addFunction(fileContent)
|
||||||
|
} catch(e: FunctionNotFound) {
|
||||||
|
// Nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,19 +155,19 @@ data class Migrations private constructor(
|
|||||||
|
|
||||||
private fun initDB() {
|
private fun initDB() {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
File(this::class.java.getResource("/sql/migration/createHistoryShema.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/createHistoryShema.sql")!!.readText().let {
|
||||||
connection.sendQuery(it.readText())
|
connection.sendQuery(it)
|
||||||
}
|
}
|
||||||
File(this::class.java.getResource("/sql/migration/createFunctionShema.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/createFunctionShema.sql")!!.readText().let {
|
||||||
connection.sendQuery(it.readText())
|
connection.sendQuery(it)
|
||||||
}
|
}
|
||||||
initialized = true
|
initialized = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun lock() {
|
private fun lock() {
|
||||||
File(this::class.java.getResource("/sql/migration/lockMigrationTables.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/lockMigrationTables.sql")!!.readText().let {
|
||||||
connection.sendQuery(it.readText())
|
connection.sendQuery(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package fr.postgresjson.migration
|
|||||||
import fr.postgresjson.connexion.Connection
|
import fr.postgresjson.connexion.Connection
|
||||||
import fr.postgresjson.entity.Entity
|
import fr.postgresjson.entity.Entity
|
||||||
import fr.postgresjson.migration.Migration.Action
|
import fr.postgresjson.migration.Migration.Action
|
||||||
import java.io.File
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
data class Query(
|
data class Query(
|
||||||
@@ -16,10 +15,10 @@ data class Query(
|
|||||||
override var doExecute: Action? = null
|
override var doExecute: Action? = null
|
||||||
|
|
||||||
override fun up(): Migration.Status {
|
override fun up(): Migration.Status {
|
||||||
connection.exec(up)
|
connection.sendQuery(up)
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/insertHistory.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
|
||||||
connection.selectOne<MigrationEntity>(it.readText(), listOf(name, up, down))?.let { query ->
|
connection.selectOne<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
|
||||||
executedAt = query.executedAt
|
executedAt = query.executedAt
|
||||||
doExecute = Action.OK
|
doExecute = Action.OK
|
||||||
}
|
}
|
||||||
@@ -29,10 +28,10 @@ data class Query(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun down(): Migration.Status {
|
override fun down(): Migration.Status {
|
||||||
connection.exec(down)
|
connection.sendQuery(down)
|
||||||
|
|
||||||
File(this::class.java.getResource("/sql/migration/deleteHistory.sql").toURI()).let {
|
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
|
||||||
connection.exec(it.readText(), listOf(name))
|
connection.exec(it, listOf(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Migration.Status.OK
|
return Migration.Status.OK
|
||||||
|
|||||||
@@ -32,18 +32,6 @@ class MigrationTest(): TestAbstract() {
|
|||||||
} shouldThrow Migrations.DownMigrationNotDefined::class
|
} shouldThrow Migrations.DownMigrationNotDefined::class
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `run forced down query`() {
|
|
||||||
val resources = File(this::class.java.getResource("/sql/migrations").toURI())
|
|
||||||
val m = Migrations(resources, getConnextion())
|
|
||||||
repeat(3) {
|
|
||||||
m.down(true).apply {
|
|
||||||
this `should contain` Pair("1", Migration.Status.OK)
|
|
||||||
size `should be equal to` 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `run dry migrations`() {
|
fun `run dry migrations`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
@@ -77,6 +65,11 @@ class MigrationTest(): TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run migrations force down`() {
|
fun `run migrations force down`() {
|
||||||
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
val resources = File(this::class.java.getResource("/sql/real_migrations").toURI())
|
||||||
|
Migrations(resources, getConnextion()).apply {
|
||||||
|
up().apply {
|
||||||
|
size `should be equal to` 1
|
||||||
|
}
|
||||||
|
}
|
||||||
Migrations(resources, getConnextion()).apply {
|
Migrations(resources, getConnextion()).apply {
|
||||||
forceAllDown().apply {
|
forceAllDown().apply {
|
||||||
size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
|
|||||||
Reference in New Issue
Block a user