update table indexes based on schema changes

This commit is contained in:
Ari Lazier 2015-06-04 17:44:06 -07:00
parent 85047bb96d
commit 1ccf9f6192
2 changed files with 64 additions and 2 deletions

View File

@ -289,6 +289,8 @@ bool ObjectStore::update_realm_with_schema(realm::Group *group,
} }
} }
changed = changed | update_indexes(group, schema);
if (!migrating) { if (!migrating) {
return changed; return changed;
} }
@ -313,3 +315,52 @@ ObjectStore::Schema ObjectStore::schema_from_group(Group *group) {
} }
return schema; return schema;
} }
bool ObjectStore::are_indexes_up_to_date(Group *group, Schema &schema) {
for (auto &object_schema:schema) {
TableRef table = table_for_object_type(group, object_schema.name);
if (!table) {
continue;
}
validate_schema_and_update_column_mapping(group, object_schema); // FIXME we just need the column mapping
for (auto &property:object_schema.properties) {
if (property.is_indexed != table->has_search_index(property.table_column)) {
return false;
}
}
}
return true;
}
bool ObjectStore::update_indexes(Group *group, Schema &schema) {
bool changed = false;
for (auto &object_schema:schema) {
TableRef table = table_for_object_type(group, object_schema.name);
if (!table) {
continue;
}
for (auto &property:object_schema.properties) {
if (property.is_indexed == table->has_search_index(property.table_column)) {
continue;
}
changed = true;
if (property.is_indexed) {
try {
table->add_search_index(property.table_column);
}
catch (realm::LogicError const&) {
throw ObjectStoreException(ObjectStoreException::RealmPropertyTypeNotIndexable,
{{"object_type", object_schema.name}, {"property_name", property.name}, {"property_type", string_for_property_type(property.type)}});
}
}
else {
table->remove_search_index(property.table_column);
}
}
}
return changed;
}

View File

@ -38,7 +38,7 @@ namespace realm {
// returns array of validation errors // returns array of validation errors
static std::vector<std::string> validate_schema_and_update_column_mapping(Group *group, ObjectSchema &target_schema); static std::vector<std::string> validate_schema_and_update_column_mapping(Group *group, ObjectSchema &target_schema);
// updates a Realm to a given target schema/version creating tables as necessary // updates a Realm to a given target schema/version creating tables and updating indexes as necessary
// returns if any changes were made // returns if any changes were made
// passed in schema ar updated with the correct column mapping // passed in schema ar updated with the correct column mapping
// optionally runs migration function/lambda if schema is out of date // optionally runs migration function/lambda if schema is out of date
@ -56,6 +56,9 @@ namespace realm {
// get existing Schema from a group // get existing Schema from a group
static Schema schema_from_group(Group *group); static Schema schema_from_group(Group *group);
// check if indexes are up to date - if false you need to call update_realm_with_schema
static bool are_indexes_up_to_date(Group *group, Schema &schema);
private: private:
// set a new schema version // set a new schema version
static void set_schema_version(Group *group, uint64_t version); static void set_schema_version(Group *group, uint64_t version);
@ -83,6 +86,9 @@ namespace realm {
static std::string table_name_for_object_type(std::string class_name); static std::string table_name_for_object_type(std::string class_name);
static std::string object_type_for_table_name(std::string table_name); static std::string object_type_for_table_name(std::string table_name);
// returns if any indexes were changed
static bool update_indexes(Group *group, Schema &schema);
friend ObjectSchema; friend ObjectSchema;
}; };
@ -91,12 +97,17 @@ namespace realm {
enum Kind { enum Kind {
// thrown when calling update_realm_to_schema and the realm version is greater than the given version // thrown when calling update_realm_to_schema and the realm version is greater than the given version
RealmVersionGreaterThanSchemaVersion, RealmVersionGreaterThanSchemaVersion,
RealmPropertyTypeNotIndexable, // object_type, property_name, property_type
}; };
ObjectStoreException(Kind kind) : m_kind(kind) {}
typedef std::map<std::string, std::string> Dict;
ObjectStoreException(Kind kind, Dict dict = Dict()) : m_kind(kind), m_dict(dict) {}
ObjectStoreException::Kind kind() { return m_kind; } ObjectStoreException::Kind kind() { return m_kind; }
ObjectStoreException::Dict &dict() { return m_dict; }
private: private:
Kind m_kind; Kind m_kind;
Dict m_dict;
}; };
class ObjectStoreValidationException : public std::exception { class ObjectStoreValidationException : public std::exception {