Compare commits
2 Commits
4a5d53be0f
...
8b8614b4de
| Author | SHA1 | Date | |
|---|---|---|---|
|
8b8614b4de
|
|||
|
9f32143b60
|
@@ -1,7 +1,8 @@
|
||||
group = "com.infendro"
|
||||
version = "1.0.0"
|
||||
version = "1.0.1"
|
||||
|
||||
repositories {
|
||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
@@ -12,8 +13,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(libs.ktor.server)
|
||||
implementation(libs.exposed)
|
||||
implementation(libs.exposed.dao)
|
||||
implementation(libs.migration)
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[versions]
|
||||
kotlin = "2.2.10"
|
||||
ktor = "3.2.3"
|
||||
exposed = "0.60.0"
|
||||
migration = "1.0.0"
|
||||
|
||||
[plugins]
|
||||
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||
@@ -9,5 +9,4 @@ kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||
[libraries]
|
||||
ktor-server = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor" }
|
||||
|
||||
exposed = { module = "org.jetbrains.exposed:exposed-core", version.ref = "exposed" }
|
||||
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
|
||||
migration = { module = "com.infendro:migration", version.ref = "migration" }
|
||||
|
||||
12
src/main/kotlin/com/infendro/ktor/migration/Plugin.kt
Normal file
12
src/main/kotlin/com/infendro/ktor/migration/Plugin.kt
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.infendro.ktor.migration
|
||||
|
||||
import com.infendro.migration.Config
|
||||
import com.infendro.migration.migrate
|
||||
import io.ktor.server.application.*
|
||||
|
||||
val Migration = createApplicationPlugin(
|
||||
name = "Migration",
|
||||
createConfiguration = { Config() },
|
||||
) {
|
||||
migrate(pluginConfig)
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.infendro.migration
|
||||
|
||||
import com.infendro.migration.model.MigrationRepository
|
||||
import com.infendro.migration.model.MigrationTable
|
||||
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(
|
||||
name = "Migration",
|
||||
createConfiguration = { MigrationConfig() },
|
||||
) {
|
||||
transaction {
|
||||
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.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.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