refactor
This commit is contained in:
@@ -11,17 +11,35 @@ import org.slf4j.LoggerFactory
|
||||
|
||||
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
||||
|
||||
class MigrationConfig {
|
||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||
|
||||
fun migration(
|
||||
name: String,
|
||||
migrate: Transaction.() -> Unit,
|
||||
) {
|
||||
if (migrations.any { it.name == name })
|
||||
throw DuplicateMigrationException(name)
|
||||
|
||||
migrations += Migration(name, migrate)
|
||||
}
|
||||
|
||||
data class Migration(
|
||||
val name: String,
|
||||
val migrate: Transaction.() -> Unit,
|
||||
)
|
||||
}
|
||||
|
||||
fun migrate(
|
||||
config: Config
|
||||
config: MigrationConfig,
|
||||
) {
|
||||
transaction {
|
||||
SchemaUtils.create(MigrationTable)
|
||||
}
|
||||
|
||||
for ((name, migrate) in config.migrations) {
|
||||
if (MigrationRepository.exists(name)) {
|
||||
if (MigrationRepository.exists(name))
|
||||
continue
|
||||
}
|
||||
|
||||
transaction {
|
||||
try {
|
||||
@@ -37,32 +55,10 @@ fun migrate(
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNUSED")
|
||||
fun migrate(
|
||||
configure: Config.() -> Unit
|
||||
configure: MigrationConfig.() -> Unit,
|
||||
) {
|
||||
val config = Config()
|
||||
val config = MigrationConfig()
|
||||
config.configure()
|
||||
migrate(config)
|
||||
}
|
||||
|
||||
class Config {
|
||||
internal val migrations: MutableList<Migration> = mutableListOf()
|
||||
|
||||
@Suppress("UNUSED")
|
||||
fun migration(
|
||||
name: String,
|
||||
migrate: Transaction.() -> Unit,
|
||||
) {
|
||||
if (migrations.any { it.name == name }) {
|
||||
throw DuplicateMigrationException(name)
|
||||
}
|
||||
|
||||
migrations += Migration(name, migrate)
|
||||
}
|
||||
|
||||
data class Migration(
|
||||
val name: String,
|
||||
val migrate: Transaction.() -> Unit
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ object MigrationRepository {
|
||||
name: String,
|
||||
): Unit = transaction {
|
||||
MigrationEntity
|
||||
.new {
|
||||
this.name = name
|
||||
}
|
||||
.new { this.name = name }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user