Skip to content

Commit 2507304

Browse files
authored
Fix bugs when warning users using SQLite in Durable Objects in remote dev (#6699)
1 parent 9b6a74a commit 2507304

File tree

6 files changed

+80
-70
lines changed

6 files changed

+80
-70
lines changed

.changeset/fifty-students-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: Bugs when warning users using SQLite in Durable Objects in remote dev

packages/wrangler/src/api/startDevWorker/ConfigController.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
maskVars,
2020
} from "../../dev";
2121
import { getLocalPersistencePath } from "../../dev/get-local-persistence-path";
22+
import { getClassNamesWhichUseSQLite } from "../../dev/validate-dev-props";
2223
import { UserError } from "../../errors";
2324
import { processExperimentalAssetsArg } from "../../experimental-assets";
2425
import { logger } from "../../logger";
@@ -319,17 +320,17 @@ async function resolveConfig(
319320
);
320321
}
321322

323+
// TODO(do) support remote wrangler dev
324+
const classNamesWhichUseSQLite = getClassNamesWhichUseSQLite(
325+
resolved.migrations
326+
);
322327
if (
323-
resolved.migrations?.some(
324-
(m) =>
325-
Array.isArray(m.new_sqlite_classes) && m.new_sqlite_classes.length > 0
326-
) &&
327-
resolved.dev?.remote
328+
resolved.dev.remote &&
329+
Array.from(classNamesWhichUseSQLite.values()).some((v) => v)
328330
) {
329-
throw new UserError(
330-
"SQLite in Durable Objects is only supported in local mode."
331-
);
331+
logger.warn("SQLite in Durable Objects is only supported in local mode.");
332332
}
333+
333334
return resolved;
334335
}
335336
export class ConfigController extends Controller<ConfigControllerEventMap> {

packages/wrangler/src/dev.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -618,18 +618,6 @@ export async function startDev(args: StartDevOptions) {
618618
);
619619
}
620620

621-
if (
622-
args.remote &&
623-
config.migrations?.some(
624-
(m) =>
625-
Array.isArray(m.new_sqlite_classes) && m.new_sqlite_classes.length > 0
626-
)
627-
) {
628-
throw new UserError(
629-
"SQLite in Durable Objects is only supported in local mode."
630-
);
631-
}
632-
633621
const projectRoot = configPath && path.dirname(configPath);
634622

635623
const devEnv = new DevEnv();

packages/wrangler/src/dev/dev.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import { openInspector } from "./inspect";
3232
import { Local, maybeRegisterLocalWorker } from "./local";
3333
import { Remote } from "./remote";
3434
import { useEsbuild } from "./use-esbuild";
35-
import { validateDevProps } from "./validate-dev-props";
35+
import {
36+
getClassNamesWhichUseSQLite,
37+
validateDevProps,
38+
} from "./validate-dev-props";
3639
import type {
3740
DevEnv,
3841
ProxyData,
@@ -634,6 +637,17 @@ function DevSession(props: DevSessionProps) {
634637
);
635638
}
636639

