Extract some logic to a helper function to avoid recursion in update_schema()

This commit is contained in:
Thomas Goyne 2016-01-07 14:27:53 -08:00
parent 8d7b5d8d08
commit 4b001e1842
1 changed files with 16 additions and 6 deletions

View File

@ -201,12 +201,20 @@ void Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
{
schema->validate();
auto needs_update = [&] {
// If the schema version matches, just verify that the schema itself also matches
bool needs_update = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
if (!needs_update) {
bool needs_write = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
if (needs_write) {
return true;
}
ObjectStore::verify_schema(*m_config.schema, *schema, m_config.read_only);
m_config.schema = std::move(schema);
m_config.schema_version = version;
return false;
};
if (!needs_update()) {
return;
}
@ -225,11 +233,13 @@ void Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
// recheck everything
auto current_schema_version = ObjectStore::get_schema_version(read_group());
if (current_schema_version != m_config.schema_version) {
cancel_transaction();
m_config.schema_version = current_schema_version;
*m_config.schema = ObjectStore::schema_from_group(read_group());
return update_schema(std::move(schema), version);
if (!needs_update()) {
cancel_transaction();
return;
}
}
Config old_config(m_config);