Skip to content

Commit

Permalink
reload schema (#7411)
Browse files Browse the repository at this point in the history
Co-authored-by: joehan <joehanley@google.com>
  • Loading branch information
hlshen and joehan authored Jul 9, 2024
1 parent 587e593 commit 85d27c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
3 changes: 3 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## NEXT

- [Fixed] Data Connect emulator issues properly streamed on startup
- [Fixed] Data Connect schema reloads consistently

## 0.4.0

- Updated internal firebase-tools dependency to 13.13.0
Expand Down
16 changes: 12 additions & 4 deletions firebase-vscode/src/data-connect/emulator-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum Kind {
SQL_CONNECTION = "SQL_CONNECTION",
SQL_MIGRATION = "SQL_MIGRATION",
VERTEX_AI = "VERTEX_AI",
FILE_RELOAD = "FILE_RELOAD",
}
enum Severity {
SEVERITY_UNSPECIFIED = "SEVERITY_UNSPECIFIED",
Expand All @@ -36,13 +37,15 @@ export async function runEmulatorIssuesStream(
configs: ResolvedDataConnectConfigs,
fdcEndpoint: string,
isPostgresEnabled: Signal<boolean>,
schemaReloadFunc: () => void,
) {
const obsErrors = await getEmulatorIssuesStream(configs, fdcEndpoint);
const obsConverter = {
next(nextResponse: EmulatorIssueResponse) {
if (nextResponse.result?.issues?.length) {
for (const issue of nextResponse.result.issues) {
displayAndHandleIssue(issue, isPostgresEnabled);
console.log("HAROLD: ", issue)
displayAndHandleIssue(issue, isPostgresEnabled, schemaReloadFunc);
}
}
},
Expand All @@ -59,19 +62,24 @@ export async function runEmulatorIssuesStream(
/**
* Based on the severity of the issue, either log, display notification, or display interactive popup to the user
*/
export function displayAndHandleIssue(issue: EmulatorIssue, isPostgresEnabled: Signal<boolean>) {
export function displayAndHandleIssue(
issue: EmulatorIssue,
isPostgresEnabled: Signal<boolean>,
schemaReloadFunc: () => void,
) {
const issueMessage = `Data Connect Emulator: ${issue.kind.toString()} - ${issue.message}`;
if (issue.severity === Severity.ALERT) {
vscode.window.showErrorMessage(issueMessage);
} else if (issue.severity === Severity.NOTICE) {
vscode.window.showWarningMessage(issueMessage);
}
emulatorOutputChannel.appendLine(issueMessage);

// special handlings
if (issue.kind === Kind.SQL_CONNECTION) {
isPostgresEnabled.value = false;
}
if (issue.kind === Kind.FILE_RELOAD) {
schemaReloadFunc();
}
}

/**
Expand Down
42 changes: 8 additions & 34 deletions firebase-vscode/src/data-connect/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { dataConnectConfigs } from "./config";
import { runEmulatorIssuesStream } from "./emulator-stream";
import { runDataConnectCompiler } from "./core-compiler";
import { pluginLogger } from "../logger-wrapper";
import { Emulators, getEmulatorDetails, listRunningEmulators } from "../cli";
import { emulatorOutputChannel } from "./emulator-stream";

/** FDC-specific emulator logic */
export class DataConnectEmulatorController implements vscode.Disposable {
Expand All @@ -27,14 +25,19 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// on emulator restart, re-connect
dataConnectEmulatorEvents.on("restart", () => {
pluginLogger.log("Emulator started");
pluginLogger.log("Emulator restarted");
this.isPostgresEnabled.value = false;
this.connectToEmulator();
});

this.subs.push(
broker.on("connectToPostgres", () => this.connectToPostgres()),

// TODO: This is assuming only dataconnect emulator will run
effect(() => {
if (this.emulatorsController.emulators.value.status === "running") {
this.connectToEmulator();
}
}),
// Notify webviews when the emulator status changes
effect(() => {
if (this.isPostgresEnabled.value) {
Expand Down Expand Up @@ -119,47 +122,18 @@ export class DataConnectEmulatorController implements vscode.Disposable {

// Commands to run after the emulator is started successfully
private async connectToEmulator() {
this.schemaReload();
const configs = dataConnectConfigs.value?.tryReadValue;

runEmulatorIssuesStream(
configs,
this.emulatorsController.getLocalEndpoint().value,
this.isPostgresEnabled,
this.schemaReload,
);
runDataConnectCompiler(
configs,
this.emulatorsController.getLocalEndpoint().value,
);

this.setupOutputChannel();
}

private setupOutputChannel() {
if (
listRunningEmulators().filter((emulatorInfos) => {
emulatorInfos.name === Emulators.DATACONNECT;
})
) {
const dataConnectEmulatorDetails = getEmulatorDetails(
Emulators.DATACONNECT,
);

// Child process is only available when emulator is started through vscode
if (dataConnectEmulatorDetails.instance) {
dataConnectEmulatorDetails.instance.stdout?.on("data", (data) => {
emulatorOutputChannel.appendLine("DEBUG: " + data.toString());
});
// TODO: Utilize streaming api to iniate schema reloads
dataConnectEmulatorDetails.instance.stderr?.on("data", (data) => {
if (data.toString().includes("Finished reloading")) {
this.schemaReload();
} else {
emulatorOutputChannel.appendLine("ERROR: " + data.toString());
}
});
}
}
}

dispose() {
Expand Down

0 comments on commit 85d27c5

Please sign in to comment.