Search in sources :

Example 56 with Table

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

the class RealmMigrationTests method modifyPrimaryKeyFieldTypeToIntInMigration.

// This is to test how PK type can change to non-nullable int in migration.
@Test
public void modifyPrimaryKeyFieldTypeToIntInMigration() {
    final String TEMP_FIELD_ID = "temp_id";
    buildInitialMigrationSchema(MigrationFieldTypeToInt.CLASS_NAME, false);
    // create objects with the schema provided
    createObjectsWithOldPrimaryKey(MigrationFieldTypeToInt.CLASS_NAME, true);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationFieldTypeToInt.CLASS_NAME).addField("temp_id", int.class).transform(new RealmObjectSchema.Function() {

                @Override
                public void apply(DynamicRealmObject obj) {
                    String fieldValue = obj.getString(MigrationPrimaryKey.FIELD_PRIMARY);
                    if (fieldValue != null && fieldValue.length() != 0) {
                        obj.setInt(TEMP_FIELD_ID, Integer.valueOf(fieldValue).intValue());
                    } else {
                        // Since this cannot be accepted as proper pk value, we'll delete it.
                        // *You can modify with some other value such as 0, but that's not
                        // counted in this scenario.
                        obj.deleteFromRealm();
                    }
                }
            }).removeField(MigrationPrimaryKey.FIELD_PRIMARY).renameField(TEMP_FIELD_ID, MigrationFieldTypeToInt.FIELD_PRIMARY).addPrimaryKey(MigrationFieldTypeToInt.FIELD_PRIMARY);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationFieldTypeToInt.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationFieldTypeToInt.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationFieldTypeToInt.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationFieldTypeToInt.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldTypeToInt.class);
    assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY));
    assertEquals(MigrationFieldTypeToInt.FIELD_PRIMARY, objectSchema.getPrimaryKey());
    assertEquals(1, realm.where(MigrationFieldTypeToInt.class).count());
    assertEquals(12, realm.where(MigrationFieldTypeToInt.class).findFirst().fieldIntPrimary);
}
Also used : Table(io.realm.internal.Table) MigrationFieldTypeToInt(io.realm.entities.migration.MigrationFieldTypeToInt) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 57 with Table

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

the class RealmMigrationTests method modifyPrimaryKeyFieldTypeToIntegerInMigration.

// This is to test how PK type can change to nullable Integer in migration.
@Test
public void modifyPrimaryKeyFieldTypeToIntegerInMigration() {
    final String TEMP_FIELD_ID = "temp_id";
    buildInitialMigrationSchema(MigrationFieldTypeToInteger.CLASS_NAME, false);
    // Creates objects with the schema provided.
    createObjectsWithOldPrimaryKey(MigrationFieldTypeToInteger.CLASS_NAME, true);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationFieldTypeToInteger.CLASS_NAME).addField("temp_id", Integer.class).transform(new RealmObjectSchema.Function() {

                @Override
                public void apply(DynamicRealmObject obj) {
                    String fieldValue = obj.getString(MigrationPrimaryKey.FIELD_PRIMARY);
                    if (fieldValue != null && fieldValue.length() != 0) {
                        obj.setInt(TEMP_FIELD_ID, Integer.valueOf(fieldValue));
                    } else {
                        obj.setNull(TEMP_FIELD_ID);
                    }
                }
            }).removeField(MigrationPrimaryKey.FIELD_PRIMARY).renameField(TEMP_FIELD_ID, MigrationFieldTypeToInteger.FIELD_PRIMARY).addPrimaryKey(MigrationFieldTypeToInteger.FIELD_PRIMARY);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationFieldTypeToInteger.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationFieldTypeToInteger.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationFieldTypeToInteger.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationFieldTypeToInteger.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldTypeToInteger.class);
    assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY));
    assertEquals(MigrationFieldTypeToInteger.FIELD_PRIMARY, objectSchema.getPrimaryKey());
    assertEquals(2, realm.where(MigrationFieldTypeToInteger.class).count());
    // not-null value
    assertEquals(1, realm.where(MigrationFieldTypeToInteger.class).equalTo(MigrationFieldTypeToInteger.FIELD_PRIMARY, Integer.valueOf(12)).count());
    // null value
    assertEquals(1, realm.where(MigrationFieldTypeToInteger.class).equalTo(MigrationFieldTypeToInteger.FIELD_PRIMARY, (Integer) null).count());
}
Also used : Table(io.realm.internal.Table) MigrationFieldTypeToInteger(io.realm.entities.migration.MigrationFieldTypeToInteger) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 58 with Table

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

