13 Commits
1.2.0 ... 1.1.0

Author SHA1 Message Date
8a452a6897 Add ShadowJar compatibility and Query file can be define name with comment
Add Migration and Query definition class
Add Docker DB for tests
2020-05-10 21:45:50 +02:00
Fabrice Lecomte
4c73ef1e0f set JVM to 11 for jitpack 2020-03-25 16:35:18 +01:00
Fabrice Lecomte
4c03eb7c87 set JVM to 11 2020-03-25 16:25:55 +01:00
Fabrice Lecomte
07678afbe7 set JVM to 11 2020-03-25 16:18:17 +01:00
Fabrice Lecomte
d3bcfdd627 config package 2020-03-25 15:26:19 +01:00
Fabrice Lecomte
deea5153f2 add method function.perform 2020-03-25 00:30:54 +01:00
Fabrice Lecomte
05fb868574 Add serialise list 2020-03-15 20:18:55 +01:00
Fabrice Lecomte
bbeec7bb60 add methode$ "isLastPage" to pagination 2020-02-24 20:41:29 +01:00
Fabrice Lecomte
012beb6884 Improve SQL log message 2020-02-19 16:46:20 +01:00
Fabrice Lecomte
45707f9734 change Idea config 2020-02-19 12:46:04 +01:00
Fabrice Lecomte
4e89aa072d RepositoryI::requester is now readonly 2020-02-11 20:39:43 +01:00
Fabrice Lecomte
b65e82a52f Improve UuidEntityVersioning 2020-01-29 17:03:22 +01:00
Fabrice Lecomte
898951e91a Add Immutable Entities 2020-01-23 00:40:47 +01:00
9 changed files with 27 additions and 37 deletions

2
.idea/misc.xml generated
View File

@@ -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="corretto-11" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="adopt-openjdk-11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="TaskProjectConfiguration">

View File

