Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5023f229ca | |||
| 55b547db75 | |||
| 377459f430 | |||
| df99bf4743 | |||
| fc4fde490f | |||
| a6d2a4d34f | |||
| 232158f85d | |||
| 3c998edb3c | |||
| 92722b0fef | |||
| 265c586198 | |||
| 440ecc8193 | |||
| 1bae960139 | |||
| 1dec96551d |
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -3,7 +3,7 @@
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="adopt-openjdk-11" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="corretto-11" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="TaskProjectConfiguration">
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -17,7 +17,7 @@ interface UuidEntityI : EntityRefI<UUID> {
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override val id: T) : EntityRefI<T>
|
||||
open class UuidEntity(override val id: UUID = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
|
||||
open class UuidEntity(id: UUID? = null) : UuidEntityI, Entity<UUID>(id ?: UUID.randomUUID())
|
||||
|
||||
/* Version */
|
||||
interface EntityVersioning<ID, NUMBER> {
|
||||
@@ -27,8 +27,10 @@ interface EntityVersioning<ID, NUMBER> {
|
||||
|
||||
class UuidEntityVersioning(
|
||||
override var versionNumber: Int? = null,
|
||||
override val versionId: UUID = UUID.randomUUID()
|
||||
) : EntityVersioning<UUID, Int?>
|
||||
versionId: UUID? = null
|
||||
) : EntityVersioning<UUID, Int?> {
|
||||
override val versionId: UUID = versionId ?: UUID.randomUUID()
|
||||
}
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
|
||||
@@ -17,7 +17,7 @@ interface IdEntityI : EntityRefI<Int> {
|
||||
}
|
||||
|
||||
abstract class Entity<T>(override var id: T? = null) : EntityRefI<T>
|
||||
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
|
||||
open class UuidEntity(id: UUID? = null) : UuidEntityI, Entity<UUID>(id ?: UUID.randomUUID())
|
||||
open class IdEntity(override var id: Int? = null) : IdEntityI, Entity<Int>(id)
|
||||
|
||||
/* Version */
|
||||
@@ -28,8 +28,10 @@ interface EntityVersioning<ID, NUMBER> {
|
||||
|
||||
class UuidEntityVersioning(
|
||||
override var versionNumber: Int? = null,
|
||||
override var versionId: UUID = UUID.randomUUID()
|
||||
) : EntityVersioning<UUID, Int>
|
||||
versionId: UUID? = null
|
||||
) : EntityVersioning<UUID, Int?> {
|
||||
override var versionId: UUID = versionId ?: UUID.randomUUID()
|
||||
}
|
||||
|
||||
/* Dates */
|
||||
interface EntityCreatedAt {
|
||||
@@ -131,5 +133,5 @@ abstract class UuidEntityExtended<T, UserT : EntityI>(
|
||||
publishedBy: UserT?
|
||||
) :
|
||||
EntityImp<T, UserT>(updatedBy),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(),
|
||||
EntityVersioning<UUID, Int?> by UuidEntityVersioning(),
|
||||
Published<UserT> by EntityPublishedImp(publishedBy)
|
||||
|
||||
@@ -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,15 +17,19 @@ 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") }
|
||||
.map { this::class.java.getResource(path.toString()) }
|
||||
.map { it.toUri().toURL() }
|
||||
.forEach {
|
||||
logger.debug(it.toString())
|
||||
yield(Resource.build(it))
|
||||
|
||||
@@ -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