Skip to content

Commit

Permalink
feat(wrangler): Drop worker prefix when creating KV namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
pmiguel committed Jan 14, 2025
1 parent b35265a commit 4d9828e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .changeset/serious-teachers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": major
---

Remove worker name prefix from KV namespace create

BREAKING CHANGE: when running `wrangler kv namespace create <name>`, previously the name of the namespace was automatically prefixed with the worker title, or `worker-` when running outside the context of a worker.
After this change, KV namespaces will no longer get prefixed, and the name used is the name supplied, verbatim.
16 changes: 8 additions & 8 deletions packages/wrangler/src/__tests__/__snapshots__/kv.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{
Expand All @@ -15,7 +15,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace in an environment if configured to do so 1`] = `
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
{
Expand All @@ -29,7 +29,7 @@ Add the following to your configuration file in your kv_namespaces array under [
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a namespace using configured worker name 1`] = `
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{
Expand All @@ -43,7 +43,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.json > should create a preview namespace if configured to do so 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
"🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{
Expand All @@ -57,7 +57,7 @@ Add the following to your configuration file in your kv_namespaces array:
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
Expand All @@ -67,7 +67,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace in an environment if configured to do so 1`] = `
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"customEnv-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
[[kv_namespaces]]
Expand All @@ -77,7 +77,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a namespace using configured worker name 1`] = `
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
"🌀 Creating namespace with title \\"UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
Expand All @@ -87,7 +87,7 @@ id = \\"some-namespace-id\\"
`;

exports[`wrangler > kv namespace > create > wrangler.toml > should create a preview namespace if configured to do so 1`] = `
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
"🌀 Creating namespace with title \\"UnitTestNamespace_preview\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
Expand Down
8 changes: 4 additions & 4 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,23 @@ describe("wrangler", () => {
describe.each(["wrangler.json", "wrangler.toml"])("%s", (configPath) => {
it("should create a namespace", async () => {
writeWranglerConfig({ name: "worker" }, configPath);
mockCreateRequest("worker-UnitTestNamespace");
mockCreateRequest("UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchSnapshot();
});

it("should create a preview namespace if configured to do so", async () => {
writeWranglerConfig({ name: "worker" }, configPath);

mockCreateRequest("worker-UnitTestNamespace_preview");
mockCreateRequest("UnitTestNamespace_preview");
await runWrangler("kv namespace create UnitTestNamespace --preview");
expect(std.out).toMatchSnapshot();
});

it("should create a namespace using configured worker name", async () => {
writeWranglerConfig({ name: "other-worker" }, configPath);

mockCreateRequest("other-worker-UnitTestNamespace");
mockCreateRequest("UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchSnapshot();
});
Expand All @@ -223,7 +223,7 @@ describe("wrangler", () => {
configPath
);

mockCreateRequest("worker-customEnv-UnitTestNamespace");
mockCreateRequest("customEnv-UnitTestNamespace");
await runWrangler(
"kv namespace create UnitTestNamespace --env customEnv"
);
Expand Down
11 changes: 2 additions & 9 deletions packages/wrangler/src/kv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,9 @@ defineCommand({

async handler(args) {
const config = readConfig(args.config, args);
if (!config.name) {
logger.warn(
"No configured name present, using `worker` as a prefix for the title"
);
}

const name = config.name || "worker";
const environment = args.env ? `-${args.env}` : "";
const environment = args.env ? `${args.env}-` : "";
const preview = args.preview ? "_preview" : "";
const title = `${name}${environment}-${args.namespace}${preview}`;
const title = `${environment}${args.namespace}${preview}`;

const accountId = await requireAuth(config);

Expand Down

0 comments on commit 4d9828e

Please sign in to comment.