Repository: realm/realm-swift
File: Realm/RLMObjectStore.mm (Objective-C++)

Code before:
    // second pass add columns
    for (RLMObjectSchema *objectSchema in realm.schema.objectSchema) {
        tightdb::TableRef table = RLMTableForObjectClass(realm, objectSchema.className);
        
        if (table->get_column_count() == 0) {
            for (RLMProperty *prop in objectSchema.properties) {
                tightdb::StringData name(prop.name.UTF8String, prop.name.length);
                switch (prop.type) {
                    // for objects and arrays, we have to specify target table
                    case RLMPropertyTypeObject:
                    case RLMPropertyTypeArray: {
                        tightdb::TableRef linkTable = RLMTableForObjectClass(realm, prop.objectClassName);
                        table->add_column_link(tightdb::DataType(prop.type), name, *linkTable);
                        break;
                    }
                    default: {
                    	size_t column = table->add_column((tightdb::DataType)prop.type, name);
                    	if (prop.attributes & RLMPropertyAttributeIndexed) {
                            table->set_index(column);
                        }
                        break;
                    }
                }
            }
        }
        else {
            if (table->get_column_count() != objectSchema.properties.count) {
                // FIXME - rollback
                @throw [NSException exceptionWithName:@"RLMException" reason:@"Column count does not match interface - migration required"
                                             userInfo:nil];
            }
            // FIXME - verify columns match
        }
    }
    [realm commitWriteTransaction];
}

void RLMAddObjectToRealm(RLMObject *object, RLMRealm *realm) {
    // if already in the right realm then no-op
    if (object.realm == realm) {
        return;
    }
    
    // if realm is not writable throw
    if (!realm.inWriteTransaction) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Can only add an object to a Realm during a write transaction"
                                     userInfo:nil];
    }
    
    // get table and create new row

Review comment (suggestion):
We should rollback here. Unfortunately the new implicit transaction don't support it, so we can't for the time being.


Code after:
        tightdb::TableRef table = RLMTableForObjectClass(realm, objectSchema.className);
        
        if (table->get_column_count() == 0) {
            for (RLMProperty *prop in objectSchema.properties) {
                tightdb::StringData name(prop.name.UTF8String, prop.name.length);
                switch (prop.type) {
                    // for objects and arrays, we have to specify target table
                    case RLMPropertyTypeObject:
                    case RLMPropertyTypeArray: {
                        tightdb::TableRef linkTable = RLMTableForObjectClass(realm, prop.objectClassName);
                        table->add_column_link(tightdb::DataType(prop.type), name, *linkTable);
                        break;
                    }
                    default: {
                    	size_t column = table->add_column((tightdb::DataType)prop.type, name);
                    	if (prop.attributes & RLMPropertyAttributeIndexed) {
                            table->set_index(column);
                        }
                        break;
                    }
                }
            }
        }
        else {
            if (table->get_column_count() != objectSchema.properties.count) {
                // FIXME - rollback
                @throw [NSException exceptionWithName:@"RLMException" reason:@"Column count does not match interface - migration required"
                                             userInfo:nil];
            }
            // FIXME - verify columns match
        }
    }
    [realm commitWriteTransaction];
}

void RLMAddObjectToRealm(RLMObject *object, RLMRealm *realm) {
    // if already in the right realm then no-op
    if (object.realm == realm) {
        return;
    }
    
    // if realm is not writable throw
    if (!realm.inWriteTransaction) {
        @throw [NSException exceptionWithName:@"RLMException"
                                       reason:@"Can only add an object to a Realm during a write transaction"
                                     userInfo:nil];
    }
    
    // get table and create new row
    NSString *objectClassName = object.RLMObject_schema.className;
    object.realm = realm;