Skip to content

Commit

Permalink
Merge pull request realm#1361 from realm/tg-remove-pk
Browse files Browse the repository at this point in the history
Fix removing primary keys
  • Loading branch information
alazier committed Jan 20, 2015
2 parents 89e3f87 + bb09592 commit 1a581da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ x.x.x Release notes (yyyy-MM-dd)
as it is open in a 32-bit simulator.
* Fixed a crash when adding primary keys to older realm files with no primary
keys on any objects.
* Fixed a crash when removing a primary key in a migration.

0.89.2 Release notes (2015-01-02)
=============================================================
Expand Down
2 changes: 1 addition & 1 deletion Realm/RLMObjectStore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static bool RLMRealmCreateTables(RLMRealm *realm, RLMSchema *targetSchema, bool
}
else if (tableSchema.primaryKeyProperty) {
// there is no primary key, so if thre was one nil out
RLMRealmSetPrimaryKeyForObjectClass(realm, objectSchema.objectClass, nil);
RLMRealmSetPrimaryKeyForObjectClass(realm, objectSchema.className, nil);
changed = true;
}
}
Expand Down
24 changes: 24 additions & 0 deletions Realm/Tests/MigrationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,30 @@ - (void)testPrimaryKeyMigration {
[RLMRealm migrateRealmAtPath:RLMTestRealmPath()];
}

- (void)testRemovePrimaryKeyMigration {
RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:MigrationPrimaryKeyObject.class];

// create realm with old schema and populate
RLMRealm *realm = [self realmWithSingleObject:objectSchema];
[realm beginWriteTransaction];
[realm createObject:MigrationPrimaryKeyObject.className withObject:@[@1]];
[realm createObject:MigrationPrimaryKeyObject.className withObject:@[@2]];
[realm commitWriteTransaction];

objectSchema = [RLMObjectSchema schemaForObjectClass:MigrationPrimaryKeyObject.class];
objectSchema.primaryKeyProperty.isPrimary = NO;
objectSchema.primaryKeyProperty = nil;

// needs a no-op migration
XCTAssertThrows([self realmWithSingleObject:objectSchema]);

[RLMRealm setSchemaVersion:1
forRealmAtPath:RLMTestRealmPath()
withMigrationBlock:^(__unused RLMMigration *migration, __unused NSUInteger oldSchemaVersion) { }];

XCTAssertNoThrow([self realmWithSingleObject:objectSchema]);
}

- (void)testStringPrimaryKeyMigration {
// make string an int
RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:MigrationStringPrimaryKeyObject.class];
Expand Down

0 comments on commit 1a581da

Please sign in to comment.