Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/app/appsearch/Migrator#pubctors) \| [Methods](https://developer.android.com/reference/android/app/appsearch/Migrator#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/app/appsearch/Migrator#inhmethods)

# Migrator

*** ** * ** ***

` public abstract class Migrator `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.app.appsearch.Migrator |

<br />

*** ** * ** ***

A migrator class to translate `https://developer.android.com/reference/android/app/appsearch/GenericDocument` from different version of `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema`

Make non-backwards-compatible changes will delete all stored documents in old schema. You can save your documents by setting `https://developer.android.com/reference/android/app/appsearch/Migrator` via the `https://developer.android.com/reference/android/app/appsearch/SetSchemaRequest.Builder#setMigrator(java.lang.String,%20android.app.appsearch.Migrator)` for each type and target version you want to save.

`https://developer.android.com/reference/android/app/appsearch/Migrator#onDowngrade(int,%20int,%20android.app.appsearch.GenericDocument)` or `https://developer.android.com/reference/android/app/appsearch/Migrator#onUpgrade(int,%20int,%20android.app.appsearch.GenericDocument)` will be triggered if the version number of the schema stored in AppSearch is different with the version in the request.

If any error or Exception occurred in the `https://developer.android.com/reference/android/app/appsearch/Migrator#onDowngrade(int,%20int,%20android.app.appsearch.GenericDocument)` or `https://developer.android.com/reference/android/app/appsearch/Migrator#onUpgrade(int,%20int,%20android.app.appsearch.GenericDocument)`, all the setSchema request will be rejected unless the schema changes are backwards-compatible, and stored documents won't have any observable changes.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/app/appsearch/Migrator#Migrator()() ` |

| ### Public methods ||
|---|---|
| ` abstract https://developer.android.com/reference/android/app/appsearch/GenericDocument` | ` https://developer.android.com/reference/android/app/appsearch/Migrator#onDowngrade(int,%20int,%20android.app.appsearch.GenericDocument)(int currentVersion, int finalVersion, https://developer.android.com/reference/android/app/appsearch/GenericDocument document) ` Migrates `https://developer.android.com/reference/android/app/appsearch/GenericDocument` to an older version of `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema`. |
| ` abstract https://developer.android.com/reference/android/app/appsearch/GenericDocument` | ` https://developer.android.com/reference/android/app/appsearch/Migrator#onUpgrade(int,%20int,%20android.app.appsearch.GenericDocument)(int currentVersion, int finalVersion, https://developer.android.com/reference/android/app/appsearch/GenericDocument document) ` Migrates `https://developer.android.com/reference/android/app/appsearch/GenericDocument` to a newer version of `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema`. |
| ` abstract boolean` | ` https://developer.android.com/reference/android/app/appsearch/Migrator#shouldMigrate(int,%20int)(int currentVersion, int finalVersion) ` Returns `true` if this migrator's source type needs to be migrated to update from currentVersion to finalVersion. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### Migrator

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Migrator ()
```

<br />

<br />

## Public methods

### onDowngrade

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract GenericDocument onDowngrade (int currentVersion, 
                int finalVersion, 
                GenericDocument document)
```

Migrates `https://developer.android.com/reference/android/app/appsearch/GenericDocument` to an older version of `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema`.

This method will be invoked only if the `https://developer.android.com/reference/android/app/appsearch/SetSchemaRequest` is setting a lower version number than the current `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema` saved in AppSearch.

If this `https://developer.android.com/reference/android/app/appsearch/Migrator` is provided to cover a compatible schema change via `https://developer.android.com/reference/android/app/appsearch/AppSearchSession#setSchema(android.app.appsearch.SetSchemaRequest,%20java.util.concurrent.Executor,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>)`, documents under the old version won't be removed unless you use the same document ID.

This method will be invoked on the background worker thread.   

This method may take several seconds to complete, so it should only be called from a worker thread.

<br />

| Parameters ||
|---|---|
| `currentVersion` | `int`: The current version of the document's schema. <br /> |
| `finalVersion` | `int`: The final version that documents need to be migrated to. <br /> |
| `document` | `GenericDocument`: The `https://developer.android.com/reference/android/app/appsearch/GenericDocument` need to be translated to new version. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/app/appsearch/GenericDocument` | A `https://developer.android.com/reference/android/app/appsearch/GenericDocument` in new version. This value cannot be `null`. <br /> |

### onUpgrade

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract GenericDocument onUpgrade (int currentVersion, 
                int finalVersion, 
                GenericDocument document)
```

Migrates `https://developer.android.com/reference/android/app/appsearch/GenericDocument` to a newer version of `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema`.

This method will be invoked only if the `https://developer.android.com/reference/android/app/appsearch/SetSchemaRequest` is setting a higher version number than the current `https://developer.android.com/reference/android/app/appsearch/AppSearchSchema` saved in AppSearch.

If this `https://developer.android.com/reference/android/app/appsearch/Migrator` is provided to cover a compatible schema change via `https://developer.android.com/reference/android/app/appsearch/AppSearchSession#setSchema(android.app.appsearch.SetSchemaRequest,%20java.util.concurrent.Executor,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>)`, documents under the old version won't be removed unless you use the same document ID.

This method will be invoked on the background worker thread provided via `https://developer.android.com/reference/android/app/appsearch/AppSearchSession#setSchema(android.app.appsearch.SetSchemaRequest,%20java.util.concurrent.Executor,%20java.util.concurrent.Executor,%20java.util.function.Consumer<android.app.appsearch.AppSearchResult<android.app.appsearch.SetSchemaResponse>>)`.   

This method may take several seconds to complete, so it should only be called from a worker thread.

<br />

| Parameters ||
|---|---|
| `currentVersion` | `int`: The current version of the document's schema. <br /> |
| `finalVersion` | `int`: The final version that documents need to be migrated to. <br /> |
| `document` | `GenericDocument`: The `https://developer.android.com/reference/android/app/appsearch/GenericDocument` need to be translated to new version. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/app/appsearch/GenericDocument` | A `https://developer.android.com/reference/android/app/appsearch/GenericDocument` in new version. This value cannot be `null`. <br /> |

### shouldMigrate

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract boolean shouldMigrate (int currentVersion, 
                int finalVersion)
```

Returns `true` if this migrator's source type needs to be migrated to update from currentVersion to finalVersion.

Migration won't be triggered if currentVersion is equal to finalVersion even if `https://developer.android.com/reference/android/app/appsearch/Migrator#shouldMigrate(int,%20int)` return true;

<br />

| Parameters ||
|---|---|
| `currentVersion` | `int` <br /> |
| `finalVersion` | `int` <br /> |

| Returns ||
|---|---|
| `boolean` | <br /> |