Skip to content

feat(emulators): export and import every Firestore database listed in firebase.json - #11624

Open
mokshupadhyay wants to merge 3 commits into
firebase:mainfrom
mokshupadhyay:feat/export-named-firestore-databases
Open

mokshupadhyay wants to merge 3 commits into
firebase:mainfrom
mokshupadhyay:feat/export-named-firestore-databases

Conversation

@mokshupadhyay

Copy link
Copy Markdown

Description

Fixes #9665. As suggested in the issue, the Firestore emulator export now covers every database that is declared in firebase.json, not only (default), and --import restores them.

  • Export (emulators:export and --export-on-exit): the (default) database is still exported to firestore_export/, unchanged. Each named database listed in the firestore config (single object or array) is exported to its own folder, firestore_export_<id>/. The id is made filesystem-safe and folder names are de-duplicated case-insensitively, because two exports with the same export_name overwrite each other.
  • Metadata: firebase-export-metadata.json gets an optional firestore.databases array (database, path, metadata_file). It is only written when named databases exist, so the metadata of a default-only export is byte-identical to before, and old metadata imports exactly as before.
  • Import: the emulator still imports (default) at startup (seed_from_export). Named databases are imported after the emulator starts, by POST /emulator/v1/projects/<project>:import with database and export_directory pointing at the database's .overall_export_metadata file.

Not covered (same limit as discussed in the issue): databases that are created at runtime and not listed in firebase.json, because the emulator has no list-databases endpoint.

Open questions for you: the folder name firestore_export_<id> and the firestore.databases array (a map keyed by id would also work); whether --only should narrow which databases are exported (I made export ignore it); and whether import should warn when a database in the metadata is no longer in firebase.json (it currently follows the metadata).

Scenarios Tested

  • Real Firestore emulator (v1.22.0, project demo-test, firebase.json with (default) and some-database, one document written to each):
    • emulators:export ./out produces firestore_export/ and firestore_export_some-database/, each with its .overall_export_metadata, and the metadata file lists some-database.
    • restarting with --import ./out restores both databases (both documents read back). With only the export change, (default) was restored and some-database came back empty.
    • a re-export from the restarted emulator again produces both folders.
  • Unit tests: new src/emulator/hubExport.spec.ts (5 tests) and two new tests in firestoreEmulator.spec.ts. Against the original sources 6 of them fail, with the change all pass; the touched spec files plus hub and commandUtils specs: 31 passing, 0 failing.
  • tsc --noEmit and prettier --check are clean, and eslint reports 0 errors on the touched files.

Not tested: the --export-on-exit path itself (it goes through the same exportEmulatorData, but I only ran emulators:export), emulators:exec, Windows paths and very long database ids. I did not run the full npm test.

Sample Commands

firebase emulators:export ./out
firebase emulators:start --import ./out

emulators:export and --export-on-exit only exported the (default)
database. Export each database declared in the firestore config into its
own folder (firestore_export_<id>) and record them in the export
metadata under firestore.databases. The (default) database still goes to
firestore_export, and metadata without named databases is unchanged.
--import only restored the (default) database. After the Firestore
emulator starts, import each database recorded in
firestore.databases of the export metadata through the emulator's
:import endpoint.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@mokshupadhyay

Copy link
Copy Markdown
Author

Hi @joehan, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!

…estore-databases

# Conflicts:
#	CHANGELOG.md

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Firestore emulator does not export non-default databases when using --export-on-exit

1 participant