Skip to content

Fixing issues with init dataconnect #7295

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

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes some cases where `firebase init dataconnect` did not write project files correctly.
2 changes: 1 addition & 1 deletion scripts/publish-vsce.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ echo "Making a $VERSION version of VSCode..."
npm version $VERSION
NEW_VSCODE_VERSION=$(jq -r ".version" package.json)
NEXT_HEADER="## NEXT"
NEW_HEADER="## NEXT \n\n## $NEW_VSCODE_VERSION\n\n- Updated internal firebase-tools dependency to $CLI_VERSION"
NEW_HEADER="## NEXT\n\n## $NEW_VSCODE_VERSION\n\n- Updated internal firebase-tools dependency to $CLI_VERSION"
sed -i -e "s/$NEXT_HEADER/$NEW_HEADER/g" CHANGELOG.md
echo "Made a $VERSION version of VSCode."

Expand Down
29 changes: 29 additions & 0 deletions src/dataconnect/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
}

const serviceNameRegex =
/projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/services\/(?<serviceId>[^\/]+)/;

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 11 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

export function parseServiceName(serviceName: string): serviceName {

Check warning on line 13 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const res = serviceNameRegex.exec(serviceName);
const projectId = res?.groups?.projectId;
const location = res?.groups?.location;
Expand All @@ -18,7 +18,7 @@
if (!projectId || !location || !serviceId) {
throw new FirebaseError(`${serviceName} is not a valid service name`);
}
const toString = () => {

Check warning on line 21 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
return `projects/${projectId}/locations/${location}/services/${serviceId}`;
};
return {
Expand All @@ -38,9 +38,9 @@
}

const connectorNameRegex =
/projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/services\/(?<serviceId>[^\/]+)\/connectors\/(?<connectorId>[^\/]+)/;

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

Check warning on line 41 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unnecessary escape character: \/

export function parseConnectorName(connectorName: string): connectorName {

Check warning on line 43 in src/dataconnect/names.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const res = connectorNameRegex.exec(connectorName);
const projectId = res?.groups?.projectId;
const location = res?.groups?.location;
Expand All @@ -60,3 +60,32 @@
toString,
};
}

interface cloudSQLInstanceName {
projectId: string;
location: string;
instanceId: string;
toString(): string;
}

const cloudSQLInstanceNameRegex =
/projects\/(?<projectId>[^\/]+)\/locations\/(?<location>[^\/]+)\/instances\/(?<instanceId>[^\/]+)/;

export function parseCloudSQLInstanceName(cloudSQLInstanceName: string): cloudSQLInstanceName {
const res = cloudSQLInstanceNameRegex.exec(cloudSQLInstanceName);
const projectId = res?.groups?.projectId;
const location = res?.groups?.location;
const instanceId = res?.groups?.instanceId;
if (!projectId || !location || !instanceId) {
throw new FirebaseError(`${cloudSQLInstanceName} is not a valid cloudSQL instance name`);
}
const toString = () => {
return `projects/${projectId}/locations/${location}/services/${instanceId}`;
};
return {
projectId,
location,
instanceId,
toString,
};
}
20 changes: 12 additions & 8 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ensureApis } from "../../../dataconnect/ensureApis";
import { listLocations, listAllServices, getSchema } from "../../../dataconnect/client";
import { Schema, Service } from "../../../dataconnect/types";
import { DEFAULT_POSTGRES_CONNECTION } from "../emulators";
import { parseServiceName } from "../../../dataconnect/names";
import { parseCloudSQLInstanceName, parseServiceName } from "../../../dataconnect/names";
import { logger } from "../../../logger";

const TEMPLATE_ROOT = resolve(__dirname, "../../../../templates/init/dataconnect/");
Expand Down Expand Up @@ -67,6 +67,10 @@ export async function doSetup(setup: Setup, config: Config): Promise<void> {
const subbedDataconnectYaml = subValues(DATACONNECT_YAML_TEMPLATE, info);
const subbedConnectorYaml = subValues(CONNECTOR_YAML_TEMPLATE, info);

if (!config.has("dataconnect")) {
config.set("dataconnect.source", dir);
config.set("dataconnect.location", info.locationId);
}
await config.askWriteProjectFile(join(dir, "dataconnect.yaml"), subbedDataconnectYaml);
await config.askWriteProjectFile(join(dir, "schema", "schema.gql"), SCHEMA_TEMPLATE);
await config.askWriteProjectFile(
Expand All @@ -78,6 +82,7 @@ export async function doSetup(setup: Setup, config: Config): Promise<void> {
join(dir, info.connectorId, "mutations.gql"),
MUTATIONS_TEMPLATE,
);

if (
setup.projectId &&
(info.isNewInstance || info.isNewDatabase) &&
Expand Down Expand Up @@ -156,8 +161,12 @@ async function promptForService(setup: Setup, info: RequiredInfo): Promise<Requi
info.serviceId = serviceName.serviceId;
info.locationId = serviceName.location;
if (choice.schema) {
info.cloudSqlInstanceId =
choice.schema.primaryDatasource.postgresql?.cloudSql.instance ?? "";
if (choice.schema.primaryDatasource.postgresql?.cloudSql.instance) {
const instanceName = parseCloudSQLInstanceName(
choice.schema.primaryDatasource.postgresql?.cloudSql.instance,
);
info.cloudSqlInstanceId = instanceName.instanceId;
}
info.cloudSqlDatabase = choice.schema.primaryDatasource.postgresql?.database ?? "";
}
}
Expand Down Expand Up @@ -243,11 +252,6 @@ async function promptForDatabase(
config: Config,
info: RequiredInfo,
): Promise<RequiredInfo> {
const dir: string = config.get("dataconnect.source") || "dataconnect";
if (!config.has("dataconnect")) {
config.set("dataconnect.source", dir);
config.set("dataconnect.location", info.locationId);
}
if (!info.isNewInstance && setup.projectId) {
try {
const dbs = await cloudsql.listDatabases(setup.projectId, info.cloudSqlInstanceId);
Expand Down
Loading