Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-fireants-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: refactor d1's time-travel compatibility check
94 changes: 94 additions & 0 deletions packages/wrangler/src/__tests__/d1/timeTravel.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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();
});
});
});
7 changes: 2 additions & 5 deletions packages/wrangler/src/d1/timeTravel/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,7 +31,7 @@ export const InfoHandler = withConfig<HandlerOptions>(
// 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,
Expand Down
7 changes: 2 additions & 5 deletions packages/wrangler/src/d1/timeTravel/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,7 +53,7 @@ export const RestoreHandler = withConfig<HandlerOptions>(
// 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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/wrangler/src/d1/timeTravel/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ export const getBookmarkIdFromTimestamp = async (
return bookmarkResult;
};

export const checkIfDatabaseIsExperimental = async (
export const throwIfDatabaseIsAlpha = async (
accountId: string,
databaseId: string
): Promise<void> => {
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."
);
}
};
Expand Down