640+
// TODO(do) support remote wrangler dev
641+
const classNamesWhichUseSQLite = getClassNamesWhichUseSQLite(
642+
props.migrations
643+
);
644+
if (
645+
!props.local &&
646+
Array.from(classNamesWhichUseSQLite.values()).some((v) => v)
647+
) {
648+
logger.warn("SQLite in Durable Objects is only supported in local mode.");
649+
}
650+
637651
// this won't be called with props.experimentalDevEnv because useWorker is guarded with the same flag
638652
const announceAndOnReady: typeof props.onReady = async (
639653
finalIp,

packages/wrangler/src/dev/miniflare.ts

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { UserError } from "../errors";
2222
import { logger } from "../logger";
2323
import { getSourceMappedString } from "../sourcemap";
2424
import { updateCheck } from "../update-check";
25+
import { getClassNamesWhichUseSQLite } from "./validate-dev-props";
2526
import type { ServiceFetch } from "../api";
2627
import type { Config } from "../config";
2728
import type {
@@ -676,55 +677,6 @@ export function buildMiniflareBindingOptions(config: MiniflareBindingsConfig): {
676677
};
677678
}
678679

679-
function getClassNamesWhichUseSQLite(
680-
migrations: Config["migrations"] | undefined
681-
) {
682-
const classNameToUseSQLite = new Map<string, boolean>();
683-
(migrations || []).forEach((migration) => {
684-
migration.deleted_classes?.forEach((deleted_class) => {
685-
if (!classNameToUseSQLite.delete(deleted_class)) {
686-
throw new UserError(
687-
`Cannot apply deleted_classes migration to non-existent class ${deleted_class}`
688-
);
689-
}
690-
});
691-
692-
migration.renamed_classes?.forEach(({ from, to }) => {
693-
const useSQLite = classNameToUseSQLite.get(from);
694-
if (useSQLite === undefined) {
695-
throw new UserError(
696-
`Cannot apply renamed_classes migration to non-existent class ${from}`
697-
);
698-
} else {
699-
classNameToUseSQLite.delete(from);
700-
classNameToUseSQLite.set(to, useSQLite);
701-
}
702-
});
703-
704-
migration.new_classes?.forEach((new_class) => {
705-
if (classNameToUseSQLite.has(new_class)) {
706-
throw new UserError(
707-
`Cannot apply new_classes migration to existing class ${new_class}`
708-
);
709-
} else {
710-
classNameToUseSQLite.set(new_class, false);
711-
}
712-
});
713-
714-
migration.new_sqlite_classes?.forEach((new_class) => {
715-
if (classNameToUseSQLite.has(new_class)) {
716-
throw new UserError(
717-
`Cannot apply new_sqlite_classes migration to existing class ${new_class}`
718-
);
719-
} else {
720-
classNameToUseSQLite.set(new_class, true);
721-
}
722-
});
723-
});
724-
725-
return classNameToUseSQLite;
726-
}
727-
728680
type PickTemplate<T, K extends string> = {
729681
[P in keyof T & K]: T[P];
730682
};

packages/wrangler/src/dev/validate-dev-props.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { UserError } from "../errors";
2+
import type { Config } from "../config";
23
import type { DevProps } from "./dev";
34

45
export function validateDevProps(props: Omit<DevProps, "host">) {
@@ -30,3 +31,52 @@ export function validateDevProps(props: Omit<DevProps, "host">) {
3031
);
3132
}
3233
}
34+
35+
export function getClassNamesWhichUseSQLite(
36+
migrations: Config["migrations"] | undefined
37+
) {
38+
const classNamesWhichUseSQLite = new Map<string, boolean>();
39+
(migrations || []).forEach((migration) => {
40+
migration.deleted_classes?.forEach((deleted_class) => {
41+
if (!classNamesWhichUseSQLite.delete(deleted_class)) {
42+
throw new UserError(
43+
`Cannot apply deleted_classes migration to non-existent class ${deleted_class}`
44+
);
45+
}
46+
});
47+
48+
migration.renamed_classes?.forEach(({ from, to }) => {
49+
const useSQLite = classNamesWhichUseSQLite.get(from);
50+
if (useSQLite === undefined) {
51+
throw new UserError(
52+
`Cannot apply renamed_classes migration to non-existent class ${from}`
53+
);
54+
} else {
55+
classNamesWhichUseSQLite.delete(from);
56+
classNamesWhichUseSQLite.set(to, useSQLite);
57+
}
58+
});
59+
60+
migration.new_classes?.forEach((new_class) => {
61+
if (classNamesWhichUseSQLite.has(new_class)) {
62+
throw new UserError(
63+
`Cannot apply new_classes migration to existing class ${new_class}`
64+
);
65+
} else {
66+
classNamesWhichUseSQLite.set(new_class, false);
67+
}
68+
});
69+
70+
migration.new_sqlite_classes?.forEach((new_class) => {
71+
if (classNamesWhichUseSQLite.has(new_class)) {
72+
throw new UserError(
73+
`Cannot apply new_sqlite_classes migration to existing class ${new_class}`
74+
);
75+
} else {
76+
classNamesWhichUseSQLite.set(new_class, true);
77+
}
78+
});
79+
});
80+
81+
return classNamesWhichUseSQLite;
82+
}

0 commit comments

Comments
 (0)