Added in API level 31

Migrator


abstract class Migrator
kotlin.Any
   ↳ android.app.appsearch.Migrator

A migrator class to translate GenericDocument from different version of AppSearchSchema

Make non-backwards-compatible changes will delete all stored documents in old schema. You can save your documents by setting Migrator via the android.app.appsearch.SetSchemaRequest.Builder#setMigrator for each type and target version you want to save.

onDowngrade or onUpgrade 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 onDowngrade or onUpgrade, 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

Public methods
abstract GenericDocument
onDowngrade(currentVersion: Int, finalVersion: Int, document: GenericDocument)

Migrates GenericDocument to an older version of AppSearchSchema.

abstract GenericDocument
onUpgrade(currentVersion: Int, finalVersion: Int, document: GenericDocument)

Migrates GenericDocument to a newer version of AppSearchSchema.

abstract Boolean
shouldMigrate(currentVersion: Int, finalVersion: Int)

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

Public constructors

Migrator

Added in API level 31
Migrator()

Public methods

onDowngrade

Added in API level 31
abstract fun onDowngrade(
    currentVersion: Int,
    finalVersion: Int,
    document: GenericDocument
): GenericDocument

Migrates GenericDocument to an older version of AppSearchSchema.

This method will be invoked only if the SetSchemaRequest is setting a lower version number than the current AppSearchSchema saved in AppSearch.

If this Migrator is provided to cover a compatible schema change via android.app.appsearch.AppSearchSession#setSchema, 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.

Parameters
currentVersion Int: The current version of the document's schema.
finalVersion Int: The final version that documents need to be migrated to.
document GenericDocument: The GenericDocument need to be translated to new version. This value cannot be null.
Return
GenericDocument A GenericDocument in new version. This value cannot be null.

onUpgrade

Added in API level 31
abstract fun onUpgrade(
    currentVersion: Int,
    finalVersion: Int,
    document: GenericDocument
): GenericDocument

Migrates GenericDocument to a newer version of AppSearchSchema.

This method will be invoked only if the SetSchemaRequest is setting a higher version number than the current AppSearchSchema saved in AppSearch.

If this Migrator is provided to cover a compatible schema change via android.app.appsearch.AppSearchSession#setSchema, 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 android.app.appsearch.AppSearchSession#setSchema.
This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
currentVersion Int: The current version of the document's schema.
finalVersion Int: The final version that documents need to be migrated to.
document GenericDocument: The GenericDocument need to be translated to new version. This value cannot be null.
Return
GenericDocument A GenericDocument in new version. This value cannot be null.

shouldMigrate

Added in API level 31
abstract fun shouldMigrate(
    currentVersion: Int,
    finalVersion: Int
): Boolean

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 shouldMigrate return true;