Search in sources :

Example 1 with OsSchemaInfo

use of io.realm.internal.OsSchemaInfo in project realm-java by realm.

the class BaseRealm method migrateRealm.

/**
 * Migrates the Realm file defined by the given configuration using the provided migration block.
 *
 * @param configuration configuration for the Realm that should be migrated. If this is a SyncConfiguration this
 * method does nothing.
 * @param migration if set, this migration block will override what is set in {@link RealmConfiguration}.
 * @throws FileNotFoundException if the Realm file doesn't exist.
 * @throws IllegalArgumentException if the provided configuration is a {@code SyncConfiguration}.
 */
protected static void migrateRealm(final RealmConfiguration configuration, @Nullable final RealmMigration migration) throws FileNotFoundException {
    // noinspection ConstantConditions
    if (configuration == null) {
        throw new IllegalArgumentException("RealmConfiguration must be provided");
    }
    if (configuration.isSyncConfiguration()) {
        throw new IllegalArgumentException("Manual migrations are not supported for synced Realms");
    }
    if (migration == null && configuration.getMigration() == null) {
        throw new RealmMigrationNeededException(configuration.getPath(), "RealmMigration must be provided.");
    }
    final AtomicBoolean fileNotFound = new AtomicBoolean(false);
    RealmCache.invokeWithGlobalRefCount(configuration, new RealmCache.Callback() {

        @Override
        public void onResult(int count) {
            if (count != 0) {
                throw new IllegalStateException("Cannot migrate a Realm file that is already open: " + configuration.getPath());
            }
            File realmFile = new File(configuration.getPath());
            if (!realmFile.exists()) {
                fileNotFound.set(true);
                return;
            }
            RealmProxyMediator mediator = configuration.getSchemaMediator();
            OsSchemaInfo schemaInfo = new OsSchemaInfo(mediator.getExpectedObjectSchemaInfoMap().values());
            OsSharedRealm.MigrationCallback migrationCallback = null;
            final RealmMigration migrationToBeApplied = migration != null ? migration : configuration.getMigration();
            if (migrationToBeApplied != null) {
                migrationCallback = createMigrationCallback(migrationToBeApplied);
            }
            OsRealmConfig.Builder configBuilder = new OsRealmConfig.Builder(configuration).autoUpdateNotification(false).schemaInfo(schemaInfo).migrationCallback(migrationCallback);
            OsSharedRealm sharedRealm = null;
            try {
                sharedRealm = OsSharedRealm.getInstance(configBuilder, OsSharedRealm.VersionID.LIVE);
            } finally {
                if (sharedRealm != null) {
                    sharedRealm.close();
                }
            }
        }
    });
    if (fileNotFound.get()) {
        throw new FileNotFoundException("Cannot migrate a Realm file which doesn't exist: " + configuration.getPath());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException) OsSchemaInfo(io.realm.internal.OsSchemaInfo) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RealmProxyMediator(io.realm.internal.RealmProxyMediator) File(java.io.File) OsSharedRealm(io.realm.internal.OsSharedRealm)

Aggregations

RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)1 OsSchemaInfo (io.realm.internal.OsSchemaInfo)1 OsSharedRealm (io.realm.internal.OsSharedRealm)1 RealmProxyMediator (io.realm.internal.RealmProxyMediator)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1