Skip to content

Commit

Permalink
Address PR feedback and add another testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlemilio committed Mar 11, 2024
1 parent 0e82e17 commit 787e9a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .changeset/twenty-laws-mix.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"wrangler": minor
---

URL decode components of the Hyperdrive config connection string
feature: URL decode components of the Hyperdrive config connection string
2 changes: 1 addition & 1 deletion packages/miniflare/src/plugins/hyperdrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const HYPERDRIVE_PLUGIN: Plugin<typeof HyperdriveInputOptionsSchema> = {
name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`,
},
database: decodeURIComponent(database),
user: url.username,
user: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
scheme,
},
Expand Down
24 changes: 24 additions & 0 deletions packages/wrangler/src/__tests__/hyperdrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,30 @@ describe("hyperdrive commands", () => {
`);
});

it("should handle disabling caching for a hyperdrive config", async () => {
mockHyperdriveRequest();
await runWrangler(
"hyperdrive update 7a040c1eee714e91a30ea6707a2d125c --caching-disabled=true"
);
expect(std.out).toMatchInlineSnapshot(`
"🚧 Updating '7a040c1eee714e91a30ea6707a2d125c'
✅ Updated 7a040c1eee714e91a30ea6707a2d125c Hyperdrive config
{
\\"id\\": \\"7a040c1eee714e91a30ea6707a2d125c\\",
\\"name\\": \\"test123\\",
\\"origin\\": {
\\"host\\": \\"foo.us-east-2.aws.neon.tech\\",
\\"port\\": 5432,
\\"database\\": \\"neondb\\",
\\"user\\": \\"test\\"
},
\\"caching\\": {
\\"disabled\\": true
}
}"
`);
});

it("should handle updating a hyperdrive config's name", async () => {
mockHyperdriveRequest();
await runWrangler(
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/hyperdrive/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function handler(
port: parseInt(url.port),
scheme: url.protocol.replace(":", ""),
database: decodeURIComponent(url.pathname.replace("/", "")),
user: url.username,
user: decodeURIComponent(url.username),
password: decodeURIComponent(url.password),
},
caching: {
Expand All @@ -103,4 +103,4 @@ export async function handler(
JSON.stringify(database, null, 2)
);
}
}
}
38 changes: 18 additions & 20 deletions packages/wrangler/src/hyperdrive/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ export function options(yargs: CommonYargsArgv) {
}

const requiredOriginOptions = [
"origin-host",
"origin-port",
"originHost",
"originPort",
"database",
"origin-user",
"origin-password",
];
"originUser",
"originPassword",
] as const;

function camelToKebab(str: string): string {
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
}

function isOptionSet<T extends object>(args: T, key: keyof T): boolean {
return key in args && args[key] !== undefined;
Expand All @@ -78,15 +82,15 @@ export async function handler(
) {
// check if all or none of the required origin fields are set, since we don't allow partial updates of the origin
const allOriginFieldsSet = requiredOriginOptions.every((field) =>
isOptionSet(args, field as keyof typeof options)
isOptionSet(args, field)
);
const noOriginFieldSet = requiredOriginOptions.every(
(field) => !isOptionSet(args, field as keyof typeof options)
(field) => !isOptionSet(args, field)
);

if (!allOriginFieldsSet && !noOriginFieldSet) {
throw new Error(
`When updating the origin, all of the following must be set: ${requiredOriginOptions.join(
`When updating the origin, all of the following must be set: ${requiredOriginOptions.map(option => camelToKebab(option)).join(
", "
)}`
);
Expand All @@ -104,26 +108,20 @@ export async function handler(

if (allOriginFieldsSet) {
database.origin = {
scheme: args.originScheme ?? "postgresql",
host: args.originHost,
port: args.originPort,
database: args.database,
user: args.originUser,
password: args.originPassword,
};
if (args.originScheme !== undefined) {
database.origin.scheme = args.originScheme;
} else {
database.origin.scheme = "postgresql"; // setting default if not passed
}
}

if (args.cachingDisabled || args.maxAge || args.swr) {
database.caching = {
disabled: args.cachingDisabled,
maxAge: args.maxAge,
staleWhileRevalidate: args.swr,
};
}
database.caching = {
disabled: args.cachingDisabled,
maxAge: args.maxAge,
staleWhileRevalidate: args.swr,
};

const updated = await patchConfig(config, args.id, database);
logger.log(
Expand Down

0 comments on commit 787e9a8

Please sign in to comment.