Skip to content

Commit

Permalink
hyperdrive: Default to port 5432 if not specified (#4069)
Browse files Browse the repository at this point in the history
And also remove an unneeded protocol !== "" check while I'm here.
  • Loading branch information
a-robinson authored Oct 9, 2023
1 parent f880a00 commit f4d2891
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-trains-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Default new Hyperdrive configs for PostgreSQL databases to port 5432 if the port is not specified
70 changes: 49 additions & 21 deletions packages/wrangler/src/__tests__/hyperdrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,31 @@ describe("hyperdrive commands", () => {
it("should handle creating a hyperdrive config", async () => {
mockHyperdriveRequest();
await runWrangler(
"hyperdrive create test123 --connection-string='postgresql://test:password@foo.us-east-2.aws.neon.tech:5432/neondb'"
"hyperdrive create test123 --connection-string='postgresql://test:password@foo.us-east-2.aws.neon.tech:12345/neondb'"
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
✅ Created new Hyperdrive config
{
\\"id\\": \\"7a040c1eee714e91a30ea6707a2d125c\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"foo.us-east-2.aws.neon.tech\\",
\\"port\\": 12345,
\\"database\\": \\"neondb\\",
\\"user\\": \\"test\\"
},
\\"caching\\": {
\\"disabled\\": false
}
}"
`);
});

it("should handle creating a hyperdrive config for postgres without a port specified", async () => {
mockHyperdriveRequest();
await runWrangler(
"hyperdrive create test123 --connection-string='postgresql://test:password@foo.us-east-2.aws.neon.tech/neondb'"
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Creating 'test123'
Expand Down Expand Up @@ -201,28 +225,32 @@ function mockHyperdriveRequest() {
);
}
),
rest.post("*/accounts/:accountId/hyperdrive/configs", (req, res, ctx) => {
return res.once(
ctx.json(
createFetchResult(
{
id: "7a040c1eee714e91a30ea6707a2d125c",
name: "test123",
origin: {
host: "foo.us-east-2.aws.neon.tech",
port: 5432,
database: "neondb",
user: "test",
},
caching: {
disabled: false,
rest.post(
"*/accounts/:accountId/hyperdrive/configs",
async (req, res, ctx) => {
const reqBody = await req.json();
return res.once(
ctx.json(
createFetchResult(
{
id: "7a040c1eee714e91a30ea6707a2d125c",
name: "test123",
origin: {
host: "foo.us-east-2.aws.neon.tech",
port: reqBody.origin.port,
database: "neondb",
user: "test",
},
caching: {
disabled: false,
},
},
},
true
true
)
)
)
);
}),
);
}
),
rest.delete(
"*/accounts/:accountId/hyperdrive/configs/7a040c1eee714e91a30ea6707a2d125c",
(req, res, ctx) => {
Expand Down
13 changes: 8 additions & 5 deletions packages/wrangler/src/hyperdrive/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ export async function handler(

const url = new URL(args.connectionString);

if (
url.port === "" &&
(url.protocol == "postgresql:" || url.protocol == "postgres:")
) {
url.port = "5432";
}

if (url.protocol === "") {
logger.log("You must specify the database protocol - e.g. 'postgresql'.");
} else if (
url.protocol !== "postgresql:" &&
url.protocol !== "postgres:" &&
url.protocol !== ""
) {
} else if (url.protocol !== "postgresql:" && url.protocol !== "postgres:") {
logger.log(
"Only PostgreSQL or PostgreSQL compatible databases are currently supported."
);
Expand Down

0 comments on commit f4d2891

Please sign in to comment.