This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.0.0"
|
version = "1.0.1"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12,8 +13,7 @@ plugins {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.ktor.server)
|
implementation(libs.ktor.server)
|
||||||
implementation(libs.exposed)
|
implementation(libs.migration)
|
||||||
implementation(libs.exposed.dao)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.2.10"
|
kotlin = "2.2.10"
|
||||||
ktor = "3.2.3"
|
ktor = "3.2.3"
|
||||||
exposed = "0.60.0"
|
migration = "1.0.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||||
@@ -9,5 +9,4 @@ kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
|||||||
[libraries]
|
[libraries]
|
||||||
ktor-server = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
ktor-server = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
||||||
|
|
||||||
exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
|
migration = { module = "com.infendro:migration", version.ref = "migration" }
|
||||||
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
|
|
||||||
|
|||||||
@@ -1,55 +1,12 @@
|
|||||||
package com.infendro.ktor.migration
|
package com.infendro.ktor.migration
|
||||||
|
|
||||||
import com.infendro.ktor.migration.model.MigrationRepository
|
import com.infendro.migration.Config
|
||||||
import com.infendro.ktor.migration.model.MigrationTable
|
import com.infendro.migration.migrate
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import org.jetbrains.exposed.sql.SchemaUtils
|
|
||||||
import org.jetbrains.exposed.sql.Transaction
|
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
|
||||||
import org.slf4j.Logger
|
|
||||||
import org.slf4j.LoggerFactory
|
|
||||||
|
|
||||||
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
|
||||||
|
|
||||||
val Migration = createApplicationPlugin(
|
val Migration = createApplicationPlugin(
|
||||||
name = "Migration",
|
name = "Migration",
|
||||||
createConfiguration = { MigrationConfig() },
|
createConfiguration = { Config() },
|
||||||
) {
|
) {
|
||||||
transaction {
|
migrate(pluginConfig)
|
||||||
SchemaUtils.create(MigrationTable)
|
|
||||||
}
|
|
||||||
|
|
||||||
val (migrations) = pluginConfig
|
|
||||||
for ((name, body) in migrations) {
|
|
||||||
if (MigrationRepository.exists(name))
|
|
||||||
continue
|
|
||||||
|
|
||||||
transaction {
|
|
||||||
try {
|
|
||||||
body()
|
|
||||||
logger.info("""Migration "$name" successful""")
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("""Migration "$name" failed""")
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MigrationRepository.insert(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data class MigrationConfig(
|
|
||||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
|
||||||
) {
|
|
||||||
fun migration(
|
|
||||||
name: String,
|
|
||||||
block: Transaction.() -> Unit,
|
|
||||||
) {
|
|
||||||
migrations += Migration(name, block)
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Migration(
|
|
||||||
val name: String,
|
|
||||||
val body: Transaction.() -> Unit
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.infendro.ktor.migration.model
|
|
||||||
|
|
||||||
import org.jetbrains.exposed.dao.LongEntity
|
|
||||||
import org.jetbrains.exposed.dao.LongEntityClass
|
|
||||||
import org.jetbrains.exposed.dao.id.EntityID
|
|
||||||
import org.jetbrains.exposed.dao.id.LongIdTable
|
|
||||||
|
|
||||||
object MigrationTable : LongIdTable("migration", "id") {
|
|
||||||
val name = text("name")
|
|
||||||
}
|
|
||||||
|
|
||||||
class MigrationEntity(id: EntityID<Long>) : LongEntity(id) {
|
|
||||||
companion object : LongEntityClass<MigrationEntity>(MigrationTable)
|
|
||||||
|
|
||||||
var name by MigrationTable.name
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package com.infendro.ktor.migration.model
|
|
||||||
|
|
||||||
import org.jetbrains.exposed.sql.transactions.transaction
|
|
||||||
|
|
||||||
object MigrationRepository {
|
|
||||||
fun exists(
|
|
||||||
name: String,
|
|
||||||
): Boolean = transaction {
|
|
||||||
MigrationEntity
|
|
||||||
.find { MigrationTable.name eq name }
|
|
||||||
.any()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun insert(
|
|
||||||
name: String,
|
|
||||||
): Unit = transaction {
|
|
||||||
MigrationEntity
|
|
||||||
.new {
|
|
||||||
this.name = name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user