Search in sources :

Example 6 with Table

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

the class RealmAnnotationTests method primaryKey_migration_string.

// Tests migrating primary key from long to str with existing data.
@Test
public void primaryKey_migration_string() {
    realm.beginTransaction();
    for (int i = 1; i <= 2; i++) {
        PrimaryKeyAsLong obj = realm.createObject(PrimaryKeyAsLong.class, i);
        obj.setName("String" + i);
    }
    Table table = realm.getTable(PrimaryKeyAsLong.class);
    table.setPrimaryKey("name");
    assertEquals(1, table.getPrimaryKey());
    realm.cancelTransaction();
}
Also used : Table(io.realm.internal.Table) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test)

Example 7 with Table

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

the class RealmMigrationTests method renamePrimaryKeyFieldInMigration.

// Renaming the class should also rename the the class entry in the pk metadata table that tracks primary keys.
@Test
public void renamePrimaryKeyFieldInMigration() {
    buildInitialMigrationSchema(MigrationFieldRenamed.CLASS_NAME, false);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationFieldRenamed.CLASS_NAME).renameField(MigrationPrimaryKey.FIELD_PRIMARY, MigrationFieldRenamed.FIELD_PRIMARY);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationFieldRenamed.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationFieldRenamed.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationFieldRenamed.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationFieldRenamed.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldRenamed.class);
    assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY));
    assertEquals(MigrationFieldRenamed.FIELD_PRIMARY, objectSchema.getPrimaryKey());
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Example 8 with Table

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

the class RealmMigrationTests method setAnnotations.

@Test
public void setAnnotations() {
    // Creates v0 of the Realm.
    RealmConfiguration originalConfig = configFactory.createConfigurationBuilder().schema(AllTypes.class).build();
    Realm.getInstance(originalConfig).close();
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            RealmSchema schema = realm.getSchema();
            schema.create("AnnotationTypes").addField("id", long.class, FieldAttribute.PRIMARY_KEY).addField("indexString", String.class, FieldAttribute.INDEXED).addField("notIndexString", String.class);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(AllTypes.class, AnnotationTypes.class).migration(migration).build();
    realm = Realm.getInstance(realmConfig);
    Table table = realm.getTable(AnnotationTypes.class);
    assertEquals(3, table.getColumnCount());
    assertTrue(table.hasPrimaryKey());
    assertTrue(table.hasSearchIndex(table.getColumnIndex("id")));
    assertTrue(table.hasSearchIndex(table.getColumnIndex("indexString")));
}
Also used : Table(io.realm.internal.Table) AllTypes(io.realm.entities.AllTypes) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 9 with Table

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

the class RealmMigrationTests method renameClassTransferPrimaryKey.

// Tests to show renaming a class does not hinder its PK field's attribute.
@Test
public void renameClassTransferPrimaryKey() {
    buildInitialMigrationSchema(MigrationClassRenamed.CLASS_NAME, true);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().rename(MigrationPrimaryKey.CLASS_NAME, 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 10 with Table

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

the class RealmMigrationTests method removeFieldsAfterPrimaryKey.

// Removing fields after a pk field does not affect the pk.
@Test
public void removeFieldsAfterPrimaryKey() {
    buildInitialMigrationSchema(MigrationPriorIndexOnly.CLASS_NAME, false);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationPriorIndexOnly.CLASS_NAME).removeField(MigrationPrimaryKey.FIELD_FOURTH).removeField(MigrationPrimaryKey.FIELD_FIFTH);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationPriorIndexOnly.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationPriorIndexOnly.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationPriorIndexOnly.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationPriorIndexOnly.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    assertEquals(MigrationPriorIndexOnly.FIELD_PRIMARY, table.getColumnName(table.getPrimaryKey()));
}
Also used : 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