Skip to content

[RTDB Desktop] Persist persistence_enabled across Database instances and fix LevelDB .priority reconstruction - #1989

Merged
dconeybe merged 8 commits into
mainfrom
dconeybe/fix-desktop-persistence-across-instances
Nov 26, 2026
Merged

dconeybe merged 8 commits into
mainfrom
dconeybe/fix-desktop-persistence-across-instances

Conversation

@dconeybe

@dconeybe dconeybe commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes two Desktop Realtime Database offline persistence bugs: preserving persistence_enabled_ across recreated DatabaseInternal instances for the lifetime of an App, and preventing LevelDB cache reconstruction from corrupting maps whose .priority key sorts before sibling child keys. It also enables the LevelDB persistence storage engine unit tests in CMake and adds dedicated unit tests for both fixes.

Highlights

  • Cross-Instance Persistence State: Persisted DatabaseInternal::persistence_enabled_ in a thread-safe map keyed by live App* and normalized database URL (ParseUrl with lowercased hostname), matching Android and iOS lifecycle behavior across delete database + Database::GetInstance(...) and automatically cleaning up entries via CleanupNotifier when the App is destroyed.
  • LevelDB .priority Reconstruction Fix: Guarded scalar-wrapping in VariantAddCachedValue() with !variant->is_null() so map nodes whose .priority key sorts lexicographically before sibling child keys (such as "subkey" or "value") initialize as empty maps instead of {".value": Null}, while scalars with .priority continue to reconstruct as {".value", ".priority"}.
  • Expanded Desktop Persistence Test Coverage: Enabled firebase_rtdb_desktop_level_db_persistence_storage_engine_test in CMake with cross-platform testing::TempDir() and leveldb::DestroyDB() cleanup, added .priority map/scalar regression tests, and added firebase_rtdb_desktop_database_desktop_test covering cross-instance persistence, URL normalization, isolation, post-init immutability, and App destruction cleanup.
Changelog
  • database_desktop.h
    • Added an internal persistence_enabled() getter on DatabaseInternal for unit testing.
  • database_desktop.cc
    • Added a thread-safe App*-scoped std::unordered_map (GetPersistenceEnabledMap() guarded by a heap-allocated leaf lock g_persistence_enabled_mutex) storing persistence_enabled_ by normalized database URL.
    • Added NormalizeDatabaseUrl() using ParseUrl with lowercased hostnames (falling back to the raw URL on parse failure).
    • Updated DatabaseInternal::DatabaseInternal() to restore persistence_enabled_ from the map and DatabaseInternal::SetPersistenceEnabled() to persist the setting and register cleanup on App's CleanupNotifier.
  • level_db_persistence_storage_engine.cc
    • Updated VariantAddCachedValue() to check !variant->is_null() before wrapping a non-map variant into {".value": *variant} when encountering .priority.
  • CMakeLists.txt
    • Registered firebase_rtdb_desktop_level_db_persistence_storage_engine_test and firebase_rtdb_desktop_database_desktop_test targets.
  • database_desktop_test.cc
    • New unit test suite verifying DatabaseInternal::SetPersistenceEnabled() defaults, persistence across instances, false overriding true, URL normalization, invalid URL fallback, App/URL isolation, post-repo_ immutability, and cleanup on App destruction.
  • level_db_persistence_storage_engine_test.cc
    • Replaced custom TEST_TMPDIR/<windows.h> temp directory logic with testing::TempDir(), unique suite+test directory names, and leveldb::DestroyDB() cleanup in InitializeLevelDb(), TearDown(), and ConstructorBasic.
    • Updated RunTwice() to close and reopen engine_ directly without wiping the database mid-test.
    • Fixed gmock/gmock.h include and uint64_t portability.
    • Added regression tests for maps and scalars with .priority (including keys sorting before and after .priority, nested maps, falsy scalars, and scalar/map overwrites).
  • readme.md
    • Added Upcoming release notes entries for both Desktop Realtime Database offline persistence fixes.

…ge engine tests

When reconstructing cached server data from LevelDB in VariantAddCachedValue(),
checking IsPriorityKey(directory) without checking !variant->is_null() wrapped
an uninitialized Variant::Null() placeholder into {".value": Null} whenever
".priority" sorted lexicographically before sibling child keys (e.g., "subkey",
"value"). Guard the scalar-wrapping branch with !variant->is_null() so map
nodes initialize as empty maps while scalar values with ".priority" still wrap
into {".value", ".priority"}.

Also enable firebase_rtdb_desktop_level_db_persistence_storage_engine_test in
CMake, fix portability issues (gmock header, uint64_t, testing::TempDir() with
leveldb::DestroyDB cleanup), and add regression tests covering maps and scalars
with .priority.
On Android and iOS, deleting the C++ Database wrapper and calling
Database::GetInstance() reuses the underlying native FirebaseDatabase/FIRDatabase
singleton, preserving persistence_enabled across instances for the lifetime of
the App. On Desktop, however, DatabaseInternal::persistence_enabled_ was stored
only on the instance and reset to false after delete database +
Database::GetInstance().

Remember persistence_enabled_ per (App*, normalized database URL) in a
thread-safe map, clean up entries automatically via the App's CleanupNotifier
when the App is destroyed, and add unit tests in database_desktop_test.cc.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to track and persist the persistence-enabled state across DatabaseInternal instances on desktop, ensuring proper cleanup when an App is destroyed. It also fixes an issue in the LevelDB storage engine where a priority key sorting before child keys on a null variant incorrectly wrapped Null in a .value field, and adds comprehensive unit tests. The reviewer suggests updating NormalizeDatabaseUrl to include the database namespace in the normalized URL, which prevents distinct database instances sharing the same host (e.g., in emulator or multi-tenant environments) from incorrectly sharing persistence settings.

@dconeybe dconeybe self-assigned this Nov 24, 2026
@dconeybe dconeybe added the tests-requested: quick Trigger a quick set of integration tests. label Nov 24, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: quick Trigger a quick set of integration tests. labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅  Integration test succeeded!

Requested by @dconeybe on commit 70aa1bc
Last updated: Wed Nov 25 18:39 PST 2026
View integration test log & download artifacts

@github-actions github-actions Bot added the tests: failed This PR's integration tests failed. label Nov 25, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 25, 2026
@dconeybe dconeybe added tests-requested: quick Trigger a quick set of integration tests. and removed tests: failed This PR's integration tests failed. labels Nov 25, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: succeeded This PR's integration tests succeeded. and removed tests-requested: quick Trigger a quick set of integration tests. labels Nov 25, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 25, 2026
@dconeybe
dconeybe merged commit 70aa1bc into main Nov 26, 2026
88 of 96 checks passed
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests: succeeded This PR's integration tests succeeded. labels Nov 26, 2026
@dconeybe
dconeybe deleted the dconeybe/fix-desktop-persistence-across-instances branch November 26, 2026 02:04
@github-actions github-actions Bot added the tests: succeeded This PR's integration tests succeeded. label Nov 26, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: database tests: succeeded This PR's integration tests succeeded.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants