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

Code before:
            RLMVerifyAndAlignTableColumns(tableSchema, objectSchema);
        }
    }
    return added;
}

// remove old columns in the realm not in targetSchema
// NOTE: must be called from within write transaction
bool RLMRemoveOldColumnsFromSchema(RLMRealm *realm, RLMSchema *targetSchema) {
    bool removed = false;
    for (RLMObjectSchema *objectSchema in targetSchema.objectSchema) {
        tightdb::TableRef table = RLMTableForObjectClass(realm, objectSchema.className);
        RLMObjectSchema *tableSchema = [RLMObjectSchema schemaForTable:table.get() className:objectSchema.className];

        // remove any columns from tableSchema not in final schema
        for (int i = (int)tableSchema.properties.count - 1; i >= 0; i--) {
            RLMProperty *prop = tableSchema.properties[i];
            if (!objectSchema[prop.name] || ![prop isEqualToProperty:objectSchema[prop.name]]) {
                table->remove_column(prop.column);
                removed = true;
            }
        }
    }
    return removed;
}

bool RLMUpdateTables(RLMRealm *realm, RLMSchema *targetSchema) {
    // set new schema
    realm.schema = [targetSchema copy];

    // first pass create missing tables and verify existing
    bool changed = RLMCreateMissingTables(realm, realm.schema, NO);
    
    // second pass add columns to empty tables
    changed = RLMAddNewColumnsToSchema(realm, realm.schema, NO) || changed;
    
    // remove expired columns
    changed = RLMRemoveOldColumnsFromSchema(realm, realm.schema) || changed;
    
    // FIXME - remove deleted objects
    
    // verify
    // FIXME - remove once we are sure the rest of the code actually works properly
    for (RLMObjectSchema *objectSchema in realm.schema.objectSchema) {
        tightdb::TableRef table = RLMTableForObjectClass(realm, objectSchema.className);
        RLMObjectSchema *tableSchema = [RLMObjectSchema schemaForTable:table.get() className:objectSchema.className];
        RLMVerifyAndAlignTableColumns(tableSchema, objectSchema);
    }
    
    return changed;
}

Review comment (suggestion):
Cause we don't always need to copy - we only copy in cases where we don't create it ourselves (and someone might hold onto it)


Code after:
    }
    return added;
}

// remove old columns in the realm not in targetSchema
// NOTE: must be called from within write transaction
bool RLMRemoveExtraColumns(RLMRealm *realm) {
    bool removed = false;
    for (RLMObjectSchema *objectSchema in realm.schema.objectSchema) {
        RLMObjectSchema *tableSchema = [RLMObjectSchema schemaForTable:objectSchema->_table.get() className:objectSchema.className];

        // remove any columns from tableSchema not in final schema
        for (int i = (int)tableSchema.properties.count - 1; i >= 0; i--) {
            RLMProperty *prop = tableSchema.properties[i];
            if (!objectSchema[prop.name] || ![prop isEqualToProperty:objectSchema[prop.name]]) {
                objectSchema->_table->remove_column(prop.column);
                removed = true;
            }
        }
    }
    return removed;
}

bool RLMRealmSetSchema(RLMRealm *realm, RLMSchema *targetSchema, bool migration) {
    // set new schema
    realm.schema = [targetSchema copy];

    // first pass create missing tables and verify existing
    bool changed = RLMCreateMissingTables(realm);

    // if not migrating then verify all non-empty tables
    if (!migration) {
        for (RLMObjectSchema *objectSchema in realm.schema.objectSchema) {
            if (objectSchema->_table->get_column_count()) {
                RLMObjectSchema *tableSchema = [RLMObjectSchema schemaForTable:objectSchema->_table.get()
                                                                     className:objectSchema.className];
                RLMVerifyAndAlignColumns(tableSchema, objectSchema);
            }
        }
    }

    // second pass add columns to empty tables
    changed = RLMAddMissingColumns(realm) || changed;
    
    // remove expired columns
    changed = RLMRemoveExtraColumns(realm) || changed;
    
    // FIXME - remove deleted objects
    
    // align all tables
    RLMVerifyAndAlignSchema(realm.schema);