@@ -11,7 +11,7 @@ plugins {
id("fr.coppernic.versioning") version "3.1.2"
}
group = "com.github.flecomte"
group = "flecomte"
version = versioning.info.tag
repositories {

View File

@@ -20,10 +20,10 @@ interface Resource {
fun build(resource: String, path: Path): Resource =
try {
Migration(resource, path)
Function(resource, path)
} catch (e: ParseException) {
try {
Function(resource, path)
Migration(resource, path)
} catch (e: ParseException) {
try {
Query(resource, path)

View File

@@ -17,7 +17,7 @@ interface UuidEntityI : EntityRefI<UUID> {
}
abstract class Entity<T>(override val id: T) : EntityRefI<T>
open class UuidEntity(id: UUID? = null) : UuidEntityI, Entity<UUID>(id ?: UUID.randomUUID())
open class UuidEntity(override val id: UUID = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
/* Version */
interface EntityVersioning<ID, NUMBER> {
@@ -27,10 +27,8 @@ interface EntityVersioning<ID, NUMBER> {
class UuidEntityVersioning(
override var versionNumber: Int? = null,
versionId: UUID? = null
) : EntityVersioning<UUID, Int?> {
override val versionId: UUID = versionId ?: UUID.randomUUID()
}
override val versionId: UUID = UUID.randomUUID()
) : EntityVersioning<UUID, Int?>
/* Dates */
interface EntityCreatedAt {

View File

@@ -17,7 +17,7 @@ interface IdEntityI : EntityRefI<Int> {
}
abstract class Entity<T>(override var id: T? = null) : EntityRefI<T>
open class UuidEntity(id: UUID? = null) : UuidEntityI, Entity<UUID>(id ?: UUID.randomUUID())
open class UuidEntity(override var id: UUID? = UUID.randomUUID()) : UuidEntityI, Entity<UUID>(id)
open class IdEntity(override var id: Int? = null) : IdEntityI, Entity<Int>(id)
/* Version */
@@ -28,10 +28,8 @@ interface EntityVersioning<ID, NUMBER> {
class UuidEntityVersioning(
override var versionNumber: Int? = null,
versionId: UUID? = null
) : EntityVersioning<UUID, Int?> {
override var versionId: UUID = versionId ?: UUID.randomUUID()
}
override var versionId: UUID = UUID.randomUUID()
) : EntityVersioning<UUID, Int>
/* Dates */
interface EntityCreatedAt {
@@ -133,5 +131,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)

View File

@@ -36,13 +36,12 @@ interface Migration {
data class Migrations private constructor(
private val connection: Connection,
private val migrationsScripts: MutableMap<String, MigrationScript> = mutableMapOf(),
private val migrationsScripts: MutableMap<String, Query> = 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()
@@ -86,7 +85,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] = MigrationScript(query.filename, query.up, query.down, connection, query.executedAt)
migrationsScripts[query.filename] = Query(query.filename, query.up, query.down, connection, query.executedAt)
}
}
}
@@ -106,8 +105,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
@@ -120,7 +119,7 @@ data class Migrations private constructor(
val down = downs[migration.name] ?: throw DownMigrationNotDefined(migration.name + ".down.sql")
downs -= migration.name
addMigrationScript(migration, down)
addQuery(migration, down)
} else if (migration is DefinitionFunction) {
addFunction(migration)
}
@@ -154,12 +153,12 @@ data class Migrations private constructor(
return this
}
fun addMigrationScript(up: DefinitionMigration, down: DefinitionMigration, callback: (MigrationScript) -> Unit = {}): Migrations =
addMigrationScript(up.name, up.script, down.script, callback)
fun addQuery(up: DefinitionMigration, down: DefinitionMigration, callback: (Query) -> Unit = {}): Migrations =
addQuery(up.name, up.script, down.script, callback)
fun addMigrationScript(name: String, up: String, down: String, callback: (MigrationScript) -> Unit = {}): Migrations {
fun addQuery(name: String, up: String, down: String, callback: (Query) -> Unit = {}): Migrations {
if (migrationsScripts[name] === null) {
migrationsScripts[name] = MigrationScript(name, up, down, connection).apply {
migrationsScripts[name] = Query(name, up, down, connection).apply {
doExecute = Action.UP
}
} else {

View File

@@ -5,7 +5,7 @@ import fr.postgresjson.entity.mutable.Entity
import fr.postgresjson.migration.Migration.Action
import java.util.*
data class MigrationScript(
data class Query(
val name: String,
val up: String,
val down: String,
@@ -57,7 +57,7 @@ data class MigrationScript(
return Migration.Status.OK // TODO
}
fun copy(): MigrationScript {
fun copy(): Query {
return this.copy(name = name, up = up, down = down, connection = connection, executedAt = executedAt).also {
it.doExecute = this.doExecute
}

View File

@@ -5,7 +5,6 @@ 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
@@ -17,19 +16,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") {
try {
FileSystems.getFileSystem(uri)
} catch (e: FileSystemNotFoundException) {
FileSystems.newFileSystem(uri, emptyMap<String, Any>())
}
uri
val relativePath = uri.toString().substringAfter('!')
FileSystems
.newFileSystem(uri, emptyMap<String, Any>())
.getPath(relativePath)
.walk(5)
.asSequence()
.filter { it.fileName.toString().endsWith(".sql") }
.map { it.toUri().toURL() }
.map { this::class.java.getResource(path.toString()) }
.forEach {
logger.debug(it.toString())
yield(Resource.build(it))

View File

@@ -16,7 +16,7 @@ class MigrationTest() : TestAbstract() {
@Test
fun `run up query`() {
val resources = this::class.java.getResource("/sql/migrations").toURI()
val m = Migrations(connection, resources)
val m = Migrations(resources, connection)
m.up().apply {
this `should contain` Pair("1", Migration.Status.OK)
size `should be equal to` 1