This commit is contained in:
61
src/main/kotlin/com/infendro/migration/Plugin.kt
Normal file
61
src/main/kotlin/com/infendro/migration/Plugin.kt
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.infendro.migration
|
||||
|
||||
import com.infendro.migration.model.MigrationRepository
|
||||
import com.infendro.migration.model.MigrationTable
|
||||
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")
|
||||
|
||||
fun migrate(
|
||||
config: Config
|
||||
) {
|
||||
transaction {
|
||||
SchemaUtils.create(MigrationTable)
|
||||
}
|
||||
|
||||
for ((name, migrate) in config.migrations) {
|
||||
if (MigrationRepository.exists(name)) {
|
||||
continue
|
||||
}
|
||||
|
||||
transaction {
|
||||
try {
|
||||
migrate()
|
||||
logger.info("""Migration "$name" successful""")
|
||||
} catch (e: Exception) {
|
||||
logger.error("""Migration "$name" failed""")
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
MigrationRepository.insert(name)
|
||||
}
|
||||
}
|
||||
|
||||
fun migrate(
|
||||
configure: Config.() -> Unit
|
||||
) {
|
||||
val config = Config()
|
||||
config.configure()
|
||||
migrate(config)
|
||||
}
|
||||
|
||||
class Config {
|
||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||
|
||||
fun migration(
|
||||
name: String,
|
||||
migrate: Transaction.() -> Unit,
|
||||
) {
|
||||
migrations += Migration(name, migrate)
|
||||
}
|
||||
|
||||
data class Migration(
|
||||
val name: String,
|
||||
val migrate: 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