override fun ensureCurrentVersion()

in shared/src/commonMain/kotlin/org/jetbrains/kotlinconf/storage/MultiplatformSettingsStorage.kt [91:134]


    override fun ensureCurrentVersion() {
        var version = settings.getInt(Keys.STORAGE_VERSION, 0)

        taggedLogger.log { "Storage version is $version" }

        if (version == 0) {
            taggedLogger.log { "Unknown previous storage version, performing destructive migration" }
            destructiveUpgrade()
            return
        }

        if (version > CURRENT_STORAGE_VERSION) {
            taggedLogger.log { "Storage version not recognized, performing destructive migration" }
            destructiveUpgrade()
            return
        }

        if (version == CURRENT_STORAGE_VERSION) {
            taggedLogger.log { "Storage version matches expected version, no need to migrate" }
            return
        }

        while (version < CURRENT_STORAGE_VERSION) {
            taggedLogger.log { "Finding migrations from $version to $CURRENT_STORAGE_VERSION..." }

            // Find a migration from the current version that takes us as far forward as possible
            val nextMigration = migrations.filter { it.from == version }.maxByOrNull { it.to }
            if (nextMigration == null) {
                taggedLogger.log { "No matching migrations found" }

                // Failed to find a migration path to latest, fall back to destructive
                destructiveUpgrade()
                return
            }

            taggedLogger.log { "Running migration from ${nextMigration.from} to ${nextMigration.to}" }

            nextMigration.migrate()
            version = nextMigration.to
            settings.set(Keys.STORAGE_VERSION, version)

            taggedLogger.log { "Successfully migrated to $version" }
        }
    }