the class RealmMigrationTests method setClassName_transferPrimaryKey.

// Test to show that renaming a class does not effect the primary key.
@Test
public void setClassName_transferPrimaryKey() {
    buildInitialMigrationSchema(MigrationClassRenamed.CLASS_NAME, true);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationPrimaryKey.CLASS_NAME).setClassName(MigrationClassRenamed.CLASS_NAME);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationClassRenamed.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationClassRenamed.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationClassRenamed.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationClassRenamed.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    assertEquals(MigrationClassRenamed.FIELD_PRIMARY, table.getColumnName(table.getPrimaryKey()));
    // Old schema does not exist.
    assertNull(realm.getSchema().get(MigrationPrimaryKey.CLASS_NAME));
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Example 59 with Table

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

the class RealmObjectTests method prepareColumnSwappedRealm.

private RealmConfiguration prepareColumnSwappedRealm() throws FileNotFoundException {
    final RealmConfiguration columnSwappedRealmConfigForV0 = configFactory.createConfigurationBuilder().name("columnSwapped.realm").migration(new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            final Table table = realm.schema.getTable(StringAndInt.class);
            final long strIndex = table.getColumnIndex("str");
            final long numberIndex = table.getColumnIndex("number");
            while (0 < table.getColumnCount()) {
                table.removeColumn(0);
            }
            final long newStrIndex;
            // Swaps column indices.
            if (strIndex < numberIndex) {
                table.addColumn(RealmFieldType.INTEGER, "number");
                newStrIndex = table.addColumn(RealmFieldType.STRING, "str");
            } else {
                newStrIndex = table.addColumn(RealmFieldType.STRING, "str");
                table.addColumn(RealmFieldType.INTEGER, "number");
            }
            table.convertColumnToNullable(newStrIndex);
        }
    }).build();
    final RealmConfiguration columnSwappedRealmConfigForV1 = configFactory.createConfigurationBuilder().name("columnSwapped.realm").migration(new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        // Does nothing.
        }
    }).schemaVersion(1L).build();
    Realm.deleteRealm(columnSwappedRealmConfigForV0);
    Realm.getInstance(columnSwappedRealmConfigForV0).close();
    Realm.migrateRealm(columnSwappedRealmConfigForV0);
    return columnSwappedRealmConfigForV1;
}
Also used : Table(io.realm.internal.Table)

Example 60 with Table

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

the class RealmMigrationTests method migratePreNull.

// Pre-null Realms will leave columns not-nullable after the underlying storage engine has
// migrated the file format. An explicit migration step to convert to nullable, and the
// old class (without @Required) can be used,
@Test
public void migratePreNull() throws IOException {
    configFactory.copyRealmFromAssets(context, "default-before-migration.realm", Realm.DEFAULT_REALM_NAME);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            Table table = realm.schema.getTable(StringOnly.class);
            table.convertColumnToNullable(table.getColumnIndex("chars"));
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(StringOnly.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    realm.beginTransaction();
    StringOnly stringOnly = realm.createObject(StringOnly.class);
    stringOnly.setChars(null);
    realm.commitTransaction();
    realm.close();
}
Also used : StringOnly(io.realm.entities.StringOnly) Table(io.realm.internal.Table) Test(org.junit.Test)

Aggregations

Table (io.realm.internal.Table)68 RealmObjectProxy (io.realm.internal.RealmObjectProxy)20 Test (org.junit.Test)19 Date (java.util.Date)10 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)5 RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)4 HashMap (java.util.HashMap)4 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)3 RealmException (io.realm.exceptions.RealmException)3 UncheckedRow (io.realm.internal.UncheckedRow)2 JSONObject (org.json.JSONObject)2 TargetApi (android.annotation.TargetApi)1 JsonReader (android.util.JsonReader)1 AllTypes (io.realm.entities.AllTypes)1 Cat (io.realm.entities.Cat)1 IOSAllTypes (io.realm.entities.IOSAllTypes)1 StringOnly (io.realm.entities.StringOnly)1 MigrationFieldTypeToInt (io.realm.entities.migration.MigrationFieldTypeToInt)1 MigrationFieldTypeToInteger (io.realm.entities.migration.MigrationFieldTypeToInteger)1 LinkView (io.realm.internal.LinkView)1