Skip to content

Commit

Permalink
Fixing an issue with --import and dataDir at the same time (#8048)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehan authored Dec 9, 2024
1 parent efd31c4 commit 89f1c8a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Changes default CF3 runtime to nodejs22 (#8037)
- Fixed an issue where `--import` would error for the Data Connect emulator if `dataDir` was also set.
22 changes: 21 additions & 1 deletion src/emulator/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { LoggingEmulator } from "./loggingEmulator";
import * as dbRulesConfig from "../database/rulesConfig";
import { EmulatorLogger, Verbosity } from "./emulatorLogger";
import { EmulatorHubClient } from "./hubClient";
import { confirm } from "../prompt";
import { confirm, promptOnce } from "../prompt";
import {
FLAG_EXPORT_ON_EXIT_NAME,
JAVA_DEPRECATION_WARNING,
Expand Down Expand Up @@ -893,6 +893,26 @@ export async function startAll(
importDirAbsPath,
exportMetadata.dataconnect.path,
);
const dataDirectory = options.config.get("emulators.dataconnect.dataDir");
if (exportMetadataFilePath && dataDirectory) {
EmulatorLogger.forEmulator(Emulators.DATACONNECT).logLabeled(
"WARN",
"dataconnect",
"'firebase.json#emulators.dataconnect.dataDir' is set and `--import` flag was passed. " +
"This will overwrite any data saved from previous runs.",
);
if (
!options.nonInteractive &&
!(await promptOnce({
type: "confirm",
message: `Do you wish to continue and overwrite data in ${dataDirectory}?`,
default: false,
}))
) {
await cleanShutdown();
return { deprecationNotices: [] };
}
}

EmulatorLogger.forEmulator(Emulators.DATACONNECT).logLabeled(
"BULLET",
Expand Down
18 changes: 17 additions & 1 deletion src/emulator/dataconnect/pgliteServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "./pg-gateway/index";
import { fromNodeSocket } from "./pg-gateway/platforms/node";
import { logger } from "../../logger";
import { hasMessage } from "../../error";
export const TRUNCATE_TABLES_SQL = `
DO $do$
BEGIN
Expand Down Expand Up @@ -98,7 +99,7 @@ export class PostgresServer {
const file = new File([rf], this.importPath);
pgliteArgs.loadDataDir = file;
}
this.db = await PGlite.create(pgliteArgs);
this.db = await this.forceCreateDB(pgliteArgs);
await this.db.waitReady;
}
return this.db;
Expand All @@ -116,6 +117,21 @@ export class PostgresServer {
fs.writeFileSync(exportPath, new Uint8Array(arrayBuff));
}

async forceCreateDB(pgliteArgs: PGliteOptions): Promise<PGlite> {
try {
const db = await PGlite.create(pgliteArgs);
return db;
} catch (err: unknown) {
if (pgliteArgs.dataDir && hasMessage(err) && /Database already exists/.test(err.message)) {
// Clear out the current pglite data
fs.rmSync(pgliteArgs.dataDir, { force: true, recursive: true });
const db = await PGlite.create(pgliteArgs);
return db;
}
throw err;
}
}

constructor(database: string, username: string, dataDirectory?: string, importPath?: string) {
this.username = username;
this.database = database;
Expand Down
3 changes: 1 addition & 2 deletions src/emulator/downloadableEmulators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "./types";
import { Constants } from "./constants";

import { FirebaseError } from "../error";
import { FirebaseError, hasMessage } from "../error";
import * as childProcess from "child_process";
import * as utils from "../utils";
import { EmulatorLogger } from "./emulatorLogger";
Expand Down Expand Up @@ -655,7 +655,6 @@ export async function start(
}

export function isIncomaptibleArchError(err: unknown): boolean {
const hasMessage = (e: any): e is { message: string } => !!e?.message;
return (
hasMessage(err) &&
/Unknown system error/.test(err.message ?? "") &&
Expand Down
5 changes: 5 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,8 @@ export function isBillingError(e: {
);
});
}

/**
* Checks whether an unknown object (such as an error) has a message field
*/
export const hasMessage = (e: any): e is { message: string } => !!e?.message;

0 comments on commit 89f1c8a

Please sign in to comment.