-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Import/export support for emulators #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
551fd87
a77c88c
d5f83db
737e283
83f169c
3071705
f9db654
e742e17
d1546a4
7345434
d8f50f1
953f169
55f1fb1
9f7872e
4fc3c42
bfec65f
1cc4699
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
| }, | ||
| "functions": {}, | ||
| "emulators": { | ||
| "hub": { | ||
| "port": 4000 | ||
| }, | ||
| "database": { | ||
| "port": 9000 | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import * as clc from "cli-color"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
|
|
||
| import * as api from "../api"; | ||
| import { Command } from "../command"; | ||
| import * as commandUtils from "../emulator/commandUtils"; | ||
| import * as utils from "../utils"; | ||
| import { EmulatorHub } from "../emulator/hub"; | ||
| import { FirebaseError } from "../error"; | ||
|
|
||
| module.exports = new Command("emulators:export <path>") | ||
| .description("export data from running emulators") | ||
| .option(commandUtils.FLAG_ONLY, commandUtils.DESC_ONLY) | ||
| .action(async (exportPath: string, options: any) => { | ||
| const projectId = options.project; | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const locator = EmulatorHub.readLocatorFile(projectId); | ||
| if (!locator) { | ||
| throw new FirebaseError( | ||
| `Did not find any running emulators for project ${clc.bold(projectId)}.`, | ||
| { exit: 1 } | ||
| ); | ||
| } | ||
| const hubOrigin = `http://${locator.host}:${locator.port}`; | ||
|
|
||
| try { | ||
| await api.request("GET", "/", { | ||
| origin: hubOrigin, | ||
| }); | ||
| } catch (e) { | ||
| throw new FirebaseError( | ||
| `The emulator hub at ${hubOrigin} did not respond to a status check. If this error continues try shutting down all running emulators and deleting the file ${EmulatorHub.getLocatorFilePath( | ||
| projectId | ||
samtstern marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| )}`, | ||
| { exit: 1 } | ||
| ); | ||
| } | ||
|
|
||
| utils.logBullet( | ||
| `Found running emulator hub for project ${clc.bold(projectId)} at ${hubOrigin}` | ||
| ); | ||
|
|
||
| // If the export target directory does not exist, we should attempt to create it | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: What happens if target directory exists and contains some stale data? Will there be any foreseeable conflict? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's actually a good question ... nothing explodes but I wonder if Firestore is writing a whole new export or appending to the existing one. Do you know? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, I don't know about the details of Firestore export format either, but let's test this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I went with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Purrrrfect. |
||
| const absPath = path.resolve(exportPath); | ||
| if (!fs.existsSync(absPath)) { | ||
| utils.logBullet(`Creating export directory ${absPath}`); | ||
| fs.mkdirSync(absPath); | ||
| } | ||
|
|
||
| const exportBody = { | ||
samtstern marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| path: absPath, | ||
| }; | ||
|
|
||
| utils.logBullet(`Exporting data to: ${absPath}`); | ||
| await api | ||
| .request("POST", EmulatorHub.PATH_EXPORT, { | ||
| origin: hubOrigin, | ||
| json: true, | ||
| data: exportBody, | ||
| }) | ||
| .catch((e) => { | ||
| throw new FirebaseError("Export request failed, see emulator logs for more information.", { | ||
| exit: 1, | ||
| original: e, | ||
| }); | ||
| }); | ||
|
|
||
| utils.logSuccess("Export complete"); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.