mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-12 15:24:18 +00:00
Reshuffle the updating of m_config in Realm::update_schema()
This commit is contained in:
parent
6c25eeb85c
commit
9c2d4703ed
@ -232,42 +232,33 @@ bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
|
|||||||
return update_schema(std::move(schema), version);
|
return update_schema(std::move(schema), version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Config old_config(m_config);
|
||||||
auto migration_function = [&](Group*, Schema&) {
|
auto migration_function = [&](Group*, Schema&) {
|
||||||
SharedRealm old_realm(new Realm(m_config));
|
SharedRealm old_realm(new Realm(old_config));
|
||||||
|
// Need to open in read-write mode so that it uses a SharedGroup, but
|
||||||
|
// users shouldn't actually be able to write via the old realm
|
||||||
old_realm->m_config.read_only = true;
|
old_realm->m_config.read_only = true;
|
||||||
|
|
||||||
auto new_realm = shared_from_this();
|
if (m_config.migration_function) {
|
||||||
m_config.schema = std::move(schema);
|
m_config.migration_function(old_realm, shared_from_this());
|
||||||
m_config.schema_version = version;
|
|
||||||
|
|
||||||
if (!m_config.migration_function) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
m_config.migration_function(old_realm, new_realm);
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
m_config.schema = std::move(old_realm->m_config.schema);
|
|
||||||
m_config.schema_version = old_realm->m_config.schema_version;
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool changed = ObjectStore::update_realm_with_schema(read_group(), *m_config.schema,
|
try {
|
||||||
version, *schema,
|
|
||||||
migration_function);
|
|
||||||
commit_transaction();
|
|
||||||
|
|
||||||
if (schema) {
|
|
||||||
// We update the schema after opening the "old" Realm in the migration
|
|
||||||
// block to reduce the amount of juggling required, but that means that
|
|
||||||
// the schema hasn't been updated if no migration occurred
|
|
||||||
m_config.schema = std::move(schema);
|
m_config.schema = std::move(schema);
|
||||||
m_config.schema_version = version;
|
m_config.schema_version = version;
|
||||||
}
|
|
||||||
|
|
||||||
|
bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema,
|
||||||
|
version, *m_config.schema,
|
||||||
|
migration_function);
|
||||||
|
commit_transaction();
|
||||||
return changed;
|
return changed;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
m_config.schema = std::move(old_config.schema);
|
||||||
|
m_config.schema_version = old_config.schema_version;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check_read_write(Realm *realm)
|
static void check_read_write(Realm *realm)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user