Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8bb3b61f2d | |||
| 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">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
<file type="web" url="file://$PROJECT_DIR$" />
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
<component name="TaskProjectConfiguration">
|
<component name="TaskProjectConfiguration">
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ plugins {
|
|||||||
id("fr.coppernic.versioning") version "3.1.2"
|
id("fr.coppernic.versioning") version "3.1.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "flecomte"
|
group = "com.github.flecomte"
|
||||||
version = versioning.info.tag
|
version = versioning.info.tag
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|||||||
@@ -20,10 +20,10 @@ interface Resource {
|
|||||||
|
|
||||||
fun build(resource: String, path: Path): Resource =
|
fun build(resource: String, path: Path): Resource =
|
||||||
try {
|
try {
|
||||||
Function(resource, path)
|
Migration(resource, path)
|
||||||
} catch (e: ParseException) {
|
} catch (e: ParseException) {
|
||||||
try {
|
try {
|
||||||
Migration(resource, path)
|
Function(resource, path)
|
||||||
} catch (e: ParseException) {
|
} catch (e: ParseException) {
|
||||||
try {
|
try {
|
||||||
Query(resource, path)
|
Query(resource, path)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ interface UuidEntityI : EntityRefI<UUID> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class Entity<T>(override val id: T) : EntityRefI<T>
|
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 */
|
/* Version */
|
||||||
interface EntityVersioning<ID, NUMBER> {
|
interface EntityVersioning<ID, NUMBER> {
|
||||||
@@ -26,9 +26,11 @@ interface EntityVersioning<ID, NUMBER> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class UuidEntityVersioning(
|
class UuidEntityVersioning(
|
||||||
override var versionNumber: Int? = null,
|
override val versionNumber: Int,
|
||||||
override val versionId: UUID = UUID.randomUUID()
|
versionId: UUID? = null
|
||||||
) : EntityVersioning<UUID, Int?>
|
) : EntityVersioning<UUID, Int> {
|
||||||
|
override val versionId: UUID = versionId ?: UUID.randomUUID()
|
||||||
|
}
|
||||||
|
|
||||||
/* Dates */
|
/* Dates */
|
||||||
interface EntityCreatedAt {
|
interface EntityCreatedAt {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ interface IdEntityI : EntityRefI<Int> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class Entity<T>(override var id: T? = null) : EntityRefI<T>
|
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)
|
open class IdEntity(override var id: Int? = null) : IdEntityI, Entity<Int>(id)
|
||||||
|
|
||||||
/* Version */
|
/* Version */
|
||||||
@@ -28,8 +28,10 @@ interface EntityVersioning<ID, NUMBER> {
|
|||||||
|
|
||||||
class UuidEntityVersioning(
|
class UuidEntityVersioning(
|
||||||
override var versionNumber: Int? = null,
|
override var versionNumber: Int? = null,
|
||||||
override var versionId: UUID = UUID.randomUUID()
|
versionId: UUID? = null
|
||||||
) : EntityVersioning<UUID, Int>
|
) : EntityVersioning<UUID, Int> {
|
||||||
|
override var versionId: UUID = versionId ?: UUID.randomUUID()
|
||||||
|
}
|
||||||
|
|
||||||
/* Dates */
|
/* Dates */
|
||||||
interface EntityCreatedAt {
|
interface EntityCreatedAt {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import fr.postgresjson.entity.mutable.Entity
|
|||||||
import fr.postgresjson.migration.Migration.Action
|
import fr.postgresjson.migration.Migration.Action
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
data class Query(
|
data class MigrationScript(
|
||||||
val name: String,
|
val name: String,
|
||||||
val up: String,
|
val up: String,
|
||||||
val down: String,
|
val down: String,
|
||||||
@@ -57,7 +57,7 @@ data class Query(
|
|||||||
return Migration.Status.OK // TODO
|
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 {
|
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
|
||||||
it.doExecute = this.doExecute
|
it.doExecute = this.doExecute
|
||||||
}
|
}
|
||||||
@@ -36,12 +36,13 @@ interface Migration {
|
|||||||
|
|
||||||
data class Migrations private constructor(
|
data class Migrations private constructor(
|
||||||
private val connection: Connection,
|
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 val functions: MutableMap<String, Function> = mutableMapOf()
|
||||||
) {
|
) {
|
||||||
private var directories: List<URI> = emptyList()
|
private var directories: List<URI> = emptyList()
|
||||||
private val logger: Logger? by LoggerDelegate()
|
private val logger: Logger? by LoggerDelegate()
|
||||||
constructor(directory: URI, connection: Connection) : this(listOf(directory), connection)
|
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) {
|
constructor(directories: List<URI>, connection: Connection) : this(connection) {
|
||||||
initDB()
|
initDB()
|
||||||
@@ -85,7 +86,7 @@ data class Migrations private constructor(
|
|||||||
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
this::class.java.classLoader.getResource("sql/migration/findAllHistory.sql")!!.readText().let {
|
||||||
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
connection.select<MigrationEntity>(it, object : TypeReference<List<MigrationEntity>>() {})
|
||||||
.map { query ->
|
.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) {
|
private fun getMigrationFromDirectory(directory: URI) {
|
||||||
val downs: MutableMap<String, DefinitionMigration> = mutableMapOf()
|
val downs: MutableMap<String, DefinitionMigration> = mutableMapOf()
|
||||||
|
|
||||||
/* Set Down Migration */
|
|
||||||
directory.searchSqlFiles().apply {
|
directory.searchSqlFiles().apply {
|
||||||
|
/* Set Down Migration */
|
||||||
forEach { migration ->
|
forEach { migration ->
|
||||||
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.DOWN) {
|
if (migration is DefinitionMigration && migration.direction == DefinitionMigration.Direction.DOWN) {
|
||||||
downs += migration.name to migration
|
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")
|
val down = downs[migration.name] ?: throw DownMigrationNotDefined(migration.name + ".down.sql")
|
||||||
downs -= migration.name
|
downs -= migration.name
|
||||||
|
|
||||||
addQuery(migration, down)
|
addMigrationScript(migration, down)
|
||||||
} else if (migration is DefinitionFunction) {
|
} else if (migration is DefinitionFunction) {
|
||||||
addFunction(migration)
|
addFunction(migration)
|
||||||
}
|
}
|
||||||
@@ -153,12 +154,12 @@ data class Migrations private constructor(
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addQuery(up: DefinitionMigration, down: DefinitionMigration, callback: (Query) -> Unit = {}): Migrations =
|
fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): Migrations =
|
||||||
addQuery(up.name, up.script, down.script, callback)
|
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) {
|
if (migrationsScripts[name] === null) {
|
||||||
migrationsScripts[name] = Query(name, up, down, connection).apply {
|
migrationsScripts[name] = MigrationScript(name, up, down, connection).apply {
|
||||||
doExecute = Action.UP
|
doExecute = Action.UP
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.slf4j.Logger
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.nio.file.FileSystemNotFoundException
|
||||||
import java.nio.file.FileSystems
|
import java.nio.file.FileSystems
|
||||||
import java.nio.file.FileVisitOption
|
import java.nio.file.FileVisitOption
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
@@ -16,15 +17,19 @@ fun URL.searchSqlFiles() = this.toURI().searchSqlFiles()
|
|||||||
fun URI.searchSqlFiles() = sequence<Resource> {
|
fun URI.searchSqlFiles() = sequence<Resource> {
|
||||||
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
val logger: Logger = LoggerFactory.getLogger("sqlFilesSearch")
|
||||||
val uri: URI = this@searchSqlFiles
|
val uri: URI = this@searchSqlFiles
|
||||||
|
logger.debug("""SQL files found in "${uri.toString().substringAfter('!')}" :""")
|
||||||
if (uri.scheme == "jar") {
|
if (uri.scheme == "jar") {
|
||||||
val relativePath = uri.toString().substringAfter('!')
|
try {
|
||||||
FileSystems
|
FileSystems.getFileSystem(uri)
|
||||||
.newFileSystem(uri, emptyMap<String, Any>())
|
} catch (e: FileSystemNotFoundException) {
|
||||||
.getPath(relativePath)
|
FileSystems.newFileSystem(uri, emptyMap<String, Any>())
|
||||||
|
}
|
||||||
|
|
||||||
|
uri
|
||||||
.walk(5)
|
.walk(5)
|
||||||
.asSequence()
|
.asSequence()
|
||||||
.filter { it.fileName.toString().endsWith(".sql") }
|
.filter { it.fileName.toString().endsWith(".sql") }
|
||||||
.map { this::class.java.getResource(path.toString()) }
|
.map { it.toUri().toURL() }
|
||||||
.forEach {
|
.forEach {
|
||||||
logger.debug(it.toString())
|
logger.debug(it.toString())
|
||||||
yield(Resource.build(it))
|
yield(Resource.build(it))
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class MigrationTest() : TestAbstract() {
|
|||||||
@Test
|
@Test
|
||||||
fun `run up query`() {
|
fun `run up query`() {
|
||||||
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
val resources = this::class.java.getResource("/sql/migrations").toURI()
|
||||||
val m = Migrations(resources, connection)
|
val m = Migrations(connection, resources)
|
||||||
m.up().apply {
|
m.up().apply {
|
||||||
this `should contain` Pair("1", Migration.Status.OK)
|
this `should contain` Pair("1", Migration.Status.OK)
|
||||||
size `should be equal to` 1
|
size `should be equal to` 1
|
||||||
|
|||||||
Reference in New Issue
Block a user