This commit is contained in:
2025-12-12 21:40:26 +01:00
parent 07d12b8b85
commit 8fcc08ecb5
2 changed files with 24 additions and 30 deletions

View File

@@ -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
)
}

View File

@@ -15,8 +15,6 @@ object MigrationRepository {
name: String,
): Unit = transaction {
MigrationEntity
.new {
this.name = name
}
.new { this.name = name }
}
}