# AutoMigration

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/AutoMigration.kt+class:androidx.room3.AutoMigration) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = )
  @Retention(value = AnnotationRetention.BINARY)
  annotation AutoMigration
  ```

*** ** * ** ***

Declares an automatic migration on a `https://developer.android.com/reference/kotlin/androidx/room3/Database`.

An automatic migration is a `https://developer.android.com/reference/kotlin/androidx/room3/migration/Migration` that is generated via the use of database schema files at two versions of a `https://developer.android.com/reference/kotlin/androidx/room3/RoomDatabase`. Room automatically detects changes on the database between these two schemas, and constructs a `https://developer.android.com/reference/kotlin/androidx/room3/migration/Migration` to migrate between the two versions. In case of ambiguous scenarios (e.g. column/table rename/deletes), additional information is required, and can be provided via a `https://developer.android.com/reference/kotlin/androidx/room3/migration/AutoMigrationSpec` defined in the `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#spec()` property.

An auto migration must define the 'from' and 'to' versions of the schema for which a migration implementation will be generated. A class that implements `AutoMigrationSpec` can be declared in the `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#spec()` property to either provide more information for ambiguous scenarios or execute callbacks during the migration.

If there are any column or table renames or deletes between the two versions of the database provided then it is said that there are ambiguous scenarios in the migration. In such scenarios then an `https://developer.android.com/reference/kotlin/androidx/room3/migration/AutoMigrationSpec` is required and the class provided must be annotated with the relevant change annotation(s): `https://developer.android.com/reference/kotlin/androidx/room3/RenameColumn`, `https://developer.android.com/reference/kotlin/androidx/room3/RenameTable`, `https://developer.android.com/reference/kotlin/androidx/room3/DeleteColumn` or `https://developer.android.com/reference/kotlin/androidx/room3/DeleteTable`. When no ambiguous scenario is present, then the `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#spec()` property is optional.

If an auto migration is defined for a database, then `https://developer.android.com/reference/kotlin/androidx/room3/Database#exportSchema()` must be set to `true`.

Example:

```kotlin
@Database(
    version = MusicDatabase.LATEST_VERSION,
    entities = [
        Song.class,
        Artist.class
    ],
    autoMigrations = [
        AutoMigration (from = 1, to = 2),
        AutoMigration (from = 2, to = 3, spec = MusicDatabase.MyExampleAutoMigrationSpec::class)
    ],
    exportSchema = true
)
abstract class MusicDatabase  : RoomDatabase() {
    const val LATEST_VERSION = 3

    @DeleteTable(tableName = "Album")
    @RenameTable(fromTableName = "Singer", toTableName = "Artist")
    @RenameColumn(
        tableName = "Song",
        fromColumnName = "songName",
        toColumnName = "songTitle"
     )
    @DeleteColumn(tableName = "Song", columnName = "genre")
    class MyExampleAutoMigrationSpec : AutoMigrationSpec {
        override suspend fun onPostMigrate(connection: SQLiteConnection) {
            // Invoked once auto migration is done
        }
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/RoomDatabase` |   |
| `https://developer.android.com/reference/kotlin/androidx/room3/migration/AutoMigrationSpec` |   |
| `https://developer.android.com/reference/kotlin/androidx/room3/ProvidedAutoMigrationSpec` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#AutoMigration(kotlin.Int,kotlin.Int,kotlin.reflect.KClass)(from: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, to: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, spec: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>)` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#from()` Version of the database schema to migrate from. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#spec()` User implemented custom auto migration spec. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/AutoMigration#to()` Version of the database schema to migrate to. | Cmn |

## Public constructors

### AutoMigration

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

```
AutoMigration(from: Int, to: Int, spec: KClass<*> = Any::class)
```

## Public properties

### from

Cmn

```
val from: Int
```

Version of the database schema to migrate from.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Version number of the database to migrate from. |

### spec

Cmn

```
val spec: KClass<*>
```

User implemented custom auto migration spec.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | The auto migration specification or none if the user has not implemented a spec |

### to

Cmn

```
val to: Int
```

Version of the database schema to migrate to.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Version number of the database to migrate to. |