The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | /** |
| 6 | * The version of how Domain models lay out their Durable Objects. Bump it by |
| 7 | * hand with every change that would otherwise lose deployed data, and add the |
| 8 | * step that carries that data over to {@link migrate}. |
| 9 | * |
| 10 | * A Worker records the version it was deployed with in its |
| 11 | * `alchemy:domain:v<N>` tag. A Worker without the tag was deployed before |
| 12 | * versions existed: version `undefined`. |
| 13 | */ |
| 14 | export const DOMAIN_VERSION = 0; |
| 15 | |
| 16 | /** |
| 17 | * The Domain version a Worker's models are laid out with: always |
| 18 | * {@link DOMAIN_VERSION}. Tests set `undefined` to deploy the layout from |
| 19 | * before versions existed, and check that the next deploy migrates it. |
| 20 | * |
| 21 | * @internal |
| 22 | */ |
| 23 | export const DeployVersion = Context.Reference<number | undefined>("alchemy/Domain/DeployVersion", { |
| 24 | defaultValue: () => DOMAIN_VERSION, |
| 25 | }); |
| 26 | |
| 27 | /** A Durable Object class a Domain model hosts, as the Worker deploy sees it. */ |
| 28 | export interface DomainClass { |
| 29 | /** The model that hosts the class. */ |
| 30 | readonly model: string; |
| 31 | /** The class name in this version, e.g. `Billing.AccountAggregate`. */ |
| 32 | readonly className: string; |
| 33 | /** The class name before Domain version 0, e.g. `AccountAggregate`. */ |
| 34 | readonly legacyName: string; |
| 35 | /** The model keeps the class's data when models shared it before version 0. */ |
| 36 | readonly legacyData: boolean; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Models in one Worker shared a Durable Object class before Domain version 0, |
| 41 | * and none, or more than one, was chosen to keep its data. |
| 42 | */ |
| 43 | export class AmbiguousDomainLegacyData extends Data.TaggedError("AmbiguousDomainLegacyData")<{ |
| 44 | readonly scriptName: string; |
| 45 | readonly legacyName: string; |
| 46 | readonly models: ReadonlyArray<string>; |
| 47 | }> { |
| 48 | override get message() { |
| 49 | return ( |
| 50 | `Models ${this.models.join(", ")} on Worker '${this.scriptName}' shared the Durable Object class '${this.legacyName}' before Domain version 0, so its data belongs to both. ` + |
| 51 | "Each model now has its own classes, and only one can keep that data. " + |
| 52 | `Set \`legacyData: true\` in the layer options of exactly one of them, e.g. \`${this.models[0]}.layer(handlers, { legacyData: true })\`; the others start empty.` |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * A newer Alchemy deployed this Worker, with a Domain layout this one doesn't |
| 59 | * know. Deploying would move its data back to an older layout, so the deploy |
| 60 | * stops before any upload. |
| 61 | */ |
| 62 | export class DomainVersionTooNew extends Data.TaggedError("DomainVersionTooNew")<{ |
| 63 | readonly scriptName: string; |
| 64 | readonly deployed: number; |
| 65 | readonly current: number; |
| 66 | }> { |
| 67 | override get message() { |
| 68 | return ( |
| 69 | `Worker '${this.scriptName}' was deployed with Domain version ${this.deployed}, but this Alchemy only knows up to version ${this.current}. ` + |
| 70 | "Deploy it with the Alchemy release that deployed it, or a newer one." |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Fail when a newer Alchemy deployed the Worker. Checked on every deploy of a |
| 77 | * Worker, whether or not it still hosts a model: older code would move the |
| 78 | * newer layout's data back, or delete its classes. |
| 79 | */ |
| 80 | export const refuseDowngrade = ({ |
| 81 | scriptName, |
| 82 | deployed, |
| 83 | }: { |
| 84 | readonly scriptName: string; |
| 85 | readonly deployed: number | undefined; |
| 86 | }) => |
| 87 | deployed !== undefined && deployed > DOMAIN_VERSION |
| 88 | ? Effect.fail(new DomainVersionTooNew({ scriptName, deployed, current: DOMAIN_VERSION })) |
| 89 | : Effect.void; |
| 90 | |
| 91 | /** The tag that records the Domain version a Worker was deployed with. */ |
| 92 | export const versionTag = (version: number) => `alchemy:domain:v${version}`; |
| 93 | |
| 94 | /** The Domain version a Worker was deployed with, from its tags; `undefined` before versions existed. */ |
| 95 | export const deployedVersion = (tags: ReadonlyArray<string>): number | undefined => { |
| 96 | for (const tag of tags) { |
| 97 | const match = /^alchemy:domain:v(\d+)$/.exec(tag); |
| 98 | if (match) return Number(match[1]); |
| 99 | } |
| 100 | return undefined; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * The Durable Object class renames that bring a Worker deployed at `deployed` |
| 105 | * up to {@link DOMAIN_VERSION} without losing data. `deployedClasses` are the |
| 106 | * classes the Worker hosts now. |
| 107 | */ |
| 108 | export const migrate = Effect.fn(function* ({ |
| 109 | scriptName, |
| 110 | deployed, |
| 111 | classes, |
| 112 | deployedClasses, |
| 113 | }: { |
| 114 | readonly scriptName: string; |
| 115 | readonly deployed: number | undefined; |
| 116 | readonly classes: ReadonlyArray<DomainClass>; |
| 117 | readonly deployedClasses: ReadonlySet<string>; |
| 118 | }) { |
| 119 | const renamed: Array<{ readonly from: string; readonly to: string }> = []; |
| 120 | if (deployed === undefined) { |
| 121 | // none -> 0: classes were named after the definition (`AccountAggregate`), |
| 122 | // now after the model too (`Billing.AccountAggregate`). |
| 123 | const byLegacyName = new Map<string, Array<DomainClass>>(); |
| 124 | for (const domainClass of classes) { |
| 125 | if (!deployedClasses.has(domainClass.legacyName)) continue; |
| 126 | const sharing = byLegacyName.get(domainClass.legacyName) ?? []; |
| 127 | sharing.push(domainClass); |
| 128 | byLegacyName.set(domainClass.legacyName, sharing); |
| 129 | } |
| 130 | for (const [legacyName, sharing] of byLegacyName) { |
| 131 | const keeps = |
| 132 | sharing.length === 1 ? sharing : sharing.filter((domainClass) => domainClass.legacyData); |
| 133 | if (keeps.length !== 1) { |
| 134 | return yield* new AmbiguousDomainLegacyData({ |
| 135 | scriptName, |
| 136 | legacyName, |
| 137 | models: sharing.map((domainClass) => domainClass.model), |
| 138 | }); |
| 139 | } |
| 140 | renamed.push({ from: legacyName, to: keeps[0]!.className }); |
| 141 | } |
| 142 | } |
| 143 | // if (deployed === undefined || deployed < 1) { ... 0 -> 1 } |
| 144 | return renamed; |
| 145 | }); |