Fix migrations of Functions
This commit is contained in:
@@ -11,7 +11,7 @@ plugins {
|
||||
id("fr.coppernic.versioning") version "3.1.2"
|
||||
}
|
||||
|
||||
group = "flecomte"
|
||||
group = "com.github.flecomte"
|
||||
version = versioning.info.tag
|
||||
|
||||
repositories {
|
||||
|
||||
@@ -20,10 +20,10 @@ interface Resource {
|
||||
|
||||
fun build(resource: String, path: Path): Resource =
|
||||
try {
|
||||
Function(resource, path)
|
||||
Migration(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Migration(resource, path)
|
||||
Function(resource, path)
|
||||
} catch (e: ParseException) {
|
||||
try {
|
||||
Query(resource, path)
|
||||
|
||||
@@ -5,7 +5,7 @@ import fr.postgresjson.entity.mutable.Entity
|
||||
import fr.postgresjson.migration.Migration.Action
|
||||
import java.util.*
|
||||
|
||||
data class Query(
|
||||
data class MigrationScript(
|
||||
val name: String,
|
||||
val up: String,
|
||||
val down: String,
|
||||
@@ -57,7 +57,7 @@ data class Query(
|
||||
return Migration.Status.OK // TODO
|
||||
}
|
||||
|
||||
fun copy(): Query {
|
||||
fun copy(): MigrationScript {
|
||||
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||
it.doExecute = this.doExecute
|
||||
}
|
||||
@@ -36,12 +36,13 @@ interface Migration {
|
||||
|
||||
data class Migrations private constructor(
|
||||
private val connection: Connection,
|
||||
private val migrationsScripts: MutableMap<String, Query> = mutableMapOf(),
|
||||
private val migrationsScripts: MutableMap<String, MigrationScript> = mutableMapOf(),
|
||||
private val functions: MutableMap<String, Function> = mutableMapOf()
|
||||
) {
|
||||
private var directories: List<URI> = emptyList()
|
||||
private val logger: Logger? by LoggerDelegate()
|
||||
constructor(directory: URI, connection: Connection) : this(listOf(directory), connection)
|
||||
constructor(connection: Connection, vararg directory: URI) : this(directory.toList(), connection)
|
||||
|
||||
constructor(directories: List<URI>, connection: Connection) : this(connection) {
|
||||
initDB()
|
||||
@@ -85,7 +86,7 @@ data class Migrations private constructor(
|
||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||
.map { query ->
|
||||
migrationsScripts[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
migrationsScripts[query.filename] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,8 +106,8 @@ data class Migrations private constructor(
|
||||
private fun getMigrationFromDirectory(directory: URI) {
|
||||
val downs: MutableMap<String, DefinitionMigration> = mutableMapOf()
|
||||
|
||||
/* Set Down Migration */
|
||||
directory.searchSqlFiles().apply {
|
||||
/* Set Down Migration */
|
||||
forEach { migration ->
|
||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.DOWN) {
|
||||
downs += migration.name to migration
|
||||
@@ -119,7 +120,7 @@ data class Migrations private constructor(
|
||||
val down = downs[migration.name] ?: throw DownMigrationNotDefined(migration.name + ".down.sql")
|
||||
downs -= migration.name
|
||||
|
||||
addQuery(migration, down)
|
||||
addMigrationScript(migration, down)
|
||||
} else if (migration is DefinitionFunction) {
|
||||
addFunction(migration)
|
||||
}
|
||||
@@ -153,12 +154,12 @@ data class Migrations private constructor(
|
||||
return this
|
||||
}
|
||||
|
||||
fun addQuery(up: DefinitionMigration, down: DefinitionMigration, callback: (Query) -> Unit = {}): Migrations =
|
||||
addQuery(up.name, up.script, down.script, callback)
|
||||
fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): Migrations =
|
||||
addMigrationScript(up.name, up.script, down.script, callback)
|
||||
|
||||
fun addQuery(name: String, up: String, down: String, callback: (Query) -> Unit = {}): Migrations {
|
||||
fun addMigrationScript(name: String, up: String, down: String, callback: (MigrationScript) -> Unit = {}): Migrations {
|
||||
if (migrationsScripts[name] === null) {
|
||||
migrationsScripts[name] = Query(name, up, down, connection).apply {
|
||||
migrationsScripts[name] = MigrationScript(name, up, down, connection).apply {
|
||||
doExecute = Action.UP
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
import java.nio.file.FileSystemNotFoundException
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.FileVisitOption
|
||||
import java.nio.file.Files
|
||||
@@ -16,11 +17,15 @@ fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
||||
fun URI.searchSqlFiles() = sequence<Resource> {
|
||||
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||
val uri: URI = this@searchSqlFiles
|
||||
logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""")
|
||||
if (uri.scheme == "jar") {
|
||||
val relativePath = uri.toString().substringAfter('!')
|
||||
FileSystems
|
||||
.newFileSystem(uri, emptyMap<String, Any>())
|
||||
.getPath(relativePath)
|
||||
try {
|
||||
FileSystems.getFileSystem(uri)
|
||||
} catch (e: FileSystemNotFoundException) {
|
||||
FileSystems.newFileSystem(uri, emptyMap<String, Any>())
|
||||
}
|
||||
|
||||
uri
|
||||
.walk(5)
|
||||
.asSequence()
|
||||
.filter { it.fileName.toString().endsWith(".sql") }
|
||||
|
||||
@@ -16,7 +16,7 @@ class MigrationTest() : TestAbstract() {
|
||||
@Test
|
||||
fun `run up query`() {
|
||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||
val m = Migrations(resources, connection)
|
||||
val m = Migrations(connection, resources)
|
||||
m.up().apply {
|
||||
this `should contain` Pair("1", Migration.Status.OK)
|
||||
size `should be equal to` 1
|
||||
|
||||
Reference in New Issue
Block a user