From 4b001e1842f5043459d98f1429b0ffaf7f905cd4 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 7 Jan 2016 14:27:53 -0800 Subject: [PATCH] Extract some logic to a helper function to avoid recursion in update_schema() --- src/shared_realm.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/shared_realm.cpp b/src/shared_realm.cpp index 8f51a96e..4720582c 100644 --- a/src/shared_realm.cpp +++ b/src/shared_realm.cpp @@ -201,12 +201,20 @@ void Realm::update_schema(std::unique_ptr schema, uint64_t version) { schema->validate(); - // 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) { + auto needs_update = [&] { + // If the schema version matches, just verify that the schema itself also matches + 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, 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);