Skip to content

Commit

Permalink
Add new_sqlite_classes migration for Durable Objects (#6606)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshthoward committed Sep 5, 2024
1 parent 1a4b4ba commit 5ea86ca
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,7 @@ describe("normalizeAndValidateConfig()", () => {
{
tag: "TAG",
new_classes: ["CLASS_1", "CLASS_2"],
new_sqlite_classes: ["CLASS_1", "CLASS_2"],
renamed_classes: [
{
from: "FROM_CLASS",
Expand Down Expand Up @@ -1702,6 +1703,7 @@ describe("normalizeAndValidateConfig()", () => {
{
tag: 111,
new_classes: [222, 333],
new_sqlite_classes: [222, 333],
renamed_classes: [
{
from: 444,
Expand All @@ -1726,6 +1728,8 @@ describe("normalizeAndValidateConfig()", () => {
- Expected \\"migrations[0].tag\\" to be of type string but got 111.
- Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
- Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
- Expected \\"migrations[0].new_sqlite_classes.[0]\\" to be of type string but got 222.
- Expected \\"migrations[0].new_sqlite_classes.[1]\\" to be of type string but got 333.
- Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
- Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
- Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
Expand Down Expand Up @@ -4456,6 +4460,7 @@ describe("normalizeAndValidateConfig()", () => {
{
tag: 111,
new_classes: [222, 333],
new_sqlite_classes: [222, 333],
renamed_classes: [
{
from: 444,
Expand All @@ -4482,6 +4487,8 @@ describe("normalizeAndValidateConfig()", () => {
- Expected \\"migrations[0].tag\\" to be of type string but got 111.
- Expected \\"migrations[0].new_classes.[0]\\" to be of type string but got 222.
- Expected \\"migrations[0].new_classes.[1]\\" to be of type string but got 333.
- Expected \\"migrations[0].new_sqlite_classes.[0]\\" to be of type string but got 222.
- Expected \\"migrations[0].new_sqlite_classes.[1]\\" to be of type string but got 333.
- Expected \\"migrations[0].renamed_classes\\" to be an array of \\"{from: string, to: string}\\" objects but got [{\\"from\\":444,\\"to\\":555}].
- Expected \\"migrations[0].deleted_classes.[0]\\" to be of type string but got 666.
- Expected \\"migrations[0].deleted_classes.[1]\\" to be of type string but got 777."
Expand Down
48 changes: 48 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7415,6 +7415,54 @@ addEventListener('fetch', event => {});`
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("should support durable object bindings to SQLite classes", async () => {
writeWranglerToml({
durable_objects: {
bindings: [
{
name: "EXAMPLE_DO_BINDING",
class_name: "ExampleDurableObject",
},
],
},
migrations: [
{ tag: "v1", new_sqlite_classes: ["ExampleDurableObject"] },
],
});
fs.writeFileSync(
"index.js",
`export class ExampleDurableObject {}; export default{};`
);
mockSubDomainRequest();
mockLegacyScriptData({
scripts: [{ id: "test-name", migration_tag: "v1" }],
});
mockUploadWorkerRequest({
expectedBindings: [
{
class_name: "ExampleDurableObject",
name: "EXAMPLE_DO_BINDING",
type: "durable_object_namespace",
},
],
});

await runWrangler("deploy index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- Durable Objects:
- EXAMPLE_DO_BINDING: ExampleDurableObject
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(`""`);
});

it("should support service-workers binding to external durable objects", async () => {
writeWranglerToml({
durable_objects: {
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ interface EnvironmentInheritable {
tag: string;
/** The new Durable Objects being defined. */
new_classes?: string[];
/** The new SQLite Durable Objects being defined. */
new_sqlite_classes?: string[];
/** The Durable Objects being renamed. */
renamed_classes?: {
from: string;
Expand Down
18 changes: 16 additions & 2 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3144,8 +3144,14 @@ const validateMigrations: ValidatorFn = (diagnostics, field, value) => {

let valid = true;
for (let i = 0; i < rawMigrations.length; i++) {
const { tag, new_classes, renamed_classes, deleted_classes, ...rest } =
rawMigrations[i];
const {
tag,
new_classes,
new_sqlite_classes,
renamed_classes,
deleted_classes,
...rest
} = rawMigrations[i];

valid =
validateAdditionalProperties(
Expand All @@ -3172,6 +3178,14 @@ const validateMigrations: ValidatorFn = (diagnostics, field, value) => {
"string"
) && valid;

valid =
validateOptionalTypedArray(
diagnostics,
`migrations[${i}].new_sqlite_classes`,
new_sqlite_classes,
"string"
) && valid;

if (renamed_classes !== undefined) {
if (!Array.isArray(renamed_classes)) {
diagnostics.errors.push(
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/deployment-bundle/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export interface CfDurableObjectMigrations {
new_tag: string;
steps: {
new_classes?: string[];
new_sqlite_classes?: string[];
renamed_classes?: {
from: string;
to: string;
Expand Down

0 comments on commit 5ea86ca

Please sign in to comment.