Migration

@frozen
public struct Migration

Migration instances encapsulate information intended to facilitate a schema migration.

A Migration instance is passed into a user-defined MigrationBlock block when updating the version of a Realm. This instance provides access to the old and new database schemas, the objects in the Realm, and provides functionality for modifying the Realm during the migration.

Properties

  • The old schema, describing the Realm before applying a migration.

    Declaration

    Swift

    public var oldSchema: Schema { get }
  • The new schema, describing the Realm after applying a migration.

    Declaration

    Swift

    public var newSchema: Schema { get }

Altering Objects During a Migration

  • Enumerates all the objects of a given type in this Realm, providing both the old and new versions of each object. Properties on an object can be accessed using subscripting.

    Declaration

    Swift

    public func enumerateObjects(ofType typeName: String, _ block: MigrationObjectEnumerateBlock)

    Parameters

    objectClassName

    The name of the Object class to enumerate.

    block

    The block providing both the old and new versions of an object in this Realm.

  • Creates and returns an Object of type className in the Realm being migrated.

    The value argument is used to populate the object. It can be a key-value coding compliant object, an array or dictionary returned from the methods in NSJSONSerialization, or an Array containing one element for each managed property. An exception will be thrown if any required properties are not present and those properties were not defined with default values.

    When passing in an Array as the value argument, all properties must be present, valid and in the same order as the properties defined in the model.

    Declaration

    Swift

    @discardableResult
    public func create(_ typeName: String, value: Any = [:]) -> MigrationObject

    Parameters

    className

    The name of the Object class to create.

    value

    The value used to populate the created object.

    Return Value

    The newly created object.

  • Deletes an object from a Realm during a migration.

    It is permitted to call this method from within the block passed to enumerate(_:block:).

    Declaration

    Swift

    public func delete(_ object: MigrationObject)

    Parameters

    object

    An object to be deleted from the Realm being migrated.

  • Deletes the data for the class with the given name.

    All objects of the given class will be deleted. If the Object subclass no longer exists in your program, any remaining metadata for the class will be removed from the Realm file.

    Declaration

    Swift

    @discardableResult
    public func deleteData(forType typeName: String) -> Bool

    Parameters

    objectClassName

    The name of the Object class to delete.

    Return Value

    A Boolean value indicating whether there was any data to delete.

  • Renames a property of the given class from oldName to newName.

    Declaration

    Swift

    public func renameProperty(onType typeName: String, from oldName: String, to newName: String)

    Parameters

    className

    The name of the class whose property should be renamed. This class must be present in both the old and new Realm schemas.

    oldName

    The old column name for the property to be renamed. There must not be a property with this name in the class as defined by the new Realm schema.

    newName

    The new column name for the property to be renamed. There must not be a property with this name in the class as defined by the old Realm schema.