Skip to content

Commit 4f50803

Browse files
authored
Cleanly shut down PGlite when shutting down Data Connect (#8056)
1 parent 5039643 commit 4f50803

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
- Changes default CF3 runtime to nodejs22 (#8037)
22
- Fixed an issue where `--import` would error for the Data Connect emulator if `dataDir` was also set.
33
- Fixed an issue where `firebase init dataconnect` errored when importing a schema with no GQL files.
4-
- CF3 callables can now be annotate with a genkit action they are serving (#8039)
5-
- HTTPS functions can now be upgraded to HTTPS Callable functions (#8039)
6-
- Update default tsconfig to support more modern defaults (#8039)
4+
- Fixed an issue where the Data Connect emulator would not cleanly shut down Postgres and corrupt data. (#8044)
5+
- CF3 callables can now be annotate with a genkit action they are serving. (#8039)
6+
- HTTPS functions can now be upgraded to HTTPS Callable functions. (#8039)
7+
- Update default tsconfig to support more modern defaults. (#8039)
78
- Update the Firebase Data Connect local toolkit to v1.7.5, which includes a fix for Kotlin codegen that ensures that generated XxxKeys.kt files include the required `@file:UseSerializers(UUIDSerializer::class)` annotation. (#8058)

src/emulator/dataconnect/pgliteServer.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://github.com/supabase-community/pg-gateway
22

3-
import { PGlite, PGliteOptions } from "@electric-sql/pglite";
3+
import { DebugLevel, PGlite, PGliteOptions } from "@electric-sql/pglite";
44
// Unfortunately, we need to dynamically import the Postgres extensions.
55
// They are only available as ESM, and if we import them normally,
66
// our tsconfig will convert them to requires, which will cause errors
@@ -18,6 +18,7 @@ import {
1818
import { fromNodeSocket } from "./pg-gateway/platforms/node";
1919
import { logger } from "../../logger";
2020
import { hasMessage } from "../../error";
21+
2122
export const TRUNCATE_TABLES_SQL = `
2223
DO $do$
2324
BEGIN
@@ -35,8 +36,11 @@ export class PostgresServer {
3536
private database: string;
3637
private dataDirectory?: string;
3738
private importPath?: string;
39+
private debug: DebugLevel;
3840

3941
public db: PGlite | undefined = undefined;
42+
private server: net.Server | undefined = undefined;
43+
4044
public async createPGServer(host: string = "127.0.0.1", port: number): Promise<net.Server> {
4145
const getDb = this.getDb.bind(this);
4246

@@ -67,6 +71,7 @@ export class PostgresServer {
6771
server.emit("error", err);
6872
});
6973
});
74+
this.server = server;
7075

7176
const listeningPromise = new Promise<void>((resolve) => {
7277
server.listen(port, host, () => {
@@ -86,7 +91,7 @@ export class PostgresServer {
8691
const pgliteArgs: PGliteOptions = {
8792
username: this.username,
8893
database: this.database,
89-
debug: 0,
94+
debug: this.debug,
9095
extensions: {
9196
vector,
9297
uuidOssp,
@@ -132,11 +137,28 @@ export class PostgresServer {
132137
}
133138
}
134139

135-
constructor(database: string, username: string, dataDirectory?: string, importPath?: string) {
136-
this.username = username;
137-
this.database = database;
138-
this.dataDirectory = dataDirectory;
139-
this.importPath = importPath;
140+
public async stop(): Promise<void> {
141+
if (this.db) {
142+
await this.db.close();
143+
}
144+
if (this.server) {
145+
this.server.close();
146+
}
147+
return;
148+
}
149+
150+
constructor(args: {
151+
database: string;
152+
username: string;
153+
dataDirectory?: string;
154+
importPath?: string;
155+
debug?: boolean;
156+
}) {
157+
this.username = args.username;
158+
this.database = args.database;
159+
this.dataDirectory = args.dataDirectory;
160+
this.importPath = args.importPath;
161+
this.debug = args.debug ? 5 : 0;
140162
}
141163
}
142164

src/emulator/dataconnectEmulator.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface DataConnectEmulatorArgs {
4040
enable_output_schema_extensions: boolean;
4141
enable_output_generated_sdk: boolean;
4242
importPath?: string;
43+
debug?: boolean;
4344
}
4445

4546
export interface DataConnectGenerateArgs {
@@ -116,7 +117,13 @@ export class DataConnectEmulator implements EmulatorInstance {
116117
const postgresDumpPath = this.args.importPath
117118
? path.join(this.args.importPath, "postgres.tar.gz")
118119
: undefined;
119-
this.postgresServer = new PostgresServer(dbId, "postgres", dataDirectory, postgresDumpPath);
120+
this.postgresServer = new PostgresServer({
121+
database: dbId,
122+
username: "fdc",
123+
dataDirectory,
124+
importPath: postgresDumpPath,
125+
debug: this.args.debug,
126+
});
120127
const server = await this.postgresServer.createPGServer(pgHost, pgPort);
121128
const connectableHost = connectableHostname(pgHost);
122129
connStr = `postgres://${connectableHost}:${pgPort}/${dbId}?sslmode=disable`;
@@ -166,6 +173,9 @@ export class DataConnectEmulator implements EmulatorInstance {
166173
);
167174
return;
168175
}
176+
if (this.postgresServer) {
177+
await this.postgresServer.stop();
178+
}
169179
return stop(Emulators.DATACONNECT);
170180
}
171181

0 commit comments

Comments
 (0)