-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linear: EM-2038 GitHub: #82
- Loading branch information
Showing
6 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package gg.essential.vigilance.data | ||
|
||
import gg.essential.vigilance.Vigilant | ||
|
||
/** See [Vigilant.migrations]. */ | ||
fun interface Migration { | ||
fun apply(config: MutableMap<String, Any?>) | ||
} |
108 changes: 108 additions & 0 deletions
108
src/main/kotlin/gg/essential/vigilance/impl/migration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package gg.essential.vigilance.impl | ||
|
||
import gg.essential.vigilance.data.Migration | ||
import gg.essential.vigilance.impl.nightconfig.core.Config | ||
|
||
private const val META_KEY = "__meta" | ||
private val VERSION_KEY = listOf(META_KEY, "version") | ||
private fun migrationLogKey(migration: Int): List<String> = listOf(META_KEY, "migration_log", "$migration") | ||
|
||
internal fun migrate(root: Config, migrations: List<Migration>) { | ||
val fileVersion = root[VERSION_KEY] ?: 0 | ||
if (fileVersion < migrations.size) { | ||
var oldMap = root.toMap() | ||
for (migration in fileVersion until migrations.size) { | ||
val newMap = oldMap.toMutableMap() | ||
|
||
migrations[migration].apply(newMap) | ||
|
||
applyMigration(root, migration, oldMap, newMap) | ||
|
||
root.update(VERSION_KEY, migration + 1) | ||
|
||
oldMap = newMap | ||
assert(oldMap == root.toMap()) | ||
} | ||
} else if (fileVersion > migrations.size) { | ||
for (migration in fileVersion - 1 downTo migrations.size) { | ||
rollbackMigration(root, migration) | ||
root.update(VERSION_KEY, migration) | ||
} | ||
} | ||
} | ||
|
||
private fun applyMigration(root: Config, migration: Int, oldMap: Map<String, Any?>, newMap: Map<String, Any?>) { | ||
val migrationLog = root.createSubConfig() | ||
|
||
for ((key, oldValue) in oldMap) { | ||
if (key !in newMap) { | ||
migrationLog.update(listOf("changed", key), oldValue) | ||
root.purge<Any?>(key.split(".")) | ||
} else { | ||
val newValue = newMap[key] | ||
if (newValue != oldValue) { | ||
migrationLog.update(listOf("changed", key), oldValue) | ||
root.update(key, newValue) | ||
} | ||
} | ||
} | ||
|
||
val added = mutableListOf<String>() | ||
for ((key, newValue) in newMap) { | ||
if (key !in oldMap) { | ||
added.add(key) | ||
root.update(key, newValue) | ||
} | ||
} | ||
if (added.isNotEmpty()) { | ||
migrationLog.update(listOf("added"), added) | ||
} | ||
|
||
if (!migrationLog.isEmpty) { | ||
root.update(migrationLogKey(migration), migrationLog) | ||
} | ||
} | ||
|
||
private fun rollbackMigration(root: Config, migration: Int) { | ||
val migrationLog = root.purge<Config?>(migrationLogKey(migration)) ?: return | ||
val added = migrationLog.get<List<String>>(listOf("added")) ?: emptyList() | ||
val changed = migrationLog.get<Config>(listOf("changed"))?.valueMap() ?: emptyMap() | ||
|
||
for (key in added) { | ||
root.purge<Any?>(key.split(".")) | ||
} | ||
for ((key, oldValue) in changed) { | ||
root.update(key, oldValue) | ||
} | ||
} | ||
|
||
/** Removes the value at the given key as well as any now-empty intermediate nodes. */ | ||
private fun <T> Config.purge(keys: List<String>): T? { | ||
return if (keys.size > 1) { | ||
val child = get<Any?>(listOf(keys[0])) as? Config ?: return null | ||
val removed = child.purge<T>(keys.subList(1, keys.size)) | ||
if (child.isEmpty) { | ||
remove<Any?>(listOf(keys[0])) | ||
} | ||
removed | ||
} else { | ||
remove(keys) | ||
} | ||
} | ||
|
||
/** Converts the nested [Config] into a flat [Map]. Does not include any [META_KEY] entries. */ | ||
private fun Config.toMap(): Map<String, Any?> { | ||
val result = mutableMapOf<String, Any?>() | ||
fun visit(config: Config, prefix: String) { | ||
for ((key, value) in config.valueMap()) { | ||
if (key == META_KEY) continue | ||
if (value is Config) { | ||
visit(value, "$prefix$key.") | ||
} else { | ||
result[prefix + key] = value | ||
} | ||
} | ||
} | ||
visit(this, "") | ||
return result | ||
} |
149 changes: 149 additions & 0 deletions
149
src/test/kotlin/gg/essential/vigilance/impl/MigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package gg.essential.vigilance.impl | ||
|
||
import gg.essential.vigilance.data.Migration | ||
import gg.essential.vigilance.impl.nightconfig.core.Config | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class MigrationTest { | ||
@Test | ||
fun testUpToDate() { | ||
val input = config("__meta" to mapOf("version" to 2), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 2), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testNoOpMigration() { | ||
val input = config("__meta" to mapOf("version" to 1), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 2), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testAddMigration() { | ||
val input = config("__meta" to mapOf("version" to 1), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration { it["new"] = 43 })) | ||
assertEquals(config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("added" to listOf("new")))), | ||
"test" to 42, | ||
"new" to 43, | ||
), input) | ||
} | ||
|
||
@Test | ||
fun testChangeMigration() { | ||
val input = config("__meta" to mapOf("version" to 1), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration { it["test"] = it["test"] as Int + 1 })) | ||
assertEquals(config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("changed" to mapOf("test" to 42)))), | ||
"test" to 43, | ||
), input) | ||
} | ||
|
||
@Test | ||
fun testRemoveMigration() { | ||
val input = config("__meta" to mapOf("version" to 1), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration { it.remove("test") })) | ||
assertEquals(config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("changed" to mapOf("test" to 42)))), | ||
), input) | ||
} | ||
|
||
@Test | ||
fun testMultipleMigrations() { | ||
val input = config("__meta" to mapOf("version" to 1), "test" to 42) | ||
migrate(input, listOf(Migration {}, Migration { it["test"] = 1 }, Migration { it["test"] = it["test"] as Int + 1 })) | ||
assertEquals(config( | ||
"__meta" to mapOf("version" to 3, "migration_log" to mapOf( | ||
"1" to mapOf("changed" to mapOf("test" to 42)), | ||
"2" to mapOf("changed" to mapOf("test" to 1)), | ||
)), | ||
"test" to 2, | ||
), input) | ||
} | ||
|
||
@Test | ||
fun testAddRollback() { | ||
val input = config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("added" to listOf("new")))), | ||
"test" to 42, | ||
"new" to 43, | ||
) | ||
migrate(input, listOf(Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 1), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testChangeRollback() { | ||
val input = config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("changed" to mapOf("test" to 42)))), | ||
"test" to 43, | ||
) | ||
migrate(input, listOf(Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 1), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testRemoveRollback() { | ||
val input = config( | ||
"__meta" to mapOf("version" to 2, "migration_log" to mapOf("1" to mapOf("changed" to mapOf("test" to 42)))), | ||
) | ||
migrate(input, listOf(Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 1), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testMultipleRollback() { | ||
val input = config( | ||
"__meta" to mapOf("version" to 3, "migration_log" to mapOf( | ||
"1" to mapOf("changed" to mapOf("test" to 42)), | ||
"2" to mapOf("changed" to mapOf("test" to 1)), | ||
)), | ||
"test" to 2, | ||
) | ||
migrate(input, listOf(Migration {})) | ||
assertEquals(config("__meta" to mapOf("version" to 1), "test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testNoMigrations() { | ||
val input = config("test" to 42) | ||
migrate(input, listOf()) | ||
assertEquals(config("test" to 42), input) | ||
} | ||
|
||
@Test | ||
fun testInitialMigration() { | ||
val input = config("test" to 42) | ||
migrate(input, listOf(Migration { config -> | ||
assert(config == mapOf("test" to 42)) | ||
config["test"] = 43 | ||
})) | ||
assertEquals(config( | ||
"__meta" to mapOf("version" to 1, "migration_log" to mapOf("0" to mapOf("changed" to mapOf("test" to 42)))), | ||
"test" to 43, | ||
), input) | ||
} | ||
|
||
private fun config(vararg entries: Pair<String, Any?>): Config = | ||
config(entries.toMap()) | ||
|
||
private fun config(map: Map<String, Any?>): Config { | ||
fun visit(map: Map<String, Any?>, config: Config) { | ||
for ((key, value) in map) { | ||
if (value is Map<*, *>) { | ||
val inner = config.createSubConfig() | ||
@Suppress("UNCHECKED_CAST") | ||
visit(value as Map<String, Any?>, inner) | ||
config.update(listOf(key), inner) | ||
} else { | ||
config.update(listOf(key), value) | ||
} | ||
} | ||
} | ||
val config = Config.inMemory() | ||
visit(map, config) | ||
return config | ||
} | ||
} |