https://github.com/boswelja/kotlin-migration
A Kotlin library to enable easier program migrations, inspired by AndroidX Room
https://github.com/boswelja/kotlin-migration
kotlin library migration-tool
Last synced: 5 months ago
JSON representation
A Kotlin library to enable easier program migrations, inspired by AndroidX Room
- Host: GitHub
- URL: https://github.com/boswelja/kotlin-migration
- Owner: boswelja
- License: apache-2.0
- Created: 2021-06-24T06:58:24.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-12-27T04:17:46.000Z (6 months ago)
- Last Synced: 2025-12-28T20:16:22.738Z (6 months ago)
- Topics: kotlin, library, migration-tool
- Language: Kotlin
- Homepage:
- Size: 534 KB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# kotlin-migration
A Kotlin library to enable easier program migrations, inspired by AndroidX Room
## Usage
### Add the dependency
```kotlin
dependencies {
implementation("io.github.boswelja.migration:migration-core:$version")
}
```
### Set up a `Migrator`
The basis of this library is a `Migrator`, that will run any `Migration` you give it. To create a `Migrator`, you should create your own class extending `Migrator`.
```kotlin
class MigrationManager : Migrator(
currentVersion = 1,
migrations = listOf(
// Your migrations here
)
) {
override suspend fun getOldVersion(): Int {
// You should fetch your previous version here
return 1
}
}
```
That's it! You can then call `migrate()` on an instance of your `Migrator` implementation.
```kotlin
val migrationManager = MigrationManager()
coroutineScope.launch {
migrationManager.migrate()
}
```
### Creating Migrations
Currently, this library provides 2 types of migrations, `VersionMigration` and `ConditionalMigration`. Both should be passed into your `Migrator` constructor.
#### `VersionMigration`
```kotlin
val migration1_2 = object : VersionMigration(fromVersion = 1, toVersion = 2) {
override suspend fun migrate(): Boolean {
// Do your migration here
return true
}
}
```
#### `ConditionalMigration`
```kotlin
val migration1_2 = object : ConditionalMigration() {
override suspend fun shouldMigrate(fromVersion: Int): Boolean {
// Check whether this migration should be run, optionally taking a version into account
var shouldMigrate = ...
return shouldMigrate
}
override suspend fun migrate(): Boolean {
// Do your migration here
return true
}
}
```
#### Creating your own migration type
You can implement the `Migration` interface on a class to build your own migration with more complex logic.
```kotlin
abstract class VersionMigration(
val fromVersion: Int,
final override val toVersion: Int
) : Migration {
final override suspend fun shouldMigrate(fromVersion: Int): Boolean {
// Run this migration if fromVersion is the same as the version provided by the user
return fromVersion == this.fromVersion
}
}
```