本文整理汇总了Java中io.realm.exceptions.RealmMigrationNeededException类的典型用法代码示例。如果您正苦于以下问题:Java RealmMigrationNeededException类的具体用法?Java RealmMigrationNeededException怎么用?Java RealmMigrationNeededException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RealmMigrationNeededException类属于io.realm.exceptions包,在下文中一共展示了RealmMigrationNeededException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: waitForInitialRemoteData_readOnlyTrue_throwsIfWrongServerSchema
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void waitForInitialRemoteData_readOnlyTrue_throwsIfWrongServerSchema() {
SyncCredentials credentials = SyncCredentials.usernamePassword(UUID.randomUUID().toString(), "password", true);
SyncUser user = SyncUser.login(credentials, Constants.AUTH_URL);
final SyncConfiguration configNew = configurationFactory.createSyncConfigurationBuilder(user, Constants.USER_REALM)
.waitForInitialRemoteData()
.readOnly()
.schema(StringOnly.class)
.build();
assertFalse(configNew.realmExists());
Realm realm = null;
try {
// This will fail, because the server Realm is completely empty and the Client is not allowed to write the
// schema.
realm = Realm.getInstance(configNew);
fail();
} catch (RealmMigrationNeededException ignored) {
} finally {
if (realm != null) {
realm.close();
}
user.logout();
}
}
示例2: Realm
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
/**
* The constructor is private to enforce the use of the static one.
*
* @param cache the {@link RealmCache} associated to this Realm instance.
* @throws IllegalArgumentException if trying to open an encrypted Realm with the wrong key.
*/
private Realm(RealmCache cache) {
super(cache, createExpectedSchemaInfo(cache.getConfiguration().getSchemaMediator()));
schema = new ImmutableRealmSchema(this,
new ColumnIndices(configuration.getSchemaMediator(), sharedRealm.getSchemaInfo()));
// FIXME: This is to work around the different behaviour between the read only Realms in the Object Store and
// in current java implementation. Opening a read only Realm with some missing schemas is allowed by Object
// Store and realm-cocoa. In that case, any query based on the missing schema should just return an empty
// results. Fix this together with https://github.com/realm/realm-java/issues/2953
if (configuration.isReadOnly()) {
RealmProxyMediator mediator = configuration.getSchemaMediator();
Set<Class<? extends RealmModel>> classes = mediator.getModelClasses();
for (Class<? extends RealmModel> clazz : classes) {
String tableName = Table.getTableNameForClass(mediator.getSimpleClassName(clazz));
if (!sharedRealm.hasTable(tableName)) {
sharedRealm.close();
throw new RealmMigrationNeededException(configuration.getPath(),
String.format(Locale.US, "Cannot open the read only Realm. '%s' is missing.",
Table.getClassNameForTable(tableName)));
}
}
}
}
示例3: getInstance_realmClosedAfterMigrationException
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void getInstance_realmClosedAfterMigrationException() throws IOException {
String REALM_NAME = "default0.realm";
RealmConfiguration realmConfig = configFactory.createConfiguration(REALM_NAME);
configFactory.copyRealmFromAssets(context, REALM_NAME, REALM_NAME);
try {
Realm.getInstance(realmConfig);
fail("A migration should be triggered");
} catch (RealmMigrationNeededException expected) {
Realm.deleteRealm(realmConfig); // Deletes old realm.
}
// This should recreate the Realm with proper schema.
Realm realm = Realm.getInstance(realmConfig);
int result = realm.where(AllTypes.class).equalTo("columnString", "Foo").findAll().size();
assertEquals(0, result);
realm.close();
}
示例4: migrationException_realmListChanged
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void migrationException_realmListChanged() throws IOException {
RealmConfiguration config = configFactory.createConfiguration();
// Initialize the schema with RealmList<Cat>
Realm.getInstance(configFactory.createConfiguration()).close();
DynamicRealm dynamicRealm = DynamicRealm.getInstance(config);
dynamicRealm.beginTransaction();
// Change the RealmList type to RealmList<Dog>
RealmObjectSchema dogSchema = dynamicRealm.getSchema().get(Dog.CLASS_NAME);
RealmObjectSchema ownerSchema = dynamicRealm.getSchema().get(CatOwner.CLASS_NAME);
ownerSchema.removeField(CatOwner.FIELD_CATS);
ownerSchema.addRealmListField(CatOwner.FIELD_CATS, dogSchema);
dynamicRealm.commitTransaction();
dynamicRealm.close();
try {
realm = Realm.getInstance(config);
fail();
} catch (RealmMigrationNeededException ignored) {
assertThat(ignored.getMessage(),
CoreMatchers.containsString("Property 'CatOwner.cats' has been changed from 'array<Dog>' to 'array<Cat>'"));
}
}
示例5: migrating_nullableField_toward_notNullable_PrimaryKeyThrows
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void migrating_nullableField_toward_notNullable_PrimaryKeyThrows() throws IOException {
configFactory.copyRealmFromAssets(context, "default-nullable-primarykey.realm", Realm.DEFAULT_REALM_NAME);
final Class[] classes = {PrimaryKeyAsByte.class, PrimaryKeyAsShort.class, PrimaryKeyAsInteger.class, PrimaryKeyAsLong.class};
for (final Class clazz : classes) {
try {
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schema(clazz)
.build();
Realm realm = Realm.getInstance(realmConfig);
realm.close();
fail();
} catch (RealmMigrationNeededException expected) {
assertThat(expected.getMessage(), CoreMatchers.containsString(
String.format("Property '%s.%s' has been made required", clazz.getSimpleName(), "id")));
}
}
}
示例6: migrationRequired_throwsOriginalException
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void migrationRequired_throwsOriginalException() {
RealmConfiguration config = configFactory.createConfigurationBuilder()
// .migration() No migration block provided, but one is required
.assetFile("default0.realm") // This Realm does not have the correct schema
.build();
Realm realm = null;
try {
realm = Realm.getInstance(config);
fail();
} catch (RealmMigrationNeededException ignored) {
} finally {
if (realm != null) {
realm.close();
}
}
}
示例7: getInstance_wrongSchemaInReadonlyThrows
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void getInstance_wrongSchemaInReadonlyThrows() {
RealmConfiguration config = configFactory.createConfigurationBuilder()
.name("readonly.realm")
.schema(StringOnlyReadOnly.class, AllJavaTypes.class)
.assetFile("readonly.realm")
.readOnly()
.build();
// This will throw because the Realm doesn't have the correct schema, and a new file cannot be re-created
// because it is read only.
try {
realm = Realm.getInstance(config);
fail();
} catch (RealmMigrationNeededException ignored) {
}
}
示例8: constructBuilder_versionEqualWhenSchemaChangesThrows
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void constructBuilder_versionEqualWhenSchemaChangesThrows() {
// Creates initial Realm.
RealmConfiguration config = new RealmConfiguration.Builder(context)
.directory(configFactory.getRoot())
.schemaVersion(42)
.schema(StringOnly.class)
.build();
Realm.getInstance(config).close();
// Creates new instance with a configuration containing another schema.
try {
config = new RealmConfiguration.Builder(context)
.directory(configFactory.getRoot())
.schemaVersion(42)
.schema(StringAndInt.class)
.build();
realm = Realm.getInstance(config);
fail("A migration should be required");
} catch (RealmMigrationNeededException ignored) {
}
}
示例9: setupMetadataRealm
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
private void setupMetadataRealm() {
final int METADATA_DB_SCHEMA_VERSION = 4;
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.modules(new BlogMetadataModule())
.schemaVersion(METADATA_DB_SCHEMA_VERSION)
.migration(new BlogMetadataDBMigration())
.build();
Realm.setDefaultConfiguration(config);
// open the Realm to check if a migration is needed
try {
Realm realm = Realm.getDefaultInstance();
realm.close();
} catch (RealmMigrationNeededException e) {
// delete existing Realm if we're below v4
if (mHACKOldSchemaVersion >= 0 && mHACKOldSchemaVersion < 4) {
Realm.deleteRealm(config);
mHACKOldSchemaVersion = -1;
}
}
AnalyticsService.logMetadataDbSchemaVersion(String.valueOf(METADATA_DB_SCHEMA_VERSION));
}
示例10: setupRealm
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
public static void setupRealm(Context context) {
// Setup
RealmConfiguration realmConfig = getRealmConfiguration(context);
Realm.setDefaultConfiguration(realmConfig);
try {
Realm realm = Realm.getDefaultInstance();
realm.close();
}
catch (RealmMigrationNeededException exception) {
Log.d(TAG, "New version! Destroy everything");
Realm.deleteRealm(realmConfig);
}
}
示例11: migrationException_getPath
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@Test
public void migrationException_getPath() throws IOException {
configFactory.copyRealmFromAssets(context, "default0.realm", Realm.DEFAULT_REALM_NAME);
File realm = new File(configFactory.getRoot(), Realm.DEFAULT_REALM_NAME);
try {
Realm.getInstance(configFactory.createConfiguration());
fail();
} catch (RealmMigrationNeededException expected) {
assertEquals(expected.getPath(), realm.getCanonicalPath());
}
}
示例12: migration_backlinkedFieldInUse
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
/**
* Table validation should fail if the backlinked column already exists in the target table.
* The realm `backlinks-fieldInUse.realm` contains the classes `BacklinksSource` and `BacklinksTarget`
* except that in the definition of {@code BacklinksTarget}, the field parent is defined as:
* <pre>
* {@code
* private RealmList<BacklinksSource> parents;
* }
* </pre>
*
* <p/>
* The backlinked field does exist but it is list of links to {@code BacklinksSource} children
* not a list of backlinks to {@code BacklinksSource} parents of which the {@code BacklinksTarget}
* is a child.
*/
@Test
public void migration_backlinkedFieldInUse() {
final String realmName = "backlinks-fieldInUse.realm";
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.name(realmName)
.schema(BacklinksSource.class, BacklinksTarget.class)
.build();
try {
configFactory.copyRealmFromAssets(context, realmName, realmName);
Realm localRealm = Realm.getInstance(realmConfig);
localRealm.close();
fail("A migration should have been required");
} catch (IOException e) {
fail("Failed copying realm");
} catch (RealmMigrationNeededException expected) {
assertThat(expected.getMessage(),
CoreMatchers.allOf(
CoreMatchers.containsString("Property 'BacklinksSource.name' has been added"),
CoreMatchers.containsString("Property 'BacklinksTarget.parents' has been removed")));
} finally {
Realm.deleteRealm(realmConfig);
}
}
示例13: migrate
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
@SuppressLint("DefaultLocale")
@Override
public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
if (oldVersion < newVersion) {
// Unknown migration
throw new RealmMigrationNeededException(realm.getPath(), String.format("Migration missing from v%d to v%d", oldVersion, newVersion));
}
}
示例14: RealmHelper
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
public RealmHelper(Application application) {
RealmConfiguration configuration = new RealmConfiguration.Builder(application).deleteRealmIfMigrationNeeded().build();
try {
Realm.setDefaultConfiguration(configuration);
realmDB = Realm.getDefaultInstance();
} catch (RealmMigrationNeededException e) {
Realm.deleteRealm(configuration);
realmDB = Realm.getDefaultInstance();
}
}
示例15: GreenHubDb
import io.realm.exceptions.RealmMigrationNeededException; //导入依赖的package包/类
public GreenHubDb() {
try {
mRealm = Realm.getDefaultInstance();
} catch (RealmMigrationNeededException e) {
// handle migration exception io.realm.exceptions.RealmMigrationNeededException
e.printStackTrace();
}
}