Skip to content

Commit

Permalink
refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Jan 24, 2025
1 parent 774721e commit e123899
Show file tree
Hide file tree
Showing 5 changed files with 451 additions and 332 deletions.
4 changes: 4 additions & 0 deletions fixtures/worker-ts/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
name = "worker-ts"
main = "src/index.ts"
compatibility_date = "2023-05-04"

[[d1_databases]]
binding = "HELLO_D2"
database_name = "lolkbhj"
116 changes: 67 additions & 49 deletions packages/wrangler/src/__tests__/provision.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { http, HttpResponse } from "msw";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import {
clearDialogs,
mockConfirm,
mockPrompt,
mockSelect,
} from "./helpers/mock-dialogs";
import { clearDialogs, mockPrompt, mockSelect } from "./helpers/mock-dialogs";
import { useMockIsTTY } from "./helpers/mock-istty";
import {
mockCreateKVNamespace,
Expand Down Expand Up @@ -190,19 +185,13 @@ describe("--x-provision", () => {
- R2
Provisioning KV (KV Namespace)...
✨ KV provisioned with test-kv
--------------------------------------
✨ KV provisioned 🎉
Provisioning D1 (D1 Database)...
✨ D1 provisioned with db-name
--------------------------------------
✨ D1 provisioned 🎉
Provisioning R2 (R2 Bucket)...
✨ R2 provisioned with existing-bucket-name
--------------------------------------
✨ R2 provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down Expand Up @@ -315,19 +304,13 @@ describe("--x-provision", () => {
- R2
Provisioning KV (KV Namespace)...
✨ KV provisioned with test-kv-1
--------------------------------------
✨ KV provisioned 🎉
Provisioning D1 (D1 Database)...
✨ D1 provisioned with test-d1-1
--------------------------------------
✨ D1 provisioned 🎉
Provisioning R2 (R2 Bucket)...
✨ R2 provisioned with existing-bucket-1
--------------------------------------
✨ R2 provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down Expand Up @@ -451,21 +434,15 @@ describe("--x-provision", () => {
Provisioning KV (KV Namespace)...
🌀 Creating new KV Namespace \\"new-kv\\"...
✨ KV provisioned with new-kv
--------------------------------------
✨ KV provisioned 🎉
Provisioning D1 (D1 Database)...
🌀 Creating new D1 Database \\"new-d1\\"...
✨ D1 provisioned with new-d1
--------------------------------------
✨ D1 provisioned 🎉
Provisioning R2 (R2 Bucket)...
🌀 Creating new R2 Bucket \\"new-r2\\"...
✨ R2 provisioned with new-r2
--------------------------------------
✨ R2 provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down Expand Up @@ -532,11 +509,8 @@ describe("--x-provision", () => {
Provisioning D1 (D1 Database)...
Resource name found in config: prefilled-d1-name
No pre-existing resource found with that name
🌀 Creating new D1 Database \\"prefilled-d1-name\\"...
✨ D1 provisioned with prefilled-d1-name
--------------------------------------
✨ D1 provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down Expand Up @@ -653,11 +627,8 @@ describe("--x-provision", () => {
Provisioning D1 (D1 Database)...
Resource name found in config: new-d1-name
No pre-existing resource found with that name
🌀 Creating new D1 Database \\"new-d1-name\\"...
✨ D1 provisioned with new-d1-name
--------------------------------------
✨ D1 provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down Expand Up @@ -730,9 +701,7 @@ describe("--x-provision", () => {
Provisioning BUCKET (R2 Bucket)...
Resource name found in config: prefilled-r2-name
🌀 Creating new R2 Bucket \\"prefilled-r2-name\\"...
✨ BUCKET provisioned with prefilled-r2-name
--------------------------------------
✨ BUCKET provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand All @@ -749,8 +718,7 @@ describe("--x-provision", () => {
expect(std.warn).toMatchInlineSnapshot(`""`);
});

// to maintain current behaviour
it("wont prompt to provision if an r2 bucket name belongs to an existing bucket", async () => {
it("won't prompt to provision if an r2 bucket name belongs to an existing bucket", async () => {
writeWranglerConfig({
main: "index.js",
r2_buckets: [
Expand Down Expand Up @@ -804,6 +772,58 @@ describe("--x-provision", () => {
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("won't prompt to provision if a D1 database name belongs to an existing database", async () => {
writeWranglerConfig({
main: "index.js",
d1_databases: [
{
binding: "DB_NAME",
database_name: "existing-db-name",
},
],
});
mockGetSettings();

msw.use(
http.get("*/accounts/:accountId/d1/database", async () => {
return HttpResponse.json(
createFetchResult([
{
name: "existing-db-name",
uuid: "existing-d1-id",
},
])
);
})
);

mockUploadWorkerRequest({
expectedBindings: [
{
name: "DB_NAME",
type: "d1",
id: "existing-d1-id",
},
],
});

await runWrangler("deploy --x-provision");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- D1 Databases:
- DB_NAME: existing-db-name (existing-d1-id)
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

// because buckets with the same name can exist in different jurisdictions
it("will provision if the jurisdiction changes", async () => {
writeWranglerConfig({
Expand Down Expand Up @@ -871,9 +891,7 @@ describe("--x-provision", () => {
Provisioning BUCKET (R2 Bucket)...
Resource name found in config: existing-bucket-name
🌀 Creating new R2 Bucket \\"existing-bucket-name\\"...
✨ BUCKET provisioned with existing-bucket-name
--------------------------------------
✨ BUCKET provisioned 🎉
🎉 All resources provisioned, continuing with deployment...
Expand Down
16 changes: 8 additions & 8 deletions packages/wrangler/src/api/startDevWorker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ export type Binding =
| ({ type: "workflow" } & BindingOmit<CfWorkflow>)
| ({ type: "queue" } & BindingOmit<CfQueue>)
| ({ type: "r2_bucket" } & BindingOmit<CfR2Bucket>)
| ({ type: "d1" } & Omit<CfD1Database, "binding">)
| ({ type: "vectorize" } & Omit<CfVectorize, "binding">)
| ({ type: "hyperdrive" } & Omit<CfHyperdrive, "binding">)
| ({ type: "service" } & Omit<CfService, "binding">)
| ({ type: "d1" } & BindingOmit<CfD1Database>)
| ({ type: "vectorize" } & BindingOmit<CfVectorize>)
| ({ type: "hyperdrive" } & BindingOmit<CfHyperdrive>)
| ({ type: "service" } & BindingOmit<CfService>)
| { type: "fetcher"; fetcher: ServiceFetch }
| ({ type: "analytics_engine" } & Omit<CfAnalyticsEngineDataset, "binding">)
| ({ type: "dispatch_namespace" } & Omit<CfDispatchNamespace, "binding">)
| ({ type: "mtls_certificate" } & Omit<CfMTlsCertificate, "binding">)
| ({ type: "pipeline" } & Omit<CfPipeline, "binding">)
| ({ type: "analytics_engine" } & BindingOmit<CfAnalyticsEngineDataset>)
| ({ type: "dispatch_namespace" } & BindingOmit<CfDispatchNamespace>)
| ({ type: "mtls_certificate" } & BindingOmit<CfMTlsCertificate>)
| ({ type: "pipeline" } & BindingOmit<CfPipeline>)
| ({ type: "logfwdr" } & NameOmit<CfLogfwdrBinding>)
| { type: `unsafe_${string}` }
| { type: "assets" };
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/d1/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const getDatabaseByNameOrBinding = async (
return dbFromConfig;
}

const allDBs = await listDatabases(accountId);
const allDBs = await listDatabases(accountId, true);
const matchingDB = allDBs.find((db) => db.name === name);
if (!matchingDB) {
throw new UserError(`Couldn't find DB with name '${name}'`);
Expand Down
Loading

0 comments on commit e123899

Please sign in to comment.