initial commit
This commit is contained in:
55
src/main/kotlin/com/infendro/migration/Plugin.kt
Normal file
55
src/main/kotlin/com/infendro/migration/Plugin.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
)
|
||||
}
|
||||
16
src/main/kotlin/com/infendro/migration/model/Migration.kt
Normal file
16
src/main/kotlin/com/infendro/migration/model/Migration.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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