use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.
the class RealmMigrationTests method settingRequiredForNullableThrows.
// If a field is not required but was not nullable before, a RealmMigrationNeededException should be thrown.
@Test
public void settingRequiredForNullableThrows() {
String[] notNullableFields = { NullTypes.FIELD_STRING_NULL, NullTypes.FIELD_BYTES_NULL, NullTypes.FIELD_BOOLEAN_NULL, NullTypes.FIELD_BYTE_NULL, NullTypes.FIELD_SHORT_NULL, NullTypes.FIELD_INTEGER_NULL, NullTypes.FIELD_LONG_NULL, NullTypes.FIELD_FLOAT_NULL, NullTypes.FIELD_DOUBLE_NULL, NullTypes.FIELD_DATE_NULL };
for (final String field : notNullableFields) {
final RealmMigration migration = new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
if (oldVersion == 0) {
// 0 after initNullTypesTableExcludes
// No @Required for not nullable field
RealmObjectSchema nullTypesSchema = realm.getSchema().getSchemaForClass(NullTypes.CLASS_NAME);
if (field.equals(NullTypes.FIELD_STRING_NULL)) {
// 1 String
nullTypesSchema.addField(field, String.class, FieldAttribute.REQUIRED);
} else if (field.equals(NullTypes.FIELD_BYTES_NULL)) {
// 2 Bytes
nullTypesSchema.addField(field, byte[].class, FieldAttribute.REQUIRED);
} else if (field.equals(NullTypes.FIELD_BOOLEAN_NULL)) {
// 3 Boolean
nullTypesSchema.addField(field, boolean.class);
} else if (field.equals(NullTypes.FIELD_BYTE_NULL) || field.equals(NullTypes.FIELD_SHORT_NULL) || field.equals(NullTypes.FIELD_INTEGER_NULL) || field.equals(NullTypes.FIELD_LONG_NULL)) {
// 4 Byte 5 Short 6 Integer 7 Long
nullTypesSchema.addField(field, int.class);
} else if (field.equals(NullTypes.FIELD_FLOAT_NULL)) {
// 8 Float
nullTypesSchema.addField(field, float.class);
} else if (field.equals(NullTypes.FIELD_DOUBLE_NULL)) {
// 9 Double
nullTypesSchema.addField(field, double.class);
} else if (field.equals(NullTypes.FIELD_DATE_NULL)) {
// 10 Date
nullTypesSchema.addField(field, Date.class, FieldAttribute.REQUIRED);
}
// 11 Object skipped
}
}
};
@SuppressWarnings("unchecked") RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(NullTypes.class).migration(migration).build();
Realm.deleteRealm(realmConfig);
// Prepares the version 0 db.
DynamicRealm dynamicRealm = DynamicRealm.getInstance(realmConfig);
TestHelper.initNullTypesTableExcludes(dynamicRealm, field);
dynamicRealm.close();
try {
realm = Realm.getInstance(realmConfig);
fail("Failed on " + field);
} catch (RealmMigrationNeededException e) {
if (field.equals(NullTypes.FIELD_STRING_NULL) || field.equals(NullTypes.FIELD_BYTES_NULL) || field.equals(NullTypes.FIELD_DATE_NULL)) {
assertEquals("Field '" + field + "' is required. Either set @Required to field '" + field + "' " + "or migrate using RealmObjectSchema.setNullable().", e.getMessage());
} else {
assertEquals("Field '" + field + "' does not support null values in the existing Realm file." + " Either set @Required, use the primitive type for field '" + field + "' or migrate using RealmObjectSchema.setNullable().", e.getMessage());
}
}
}
}
use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.
the class RealmMigrationTests method getInstance_realmClosedAfterMigrationException.
@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) {
// Deletes old realm.
Realm.deleteRealm(realmConfig);
}
// 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();
}
use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.
the class NullTypesRealmProxy method validateTable.
public static NullTypesColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) {
if (!sharedRealm.hasTable("class_NullTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'NullTypes' class is missing from the schema for this Realm.");
}
Table table = sharedRealm.getTable("class_NullTypes");
final long columnCount = table.getColumnCount();
if (columnCount != 21) {
if (columnCount < 21) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 21 but was " + columnCount);
}
if (allowExtraColumns) {
RealmLog.debug("Field count is more than expected - expected 21 but was %1$d", columnCount);
} else {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 21 but was " + columnCount);
}
}
Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>();
for (long i = 0; i < columnCount; i++) {
columnTypes.put(table.getColumnName(i), table.getColumnType(i));
}
final NullTypesColumnInfo columnInfo = new NullTypesColumnInfo(sharedRealm.getPath(), table);
if (table.hasPrimaryKey()) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary Key defined for field " + table.getColumnName(table.getPrimaryKey()) + " was removed.");
}
if (!columnTypes.containsKey("fieldStringNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldStringNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldStringNotNull") != RealmFieldType.STRING) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'String' for field 'fieldStringNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldStringNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldStringNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldStringNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldStringNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldStringNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldStringNull") != RealmFieldType.STRING) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'String' for field 'fieldStringNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldStringNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldStringNull' is required. Either set @Required to field 'fieldStringNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldBooleanNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldBooleanNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldBooleanNotNull") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Boolean' for field 'fieldBooleanNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldBooleanNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldBooleanNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldBooleanNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldBooleanNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldBooleanNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldBooleanNull") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Boolean' for field 'fieldBooleanNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldBooleanNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldBooleanNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldBooleanNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldBytesNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldBytesNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldBytesNotNull") != RealmFieldType.BINARY) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'byte[]' for field 'fieldBytesNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldBytesNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldBytesNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldBytesNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldBytesNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldBytesNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldBytesNull") != RealmFieldType.BINARY) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'byte[]' for field 'fieldBytesNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldBytesNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldBytesNull' is required. Either set @Required to field 'fieldBytesNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldByteNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldByteNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldByteNotNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Byte' for field 'fieldByteNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldByteNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldByteNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldByteNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldByteNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldByteNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldByteNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Byte' for field 'fieldByteNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldByteNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldByteNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldByteNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldShortNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldShortNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldShortNotNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Short' for field 'fieldShortNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldShortNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldShortNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldShortNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldShortNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldShortNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldShortNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Short' for field 'fieldShortNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldShortNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldShortNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldShortNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldIntegerNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldIntegerNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldIntegerNotNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Integer' for field 'fieldIntegerNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldIntegerNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldIntegerNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldIntegerNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldIntegerNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldIntegerNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldIntegerNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Integer' for field 'fieldIntegerNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldIntegerNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldIntegerNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldIntegerNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldLongNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldLongNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldLongNotNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Long' for field 'fieldLongNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldLongNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldLongNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldLongNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldLongNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldLongNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldLongNull") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Long' for field 'fieldLongNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldLongNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldLongNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldLongNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldFloatNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldFloatNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldFloatNotNull") != RealmFieldType.FLOAT) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Float' for field 'fieldFloatNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldFloatNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldFloatNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldFloatNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldFloatNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldFloatNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldFloatNull") != RealmFieldType.FLOAT) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Float' for field 'fieldFloatNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldFloatNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldFloatNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldFloatNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldDoubleNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldDoubleNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldDoubleNotNull") != RealmFieldType.DOUBLE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Double' for field 'fieldDoubleNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldDoubleNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldDoubleNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldDoubleNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldDoubleNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldDoubleNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldDoubleNull") != RealmFieldType.DOUBLE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Double' for field 'fieldDoubleNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldDoubleNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldDoubleNull' does not support null values in the existing Realm file. Either set @Required, use the primitive type for field 'fieldDoubleNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldDateNotNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldDateNotNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldDateNotNull") != RealmFieldType.DATE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Date' for field 'fieldDateNotNull' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.fieldDateNotNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldDateNotNull' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'fieldDateNotNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldDateNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldDateNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldDateNull") != RealmFieldType.DATE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Date' for field 'fieldDateNull' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.fieldDateNullIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'fieldDateNull' is required. Either set @Required to field 'fieldDateNull' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("fieldObjectNull")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'fieldObjectNull' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("fieldObjectNull") != RealmFieldType.OBJECT) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'NullTypes' for field 'fieldObjectNull'");
}
if (!sharedRealm.hasTable("class_NullTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing class 'class_NullTypes' for field 'fieldObjectNull'");
}
Table table_20 = sharedRealm.getTable("class_NullTypes");
if (!table.getLinkTarget(columnInfo.fieldObjectNullIndex).hasSameSchema(table_20)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid RealmObject for field 'fieldObjectNull': '" + table.getLinkTarget(columnInfo.fieldObjectNullIndex).getName() + "' expected - was '" + table_20.getName() + "'");
}
return columnInfo;
}
use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.
the class BooleansRealmProxy method validateTable.
public static BooleansColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) {
if (!sharedRealm.hasTable("class_Booleans")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'Booleans' class is missing from the schema for this Realm.");
}
Table table = sharedRealm.getTable("class_Booleans");
final long columnCount = table.getColumnCount();
if (columnCount != 4) {
if (columnCount < 4) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 4 but was " + columnCount);
}
if (allowExtraColumns) {
RealmLog.debug("Field count is more than expected - expected 4 but was %1$d", columnCount);
} else {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 4 but was " + columnCount);
}
}
Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>();
for (long i = 0; i < columnCount; i++) {
columnTypes.put(table.getColumnName(i), table.getColumnType(i));
}
final BooleansColumnInfo columnInfo = new BooleansColumnInfo(sharedRealm.getPath(), table);
if (table.hasPrimaryKey()) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary Key defined for field " + table.getColumnName(table.getPrimaryKey()) + " was removed.");
}
if (!columnTypes.containsKey("done")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'done' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("done") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'boolean' for field 'done' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.doneIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'done' does support null values in the existing Realm file. Use corresponding boxed type for field 'done' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("isReady")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'isReady' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("isReady") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'boolean' for field 'isReady' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.isReadyIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'isReady' does support null values in the existing Realm file. Use corresponding boxed type for field 'isReady' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("mCompleted")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'mCompleted' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("mCompleted") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'boolean' for field 'mCompleted' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.mCompletedIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'mCompleted' does support null values in the existing Realm file. Use corresponding boxed type for field 'mCompleted' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("anotherBoolean")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'anotherBoolean' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("anotherBoolean") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'boolean' for field 'anotherBoolean' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.anotherBooleanIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'anotherBoolean' does support null values in the existing Realm file. Use corresponding boxed type for field 'anotherBoolean' or migrate using RealmObjectSchema.setNullable().");
}
return columnInfo;
}
use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.
the class AllTypesRealmProxy method validateTable.
public static AllTypesColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) {
if (!sharedRealm.hasTable("class_AllTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'AllTypes' class is missing from the schema for this Realm.");
}
Table table = sharedRealm.getTable("class_AllTypes");
final long columnCount = table.getColumnCount();
if (columnCount != 9) {
if (columnCount < 9) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 9 but was " + columnCount);
}
if (allowExtraColumns) {
RealmLog.debug("Field count is more than expected - expected 9 but was %1$d", columnCount);
} else {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 9 but was " + columnCount);
}
}
Map<String, RealmFieldType> columnTypes = new HashMap<String, RealmFieldType>();
for (long i = 0; i < columnCount; i++) {
columnTypes.put(table.getColumnName(i), table.getColumnType(i));
}
final AllTypesColumnInfo columnInfo = new AllTypesColumnInfo(sharedRealm.getPath(), table);
if (!table.hasPrimaryKey()) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary key not defined for field 'columnString' in existing Realm file. @PrimaryKey was added.");
} else {
if (table.getPrimaryKey() != columnInfo.columnStringIndex) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Primary Key annotation definition was changed, from field " + table.getColumnName(table.getPrimaryKey()) + " to field columnString");
}
}
if (!columnTypes.containsKey("columnString")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnString' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnString") != RealmFieldType.STRING) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'String' for field 'columnString' in existing Realm file.");
}
if (!table.isColumnNullable(columnInfo.columnStringIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "@PrimaryKey field 'columnString' does not support null values in the existing Realm file. Migrate using RealmObjectSchema.setNullable(), or mark the field as @Required.");
}
if (!table.hasSearchIndex(table.getColumnIndex("columnString"))) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Index not defined for field 'columnString' in existing Realm file. Either set @Index or migrate using io.realm.internal.Table.removeSearchIndex().");
}
if (!columnTypes.containsKey("columnLong")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnLong' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnLong") != RealmFieldType.INTEGER) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'long' for field 'columnLong' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnLongIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnLong' does support null values in the existing Realm file. Use corresponding boxed type for field 'columnLong' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnFloat")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnFloat' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnFloat") != RealmFieldType.FLOAT) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'float' for field 'columnFloat' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnFloatIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnFloat' does support null values in the existing Realm file. Use corresponding boxed type for field 'columnFloat' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnDouble")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnDouble' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnDouble") != RealmFieldType.DOUBLE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'double' for field 'columnDouble' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnDoubleIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnDouble' does support null values in the existing Realm file. Use corresponding boxed type for field 'columnDouble' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnBoolean")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnBoolean' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnBoolean") != RealmFieldType.BOOLEAN) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'boolean' for field 'columnBoolean' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnBooleanIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnBoolean' does support null values in the existing Realm file. Use corresponding boxed type for field 'columnBoolean' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnDate")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnDate' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnDate") != RealmFieldType.DATE) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'Date' for field 'columnDate' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnDateIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnDate' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'columnDate' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnBinary")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnBinary' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnBinary") != RealmFieldType.BINARY) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'byte[]' for field 'columnBinary' in existing Realm file.");
}
if (table.isColumnNullable(columnInfo.columnBinaryIndex)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'columnBinary' does support null values in the existing Realm file. Remove @Required or @PrimaryKey from field 'columnBinary' or migrate using RealmObjectSchema.setNullable().");
}
if (!columnTypes.containsKey("columnObject")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnObject' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
}
if (columnTypes.get("columnObject") != RealmFieldType.OBJECT) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'AllTypes' for field 'columnObject'");
}
if (!sharedRealm.hasTable("class_AllTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing class 'class_AllTypes' for field 'columnObject'");
}
Table table_7 = sharedRealm.getTable("class_AllTypes");
if (!table.getLinkTarget(columnInfo.columnObjectIndex).hasSameSchema(table_7)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid RealmObject for field 'columnObject': '" + table.getLinkTarget(columnInfo.columnObjectIndex).getName() + "' expected - was '" + table_7.getName() + "'");
}
if (!columnTypes.containsKey("columnRealmList")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'columnRealmList'");
}
if (columnTypes.get("columnRealmList") != RealmFieldType.LIST) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'AllTypes' for field 'columnRealmList'");
}
if (!sharedRealm.hasTable("class_AllTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing class 'class_AllTypes' for field 'columnRealmList'");
}
Table table_8 = sharedRealm.getTable("class_AllTypes");
if (!table.getLinkTarget(columnInfo.columnRealmListIndex).hasSameSchema(table_8)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid RealmList type for field 'columnRealmList': '" + table.getLinkTarget(columnInfo.columnRealmListIndex).getName() + "' expected - was '" + table_8.getName() + "'");
}
long backlinkFieldIndex;
Table backlinkSourceTable;
Table backlinkTargetTable;
RealmFieldType backlinkFieldType;
if (!sharedRealm.hasTable("class_AllTypes")) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Cannot find source class 'some.test.AllTypes' for @LinkingObjects field 'some.test.AllTypes.parentObjects'");
}
backlinkSourceTable = sharedRealm.getTable("class_AllTypes");
backlinkFieldIndex = backlinkSourceTable.getColumnIndex("columnObject");
if (backlinkFieldIndex == Table.NO_MATCH) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Cannot find source field 'some.test.AllTypes.columnObject' for @LinkingObjects field 'some.test.AllTypes.parentObjects'");
}
backlinkFieldType = backlinkSourceTable.getColumnType(backlinkFieldIndex);
if ((backlinkFieldType != RealmFieldType.OBJECT) && (backlinkFieldType != RealmFieldType.LIST)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Source field 'some.test.AllTypes.columnObject' for @LinkingObjects field 'some.test.AllTypes.parentObjects' is not a RealmObject type");
}
backlinkTargetTable = backlinkSourceTable.getLinkTarget(backlinkFieldIndex);
if (!table.hasSameSchema(backlinkTargetTable)) {
throw new RealmMigrationNeededException(sharedRealm.getPath(), "Source field 'some.test.AllTypes.columnObject' for @LinkingObjects field 'some.test.AllTypes.parentObjects' has wrong type '" + backlinkTargetTable.getName() + "'");
}
return columnInfo;
}
Aggregations