Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheme version/migration configuration clarifications #6821

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Breaking Changes
* Removed all references and API's releated to permissions. These are now managed through MongoDB Realm. Read more [here](XXX).
* Removed Query Based Sync API's and Subscriptions. These API's are not initially supported by MongoDB Realm. They will be re-introduced in a future release. `SyncConfiguration.partionKey()` has been added as a replacement. Read more [here](XXX).
* Removed `RealmConfiguration.migration(RealmMigration)`. Specify migrations using `RealmConfigruation.schemeVersion(long, RealmMigration)` instead.

### Enhancements
* None.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void schemaDoesNotContainAllDefinedObjectShouldThrow() {
@Test
public void migration_nullThrows() {
try {
configFactory.createConfigurationBuilder().migration(null).build();
configFactory.createConfigurationBuilder().schemaVersion(1, null).build();
fail();
} catch (IllegalArgumentException ignored) {
}
Expand Down Expand Up @@ -332,8 +332,7 @@ public void standardSetup() {
.directory(configFactory.getRoot())
.name("foo.realm")
.encryptionKey(TestHelper.getRandomKey())
.schemaVersion(42)
.migration(new RealmMigration() {
.schemaVersion(42, new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// no-op
Expand Down Expand Up @@ -1028,8 +1027,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

@Test
public void detectMissingEqualsInCustomMigration() {
RealmConfiguration config1 = configFactory.createConfigurationBuilder().migration(new MigrationWithNoEquals()).build();
RealmConfiguration config2 = configFactory.createConfigurationBuilder().migration(new MigrationWithNoEquals()).build();
RealmConfiguration config1 = configFactory.createConfigurationBuilder().schemaVersion(1,new MigrationWithNoEquals()).build();
RealmConfiguration config2 = configFactory.createConfigurationBuilder().schemaVersion(1,new MigrationWithNoEquals()).build();

Realm realm = Realm.getInstance(config1);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

RealmConfiguration v2Config = configFactory.createConfigurationBuilder()
.name(MIGRATED_REALM)
.schemaVersion(2, migration)
.schema(StringOnly.class, FieldOrder.class)
.schemaVersion(2)
.migration(migration)
.build();
oldRealm = Realm.getInstance(v2Config);

Expand Down Expand Up @@ -202,9 +201,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
};

RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(StringOnly.class, AnnotationTypes.class)
.migration(migration)
.build();
try {
realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -239,9 +237,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

// Creates v1 of the Realm.
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(Thread.class, AnnotationTypes.class)
.migration(migration)
.build();
try {
realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -276,9 +273,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

// Creates v1 of the Realm.
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(Thread.class, StringOnly.class)
.migration(migration)
.build();
try {
realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -314,9 +310,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

// Creates v1 of the Realm.
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(Thread.class, PrimaryKeyAsString.class)
.migration(migration)
.build();
try {
realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -375,9 +370,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationClassRenamed.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);

Expand Down Expand Up @@ -411,9 +405,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationClassRenamed.class)
.migration(migration)
.build();
// Trigger migration
Realm realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -442,9 +435,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationClassRenamed.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);

Expand Down Expand Up @@ -479,9 +471,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationClassRenamed.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);
realm.close();
Expand Down Expand Up @@ -520,8 +511,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.migration(migration)
.schemaVersion(1, migration)
.build();

// Creating Realm instance fails.
Expand Down Expand Up @@ -553,9 +543,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationPosteriorIndexOnly.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);
Table table = realm.getSchema().getTable(MigrationPosteriorIndexOnly.class);
Expand All @@ -578,9 +567,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationPriorIndexOnly.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);
Table table = realm.getSchema().getTable(MigrationPriorIndexOnly.class);
Expand All @@ -602,9 +590,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationFieldRenamed.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);

Expand Down Expand Up @@ -663,9 +650,8 @@ public void apply(DynamicRealmObject obj) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationFieldTypeToInt.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);

Expand Down Expand Up @@ -707,9 +693,8 @@ public void apply(DynamicRealmObject obj) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(MigrationFieldTypeToInteger.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);

Expand Down Expand Up @@ -744,8 +729,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

RealmConfiguration configuration = configFactory.createConfigurationBuilder()
.schema(PrimaryKeyAsString.class)
.schemaVersion(1)
.migration(migration)
.schemaVersion(1, migration)
.build();

// Create the schema and set the int field as primary key
Expand Down Expand Up @@ -789,8 +773,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

RealmConfiguration configuration = configFactory.createConfigurationBuilder()
.schema(PrimaryKeyAsInteger.class)
.schemaVersion(1)
.migration(migration)
.schemaVersion(1, migration)
.build();

// Create the schema and set the String field as primary key
Expand Down Expand Up @@ -844,9 +827,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

// Creates v1 of the Realm.
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(StringOnly.class, AnnotationTypes.class)
.migration(migration)
.build();

realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -877,9 +859,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
};

RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(StringOnly.class, AnnotationTypes.class)
.migration(migration)
.build();

realm = Realm.getInstance(realmConfig);
Expand Down Expand Up @@ -944,9 +925,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

try {
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(0)
.schemaVersion(0, realmMigration)
.schema(StringOnly.class)
.migration(realmMigration)
.build();
Realm realm = Realm.getInstance(realmConfig);
realm.close();
Expand Down Expand Up @@ -975,9 +955,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
};

RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(StringOnly.class)
.migration(migration)
.build();
Realm realm = Realm.getInstance(realmConfig);
assertTrue(migrationCalled.get());
Expand Down Expand Up @@ -1069,10 +1048,9 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

@SuppressWarnings("unchecked")
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.name(field)
.schema(NullTypes.class)
.migration(migration)
.build();
Realm.deleteRealm(realmConfig);
// Prepares the version 0 db.
Expand Down Expand Up @@ -1136,10 +1114,9 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

@SuppressWarnings("unchecked")
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(1)
.schemaVersion(1, migration)
.name(field)
.schema(NullTypes.class)
.migration(migration)
.build();
Realm.deleteRealm(realmConfig);
// Prepares the version 0 db.
Expand Down Expand Up @@ -1177,9 +1154,8 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(SCHEMA_VERSION)
.schemaVersion(SCHEMA_VERSION, migration)
.schema(clazz)
.migration(migration)
.build();
Realm.deleteRealm(realmConfig);
configFactory.copyRealmFromAssets(context, "default-notnullable-primarykey.realm", Realm.DEFAULT_REALM_NAME);
Expand All @@ -1205,14 +1181,13 @@ public void notSettingNullableToPrimaryKeyThrows() throws IOException {
for (final Class clazz : classes) {
try {
RealmConfiguration realmConfig = configFactory.createConfigurationBuilder()
.schemaVersion(0)
.schema(clazz)
.migration(new RealmMigration() {
.schemaVersion(0, new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// Intentionally lefts empty to preserve not-nullablility of PrimaryKey on old schema.
}
})
.schema(clazz)
.build();
Realm realm = Realm.getInstance(realmConfig);
realm.close();
Expand Down Expand Up @@ -1269,7 +1244,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

@Test
public void migrateRealm_config_nonExistingRealmFile() throws FileNotFoundException {
RealmConfiguration config = configFactory.createConfigurationBuilder().migration(new RealmMigration() {
RealmConfiguration config = configFactory.createConfigurationBuilder().schemaVersion(1, new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

Expand Down Expand Up @@ -1307,8 +1282,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

RealmConfiguration config = configFactory.createConfigurationBuilder()
.schema(schemaClass)
.schemaVersion(2)
.migration(migration)
.schemaVersion(2, migration)
.assetFile("rename-and-add.realm")
.build();
Realm realm = Realm.getInstance(config);
Expand Down Expand Up @@ -1336,8 +1310,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {

RealmConfiguration config = configFactory.createConfigurationBuilder()
.schema(schemaClass)
.schemaVersion(2)
.migration(migration)
.schemaVersion(2, migration)
.assetFile("rename-and-add-indexed.realm")
.build();
realm = Realm.getInstance(config);
Expand Down Expand Up @@ -1406,8 +1379,7 @@ public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
}
};
RealmConfiguration config = configFactory.createConfigurationBuilder()
.migration(migration)
.schemaVersion(1)
.schemaVersion(1, migration)
.schema(StringOnly.class)
.build();
createEmptyRealmVersion0(config);
Expand Down
Loading