diff --git a/.changeset/proud-fireants-report.md b/.changeset/proud-fireants-report.md new file mode 100644 index 000000000000..c8540fa281c7 --- /dev/null +++ b/.changeset/proud-fireants-report.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +fix: refactor d1's time-travel compatibility check diff --git a/packages/wrangler/src/__tests__/d1/timeTravel.test.ts b/packages/wrangler/src/__tests__/d1/timeTravel.test.ts index 1d1e0311e92f..d0a554a84e93 100644 --- a/packages/wrangler/src/__tests__/d1/timeTravel.test.ts +++ b/packages/wrangler/src/__tests__/d1/timeTravel.test.ts @@ -1,10 +1,20 @@ +import { rest } from "msw"; +import { throwIfDatabaseIsAlpha } from "../../d1/timeTravel/utils"; +import { mockAccountId, mockApiToken } from "../helpers/mock-account-id"; import { mockConsoleMethods } from "../helpers/mock-console"; import { useMockIsTTY } from "../helpers/mock-istty"; +import { mockGetMemberships, mockOAuthFlow } from "../helpers/mock-oauth-flow"; +import { msw } from "../helpers/msw"; +import { runInTempDir } from "../helpers/run-in-tmp"; import { runWrangler } from "../helpers/run-wrangler"; import writeWranglerToml from "../helpers/write-wrangler-toml"; describe("time-travel", () => { mockConsoleMethods(); + mockAccountId({ accountId: null }); + mockApiToken(); + runInTempDir(); + const { mockOAuthServerCallback } = mockOAuthFlow(); const { setIsTTY } = useMockIsTTY(); describe("restore", () => { @@ -25,4 +35,88 @@ describe("time-travel", () => { ); }); }); + + describe("throwIfDatabaseIsAlpha", () => { + it("should throw for alpha dbs", async () => { + writeWranglerToml({ + d1_databases: [ + { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, + ], + }); + mockOAuthServerCallback(); + mockGetMemberships([ + { id: "IG-88", account: { id: "1701", name: "enterprise" } }, + ]); + msw.use( + rest.get( + "*/accounts/:accountId/d1/database/*", + async (_req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + result: { + uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06", + name: "northwind", + created_at: "2023-05-23T08:33:54.590Z", + version: "alpha", + num_tables: 13, + file_size: 33067008, + running_in_region: "WEUR", + }, + success: true, + errors: [], + messages: [], + }) + ); + } + ) + ); + await expect( + throwIfDatabaseIsAlpha("1701", "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06") + ).rejects.toThrowError( + "Time travel is not available for alpha D1 databases. You will need to migrate to a new database for access to this feature." + ); + }); + it("should not throw for non-alpha dbs", async () => { + writeWranglerToml({ + d1_databases: [ + { binding: "DATABASE", database_name: "db", database_id: "xxxx" }, + ], + }); + mockOAuthServerCallback(); + mockGetMemberships([ + { id: "IG-88", account: { id: "1701", name: "enterprise" } }, + ]); + msw.use( + rest.get( + "*/accounts/:accountId/d1/database/*", + async (_req, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + result: { + uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06", + name: "northwind", + created_at: "2023-05-23T08:33:54.590Z", + version: "production", + num_tables: 13, + file_size: 33067008, + running_in_region: "WEUR", + }, + success: true, + errors: [], + messages: [], + }) + ); + } + ) + ); + const result = await throwIfDatabaseIsAlpha( + "1701", + "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06" + ); + //since the function throws if the db is alpha + expect(result).toBeUndefined(); + }); + }); }); diff --git a/packages/wrangler/src/d1/timeTravel/info.ts b/packages/wrangler/src/d1/timeTravel/info.ts index b34165b27595..62de22e8b0c2 100644 --- a/packages/wrangler/src/d1/timeTravel/info.ts +++ b/packages/wrangler/src/d1/timeTravel/info.ts @@ -4,10 +4,7 @@ import { logger } from "../../logger"; import { requireAuth } from "../../user"; import { Database } from "../options"; import { getDatabaseByNameOrBinding } from "../utils"; -import { - checkIfDatabaseIsExperimental, - getBookmarkIdFromTimestamp, -} from "./utils"; +import { getBookmarkIdFromTimestamp, throwIfDatabaseIsAlpha } from "./utils"; import type { CommonYargsArgv, StrictYargsOptionsToInterface, @@ -34,7 +31,7 @@ export const InfoHandler = withConfig( // bookmark const accountId = await requireAuth(config); const db = await getDatabaseByNameOrBinding(config, accountId, database); - await checkIfDatabaseIsExperimental(accountId, db.uuid); + await throwIfDatabaseIsAlpha(accountId, db.uuid); const result = await getBookmarkIdFromTimestamp( accountId, db.uuid, diff --git a/packages/wrangler/src/d1/timeTravel/restore.ts b/packages/wrangler/src/d1/timeTravel/restore.ts index efc52856fa6f..2cb33c489dd9 100644 --- a/packages/wrangler/src/d1/timeTravel/restore.ts +++ b/packages/wrangler/src/d1/timeTravel/restore.ts @@ -7,10 +7,7 @@ import { logger } from "../../logger"; import { requireAuth } from "../../user"; import { Database } from "../options"; import { getDatabaseByNameOrBinding } from "../utils"; -import { - checkIfDatabaseIsExperimental, - getBookmarkIdFromTimestamp, -} from "./utils"; +import { getBookmarkIdFromTimestamp, throwIfDatabaseIsAlpha } from "./utils"; import type { CommonYargsArgv, StrictYargsOptionsToInterface, @@ -56,7 +53,7 @@ export const RestoreHandler = withConfig( // bookmark const accountId = await requireAuth(config); const db = await getDatabaseByNameOrBinding(config, accountId, database); - await checkIfDatabaseIsExperimental(accountId, db.uuid); + await throwIfDatabaseIsAlpha(accountId, db.uuid); const searchParams = new URLSearchParams(); if (timestamp) { diff --git a/packages/wrangler/src/d1/timeTravel/utils.ts b/packages/wrangler/src/d1/timeTravel/utils.ts index 85e512611844..5c76b24c0d29 100644 --- a/packages/wrangler/src/d1/timeTravel/utils.ts +++ b/packages/wrangler/src/d1/timeTravel/utils.ts @@ -32,14 +32,14 @@ export const getBookmarkIdFromTimestamp = async ( return bookmarkResult; }; -export const checkIfDatabaseIsExperimental = async ( +export const throwIfDatabaseIsAlpha = async ( accountId: string, databaseId: string ): Promise => { const dbInfo = await getDatabaseInfoFromId(accountId, databaseId); - if (dbInfo.version !== "beta") { + if (dbInfo.version === "alpha") { throw new UserError( - "Time travel is only available for D1 databases created with the --experimental-backend flag" + "Time travel is not available for alpha D1 databases. You will need to migrate to a new database for access to this feature." ); } };