Search in sources :

Example 11 with RealmMigrationNeededException

use of io.realm.exceptions.RealmMigrationNeededException in project realm-java by realm.

the class SimpleRealmProxy method validateTable.

public static SimpleColumnInfo validateTable(SharedRealm sharedRealm, boolean allowExtraColumns) {
    if (!sharedRealm.hasTable("class_Simple")) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "The 'Simple' class is missing from the schema for this Realm.");
    }
    Table table = sharedRealm.getTable("class_Simple");
    final long columnCount = table.getColumnCount();
    if (columnCount != 2) {
        if (columnCount < 2) {
            throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is less than expected - expected 2 but was " + columnCount);
        }
        if (allowExtraColumns) {
            RealmLog.debug("Field count is more than expected - expected 2 but was %1$d", columnCount);
        } else {
            throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field count is more than expected - expected 2 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 SimpleColumnInfo columnInfo = new SimpleColumnInfo(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("name")) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'name' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
    }
    if (columnTypes.get("name") != RealmFieldType.STRING) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'String' for field 'name' in existing Realm file.");
    }
    if (!table.isColumnNullable(columnInfo.nameIndex)) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'name' is required. Either set @Required to field 'name' or migrate using RealmObjectSchema.setNullable().");
    }
    if (!columnTypes.containsKey("age")) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Missing field 'age' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().");
    }
    if (columnTypes.get("age") != RealmFieldType.INTEGER) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Invalid type 'int' for field 'age' in existing Realm file.");
    }
    if (table.isColumnNullable(columnInfo.ageIndex)) {
        throw new RealmMigrationNeededException(sharedRealm.getPath(), "Field 'age' does support null values in the existing Realm file. Use corresponding boxed type for field 'age' or migrate using RealmObjectSchema.setNullable().");
    }
    return columnInfo;
}
Also used : Table(io.realm.internal.Table) HashMap(java.util.HashMap) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException)

Aggregations

RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)11 Table (io.realm.internal.Table)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)3 ColumnIndices (io.realm.internal.ColumnIndices)2 File (java.io.File)2 AllTypes (io.realm.entities.AllTypes)1 PrimaryKeyAsBoxedInteger (io.realm.entities.PrimaryKeyAsBoxedInteger)1 PrimaryKeyAsInteger (io.realm.entities.PrimaryKeyAsInteger)1 MigrationFieldTypeToInteger (io.realm.entities.migration.MigrationFieldTypeToInteger)1 ColumnInfo (io.realm.internal.ColumnInfo)1 RealmProxyMediator (io.realm.internal.RealmProxyMediator)1 SharedRealm (io.realm.internal.SharedRealm)1 FileNotFoundException (java.io.FileNotFoundException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1