fix: resource dir

This commit is contained in:
2019-08-02 19:32:12 +02:00
parent cddb4c352b
commit 56a141a34c
5 changed files with 50 additions and 67 deletions

View File

@@ -87,21 +87,11 @@ class Requester(
}
private fun initRequester(req: Requester): Requester {
if (queriesDirectory === null) {
val resource = this::class.java.getResource("/sql/query")
if (resource !== null) {
req.addQuery(File(resource.toURI()))
}
} else {
if (queriesDirectory !== null) {
req.addQuery(queriesDirectory)
}
if (functionsDirectory === null) {
val resource = this::class.java.getResource("/sql/function")
if (resource !== null) {
req.addFunction(File(resource.toURI()))
}
} else {
if (functionsDirectory !== null) {
req.addFunction(functionsDirectory)
}

View File

@@ -3,7 +3,6 @@ package fr.postgresjson.migration
import fr.postgresjson.connexion.Connection
import fr.postgresjson.migration.Migration.Action
import fr.postgresjson.migration.Migration.Status
import java.io.File
import java.util.*
import fr.postgresjson.definition.Function as DefinitionFunction
@@ -35,10 +34,10 @@ data class Function(
)
override fun up(): Status {
connection.exec(up.script)
connection.sendQuery(up.script)
File(this::class.java.getResource("/sql/migration/insertFunction.sql").toURI()).let {
connection.selectOne<MigrationEntity>(it.readText(), listOf(up))?.let { function ->
this::class.java.classLoader.getResource("sql/migration/insertFunction.sql")!!.readText().let {
connection.selectOne<MigrationEntity>(it, listOf(up))?.let { function ->
executedAt = function.executedAt
doExecute = Action.OK
}
@@ -47,10 +46,10 @@ data class Function(
}
override fun down(): Status {
connection.exec(down.script)
connection.sendQuery(down.script)
File(this::class.java.getResource("/sql/migration/deleteFunction.sql").toURI()).let {
connection.exec(it.readText(), listOf(down))
this::class.java.classLoader.getResource("sql/migration/deleteFunction.sql")!!.readText().let {
connection.exec(it, listOf(down))
}
return Status.OK
}

View File

@@ -1,7 +1,9 @@
package fr.postgresjson.migration
import com.fasterxml.jackson.core.type.TypeReference
import com.github.jasync.sql.db.util.size
import fr.postgresjson.connexion.Connection
import fr.postgresjson.definition.Function.FunctionNotFound
import fr.postgresjson.entity.Entity
import fr.postgresjson.migration.Migration.Action
import fr.postgresjson.migration.Migration.Status
@@ -58,15 +60,15 @@ data class Migrations private constructor(
* Get all migration from DB
*/
private fun getMigrationFromDB() {
File(this::class.java.getResource("/sql/migration/findAllFunction.sql").toURI()).let {
connection.select<MigrationEntity>(it.readText())
this::class.java.classLoader.getResource("sql/migration/findAllFunction.sql")!!.readText().let {
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
.map { function ->
functions[function.filename] = Function(function.up, function.down, connection, function.executedAt)
}
}
File(this::class.java.getResource("/sql/migration/findAllHistory.sql").toURI()).let {
connection.select<MigrationEntity>(it.readText())
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
connection.select<MigrationEntity>(it, object: TypeReference<List<MigrationEntity>>() {})
.map { query ->
queries[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
}
@@ -78,27 +80,27 @@ data class Migrations private constructor(
*/
private fun getMigrationFromDirectory(directory: File) {
directory.walk().filter {
it.isDirectory
}.forEach { subDirectory ->
subDirectory.walk().filter {
it.isFile
}.forEach { file ->
if (file.name.endsWith(".up.sql")) {
file.path.substring(0, file.path.size - 7).let {
try {
val down = File("$it.down.sql").readText()
val up = file.readText()
val name = file.name.substring(0, file.name.size - 7)
addQuery(name, up, down)
} catch (e: FileNotFoundException) {
throw DownMigrationNotDefined("$it.down.sql", e)
}
it.isFile
}.forEach { file ->
if (file.name.endsWith(".up.sql")) {
file.path.substring(0, file.path.size - 7).let {
try {
val down = File("$it.down.sql").readText()
val up = file.readText()
val name = file.name.substring(0, file.name.size - 7)
addQuery(name, up, down)
} catch (e: FileNotFoundException) {
throw DownMigrationNotDefined("$it.down.sql", e)
}
} else if (file.name.endsWith(".down.sql")) {
// Nothing
} else {
val fileContent = file.readText()
}
} else if (file.name.endsWith(".down.sql")) {
// Nothing
} else {
val fileContent = file.readText()
try {
addFunction(fileContent)
} catch(e: FunctionNotFound) {
// Nothing
}
}
}
@@ -153,19 +155,19 @@ data class Migrations private constructor(
private fun initDB() {
if (!initialized) {
File(this::class.java.getResource("/sql/migration/createHistoryShema.sql").toURI()).let {
connection.sendQuery(it.readText())
this::class.java.classLoader.getResource("sql/migration/createHistoryShema.sql")!!.readText().let {
connection.sendQuery(it)
}
File(this::class.java.getResource("/sql/migration/createFunctionShema.sql").toURI()).let {
connection.sendQuery(it.readText())
this::class.java.classLoader.getResource("sql/migration/createFunctionShema.sql")!!.readText().let {
connection.sendQuery(it)
}
initialized = true
}
}
private fun lock() {
File(this::class.java.getResource("/sql/migration/lockMigrationTables.sql").toURI()).let {
connection.sendQuery(it.readText())
this::class.java.classLoader.getResource("sql/migration/lockMigrationTables.sql")!!.readText().let {
connection.sendQuery(it)
}
}

View File

@@ -3,7 +3,6 @@ package fr.postgresjson.migration
import fr.postgresjson.connexion.Connection
import fr.postgresjson.entity.Entity
import fr.postgresjson.migration.Migration.Action
import java.io.File
import java.util.*
data class Query(
@@ -16,10 +15,10 @@ data class Query(
override var doExecute: Action? = null
override fun up(): Migration.Status {
connection.exec(up)
connection.sendQuery(up)
File(this::class.java.getResource("/sql/migration/insertHistory.sql").toURI()).let {
connection.selectOne<MigrationEntity>(it.readText(), listOf(name, up, down))?.let { query ->
this::class.java.classLoader.getResource("sql/migration/insertHistory.sql")!!.readText().let {
connection.selectOne<MigrationEntity>(it, listOf(name, up, down))?.let { query ->
executedAt = query.executedAt
doExecute = Action.OK
}
@@ -29,10 +28,10 @@ data class Query(
}
override fun down(): Migration.Status {
connection.exec(down)
connection.sendQuery(down)
File(this::class.java.getResource("/sql/migration/deleteHistory.sql").toURI()).let {
connection.exec(it.readText(), listOf(name))
this::class.java.classLoader.getResource("sql/migration/deleteHistory.sql")!!.readText().let {
connection.exec(it, listOf(name))
}
return Migration.Status.OK