123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import Foundation
- import Realm
- import Realm.Private
- public typealias MigrationBlock = (_ migration: Migration, _ oldSchemaVersion: UInt64) -> Void
- public typealias MigrationObject = DynamicObject
- public typealias MigrationObjectEnumerateBlock = (_ oldObject: MigrationObject?, _ newObject: MigrationObject?) -> Void
- public func schemaVersionAtURL(_ fileURL: URL, encryptionKey: Data? = nil) throws -> UInt64 {
- var error: NSError?
- let version = RLMRealm.__schemaVersion(at: fileURL, encryptionKey: encryptionKey, error: &error)
- guard version != RLMNotVersioned else {
- throw error!
- }
- return version
- }
- extension Realm {
-
- public static func performMigration(for configuration: Realm.Configuration = Realm.Configuration.defaultConfiguration) throws {
- try RLMRealm.performMigration(for: configuration.rlmConfiguration)
- }
- }
- public struct Migration {
-
-
- public var oldSchema: Schema { return Schema(rlmMigration.oldSchema) }
-
- public var newSchema: Schema { return Schema(rlmMigration.newSchema) }
- internal var rlmMigration: RLMMigration
-
-
- public func enumerateObjects(ofType typeName: String, _ block: MigrationObjectEnumerateBlock) {
- rlmMigration.enumerateObjects(typeName) { oldObject, newObject in
- block(unsafeBitCast(oldObject, to: MigrationObject.self),
- unsafeBitCast(newObject, to: MigrationObject.self))
- }
- }
-
- @discardableResult
- public func create(_ typeName: String, value: Any = [:]) -> MigrationObject {
- return unsafeBitCast(rlmMigration.createObject(typeName, withValue: value), to: MigrationObject.self)
- }
-
- public func delete(_ object: MigrationObject) {
- rlmMigration.delete(object.unsafeCastToRLMObject())
- }
-
- @discardableResult
- public func deleteData(forType typeName: String) -> Bool {
- return rlmMigration.deleteData(forClassName: typeName)
- }
-
- public func renameProperty(onType typeName: String, from oldName: String, to newName: String) {
- rlmMigration.renameProperty(forClass: typeName, oldName: oldName, newName: newName)
- }
- internal init(_ rlmMigration: RLMMigration) {
- self.rlmMigration = rlmMigration
- }
- }
- internal func accessorMigrationBlock(_ migrationBlock: @escaping MigrationBlock) -> RLMMigrationBlock {
- return { migration, oldVersion in
-
- for objectSchema in migration.oldSchema.objectSchema {
- objectSchema.accessorClass = MigrationObject.self
-
-
-
- objectSchema.isSwiftClass = true
- }
- for objectSchema in migration.newSchema.objectSchema {
- objectSchema.accessorClass = MigrationObject.self
- }
-
- migrationBlock(Migration(migration), oldVersion)
- }
- }
|