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();
}
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());
}
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")));
}
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));
}
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()));
}
Aggregations