Skip to content

Commit a2f695b

Browse files
Hyperdrive dev remote fix (#7783)
* Don't require Hyperdrive local connection string when using wrangler dev --remote * WIP: hyperdrive resource e2e test * Fix linting issue * Adjust e2e tests to use Hyperdrive resource * fix linting issue --------- Co-authored-by: Carmen Popoviciu <[email protected]>
1 parent 43ec094 commit a2f695b

File tree

7 files changed

+104
-3
lines changed

7 files changed

+104
-3
lines changed

.changeset/nervous-jeans-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix to not require local connection string when using Hyperdrive and wrangler dev --remote

.github/workflows/e2e.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ jobs:
4848
env:
4949
CLOUDFLARE_API_TOKEN: ${{ secrets.TEST_CLOUDFLARE_API_TOKEN }}
5050
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.TEST_CLOUDFLARE_ACCOUNT_ID }}
51+
HYPERDRIVE_DATABASE_URL: ${{ secrets.TEST_HYPERDRIVE_DATABASE_URL}}
5152
WRANGLER: node --no-warnings ${{ github.workspace}}/packages/wrangler/bin/wrangler.js
5253
WRANGLER_IMPORT: ${{ github.workspace}}/packages/wrangler/wrangler-dist/cli.js
5354
NODE_OPTIONS: "--max_old_space_size=8192"

packages/wrangler/e2e/dev-with-resources.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,35 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
580580
);
581581
});
582582

583+
it("exposes Hyperdrive bindings", async () => {
584+
const { id } = await helper.hyperdrive(false);
585+
586+
await helper.seed({
587+
"wrangler.toml": dedent`
588+
name = "${workerName}"
589+
main = "src/index.ts"
590+
compatibility_date = "2023-10-25"
591+
592+
[[hyperdrive]]
593+
binding = "HYPERDRIVE"
594+
id = "${id}"
595+
`,
596+
"src/index.ts": dedent`
597+
export default {
598+
async fetch(request, env) {
599+
if (request.url.includes("connect")) {
600+
const conn = env.HYPERDRIVE.connect();
601+
}
602+
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
603+
}
604+
}`,
605+
});
606+
607+
const worker = helper.runLongLived(`wrangler dev ${flags}`);
608+
const { url } = await worker.waitForReady();
609+
await fetch(`${url}/connect`);
610+
});
611+
583612
it.skipIf(!isLocal).fails("exposes Pipelines bindings", async () => {
584613
await helper.seed({
585614
"wrangler.toml": dedent`
@@ -692,7 +721,6 @@ describe.sequential.each(RUNTIMES)("Bindings: $flags", ({ runtime, flags }) => {
692721
});
693722

694723
// TODO(soon): implement E2E tests for other bindings
695-
it.todo("exposes hyperdrive bindings");
696724
it.skipIf(isLocal).todo("exposes send email bindings");
697725
it.skipIf(isLocal).todo("exposes browser bindings");
698726
it.skipIf(isLocal).todo("exposes Workers AI bindings");

packages/wrangler/e2e/dev.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,44 @@ describe("hyperdrive dev tests", () => {
653653
await socketMsgPromise;
654654
});
655655

656+
it("does not require local connection string when running `wrangler dev --remote`", async () => {
657+
const helper = new WranglerE2ETestHelper();
658+
const { id } = await helper.hyperdrive(false);
659+
660+
await helper.seed({
661+
"wrangler.toml": dedent`
662+
name = "${workerName}"
663+
main = "src/index.ts"
664+
compatibility_date = "2023-10-25"
665+
666+
[[hyperdrive]]
667+
binding = "HYPERDRIVE"
668+
id = "${id}"
669+
`,
670+
"src/index.ts": dedent`
671+
export default {
672+
async fetch(request, env) {
673+
if (request.url.includes("connect")) {
674+
const conn = env.HYPERDRIVE.connect();
675+
}
676+
return new Response(env.HYPERDRIVE?.connectionString ?? "no")
677+
}
678+
}`,
679+
"package.json": dedent`
680+
{
681+
"name": "worker",
682+
"version": "0.0.0",
683+
"private": true
684+
}
685+
`,
686+
});
687+
688+
const worker = helper.runLongLived("wrangler dev --remote");
689+
690+
const { url } = await worker.waitForReady();
691+
await fetch(`${url}/connect`);
692+
});
693+
656694
afterEach(() => {
657695
if (server.listening) {
658696
server.close();

packages/wrangler/e2e/helpers/e2e-wrangler-test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,27 @@ export class WranglerE2ETestHelper {
124124

125125
return name;
126126
}
127+
128+
async hyperdrive(isLocal: boolean): Promise<{ id: string; name: string }> {
129+
const name = generateResourceName("hyperdrive");
130+
131+
if (isLocal) {
132+
return { id: crypto.randomUUID(), name };
133+
}
134+
135+
const result = await this.run(
136+
`wrangler hyperdrive create ${name} --connection-string="${process.env.HYPERDRIVE_DATABASE_URL}"`
137+
);
138+
const tomlMatch = /id = "([0-9a-f]{32})"/.exec(result.stdout);
139+
const jsonMatch = /"id": "([0-9a-f]{32})"/.exec(result.stdout);
140+
const match = jsonMatch ?? tomlMatch;
141+
assert(match !== null, `Cannot find ID in ${JSON.stringify(result)}`);
142+
const id = match[1];
143+
144+
onTestFinished(async () => {
145+
await this.run(`wrangler hyperdrive delete ${name}`);
146+
});
147+
148+
return { id, name };
149+
}
127150
}

packages/wrangler/src/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,12 @@ export function getBindings(
10201020
process.env[
10211021
`WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_${hyperdrive.binding}`
10221022
];
1023-
if (!connectionStringFromEnv && !hyperdrive.localConnectionString) {
1023+
// only require a local connection string in the wrangler file or the env if not using dev --remote
1024+
if (
1025+
local &&
1026+
!connectionStringFromEnv &&
1027+
!hyperdrive.localConnectionString
1028+
) {
10241029
throw new UserError(
10251030
`When developing locally, you should use a local Postgres connection string to emulate Hyperdrive functionality. Please setup Postgres locally and set the value of the 'WRANGLER_HYPERDRIVE_LOCAL_CONNECTION_STRING_${hyperdrive.binding}' variable or "${hyperdrive.binding}"'s "localConnectionString" to the Postgres connection string.`
10261031
);

packages/wrangler/turbo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"WRANGLER_DISABLE_EXPERIMENTAL_WARNING",
6060
"WRANGLER_DISABLE_REQUEST_BODY_DRAINING",
6161
"WRANGLER_WORKER_REGISTRY_PORT",
62-
"WRANGLER_API_ENVIRONMENT"
62+
"WRANGLER_API_ENVIRONMENT",
63+
"HYPERDRIVE_DATABASE_URL"
6364
]
6465
},
6566
"test:ci": {

0 commit comments

Comments
 (0)