Compare commits
2 Commits
07d12b8b85
...
e9ba266d53
| Author | SHA1 | Date | |
|---|---|---|---|
|
e9ba266d53
|
|||
|
8fcc08ecb5
|
@@ -1,5 +1,5 @@
|
|||||||
group = "com.infendro"
|
group = "com.infendro"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
|||||||
@@ -11,17 +11,35 @@ import org.slf4j.LoggerFactory
|
|||||||
|
|
||||||
internal val logger: Logger = LoggerFactory.getLogger("Database")
|
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(
|
fun migrate(
|
||||||
config: Config
|
config: MigrationConfig,
|
||||||
) {
|
) {
|
||||||
transaction {
|
transaction {
|
||||||
SchemaUtils.create(MigrationTable)
|
SchemaUtils.create(MigrationTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
for ((name, migrate) in config.migrations) {
|
for ((name, migrate) in config.migrations) {
|
||||||
if (MigrationRepository.exists(name)) {
|
if (MigrationRepository.exists(name))
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
|
|
||||||
transaction {
|
transaction {
|
||||||
try {
|
try {
|
||||||
@@ -37,32 +55,10 @@ fun migrate(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNUSED")
|
|
||||||
fun migrate(
|
fun migrate(
|
||||||
configure: Config.() -> Unit
|
configure: MigrationConfig.() -> Unit,
|
||||||
) {
|
) {
|
||||||
val config = Config()
|
val config = MigrationConfig()
|
||||||
config.configure()
|
config.configure()
|
||||||
migrate(config)
|
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,
|
name: String,
|
||||||
): Unit = transaction {
|
): Unit = transaction {
|
||||||
MigrationEntity
|
MigrationEntity
|
||||||
.new {
|
.new { this.name = name }
|
||||||
this.name = name
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user