The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Prisma from "@alchemy.run/prisma"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import * as Stack from "alchemy/Stack"; |
| 6 | import { State } from "alchemy/State"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Path from "effect/Path"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Prisma.providers() }); |
| 13 | |
| 14 | const CONFIG_SOURCE = ` |
| 15 | import { defineConfig as ormConfig } from "@prisma/orm-postgres/config"; |
| 16 | import { definePrismaConfig } from "prisma/config"; |
| 17 | |
| 18 | export default definePrismaConfig({ |
| 19 | orm: ormConfig({ |
| 20 | contract: "./contract.prisma", |
| 21 | output: "./generated", |
| 22 | }), |
| 23 | }); |
| 24 | `; |
| 25 | |
| 26 | const CONTRACT_SOURCE = `// use prisma-8 |
| 27 | model User { |
| 28 | id Int @id @default(autoincrement()) |
| 29 | email String @unique |
| 30 | name String? |
| 31 | } |
| 32 | `; |
| 33 | |
| 34 | const DRIFTED_CONTRACT_SOURCE = |
| 35 | CONTRACT_SOURCE + |
| 36 | ` |
| 37 | model Post { |
| 38 | id Int @id @default(autoincrement()) |
| 39 | title String |
| 40 | } |
| 41 | `; |
| 42 | |
| 43 | // Making \`name\` required forces a data transform on existing rows, which |
| 44 | // Prisma renders as unfilled placeholder(...) closures. |
| 45 | const PLACEHOLDER_CONTRACT_SOURCE = CONTRACT_SOURCE.replace("name String?", "name String"); |
| 46 | |
| 47 | const TS_CONFIG_SOURCE = ` |
| 48 | import { defineConfig as ormConfig } from "@prisma/orm-postgres/config"; |
| 49 | import { definePrismaConfig } from "prisma/config"; |
| 50 | |
| 51 | export default definePrismaConfig({ |
| 52 | orm: ormConfig({ |
| 53 | contract: "./contract.ts", |
| 54 | output: "./generated", |
| 55 | }), |
| 56 | }); |
| 57 | `; |
| 58 | |
| 59 | // The TypeScript-authored form (Prisma 8's headline direction): the same |
| 60 | // contract as CONTRACT_SOURCE, via the defineContract builder DSL. |
| 61 | const TS_CONTRACT_SOURCE = ` |
| 62 | import { defineContract } from "@prisma/orm-postgres/contract-builder"; |
| 63 | |
| 64 | export const contract = defineContract({}, ({ field, model }) => ({ |
| 65 | models: { |
| 66 | User: model("User", { |
| 67 | fields: { |
| 68 | id: field.id.uuidv7String(), |
| 69 | email: field.text().unique(), |
| 70 | name: field.text().optional(), |
| 71 | }, |
| 72 | }), |
| 73 | }, |
| 74 | })); |
| 75 | `; |
| 76 | |
| 77 | const stageWorkspace = (contractSource: string) => |
| 78 | Effect.gen(function* () { |
| 79 | const fs = yield* FileSystem.FileSystem; |
| 80 | const path = yield* Path.Path; |
| 81 | // Stage inside the repo (not the OS temp dir): prisma.config.ts |
| 82 | // imports `@prisma/orm-postgres/config`, and bun resolves that bare |
| 83 | // import by walking up from the config file — which only finds |
| 84 | // `node_modules` when the staging dir lives in the workspace. |
| 85 | const cwd = yield* Effect.sync(() => process.cwd()); |
| 86 | const tempParent = path.join(cwd, ".alchemy", "tmp"); |
| 87 | yield* fs.makeDirectory(tempParent, { recursive: true }); |
| 88 | const root = yield* fs.makeTempDirectory({ |
| 89 | directory: tempParent, |
| 90 | prefix: "alchemy-prisma-contract-test-", |
| 91 | }); |
| 92 | yield* fs.writeFileString(path.join(root, "prisma.config.ts"), CONFIG_SOURCE); |
| 93 | const contractPath = path.join(root, "contract.prisma"); |
| 94 | yield* fs.writeFileString(contractPath, contractSource); |
| 95 | return { |
| 96 | root, |
| 97 | contractPath, |
| 98 | configPath: path.join(root, "prisma.config.ts"), |
| 99 | migrationsDir: path.join(root, "migrations"), |
| 100 | }; |
| 101 | }); |
| 102 | |
| 103 | const readPackageDirs = (migrationsDir: string) => |
| 104 | Effect.gen(function* () { |
| 105 | const fs = yield* FileSystem.FileSystem; |
| 106 | const path = yield* Path.Path; |
| 107 | const appDir = path.join(migrationsDir, "app"); |
| 108 | const exists = yield* fs.exists(appDir); |
| 109 | if (!exists) return [] as string[]; |
| 110 | return (yield* fs.readDirectory(appDir)).sort(); |
| 111 | }); |
| 112 | |
| 113 | const readPackageMeta = (migrationsDir: string, dirName: string) => |
| 114 | Effect.gen(function* () { |
| 115 | const fs = yield* FileSystem.FileSystem; |
| 116 | const path = yield* Path.Path; |
| 117 | const text = yield* fs.readFileString( |
| 118 | path.join(migrationsDir, "app", dirName, "migration.json"), |
| 119 | ); |
| 120 | return JSON.parse(text) as { from: string | null; to: string; migrationHash: string }; |
| 121 | }); |
| 122 | |
| 123 | const getStatus = Effect.fn(function* (fqn: string) { |
| 124 | const state = yield* yield* State; |
| 125 | const stk = yield* Stack.Stack; |
| 126 | const s = yield* state.get({ stack: stk.name, stage: stk.stage, fqn }); |
| 127 | return s?.status; |
| 128 | }); |
| 129 | |
| 130 | test.provider( |
| 131 | "initial deploy emits the contract and plans the first migration", |
| 132 | (stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const fs = yield* FileSystem.FileSystem; |
| 135 | const path = yield* Path.Path; |
| 136 | const ws = yield* stageWorkspace(CONTRACT_SOURCE); |
| 137 | |
| 138 | const contract = yield* stack.deploy( |
| 139 | Prisma.Contract("app-contract", { config: ws.configPath }), |
| 140 | ); |
| 141 | |
| 142 | // Emitted artifacts land in the config's output dir. |
| 143 | expect(yield* fs.exists(path.join(ws.root, "generated", "contract.json"))).toBe(true); |
| 144 | expect(yield* fs.exists(path.join(ws.root, "generated", "contract.d.ts"))).toBe(true); |
| 145 | |
| 146 | // One migration package planned from the empty contract to the head. |
| 147 | const dirs = yield* readPackageDirs(ws.migrationsDir); |
| 148 | expect(dirs).toHaveLength(1); |
| 149 | const meta = yield* readPackageMeta(ws.migrationsDir, dirs[0]!); |
| 150 | expect(meta.from).toBeNull(); |
| 151 | expect(meta.to).toEqual(contract.contractHash); |
| 152 | expect(contract.migrations).toEqual(dirs); |
| 153 | }), |
| 154 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 155 | ); |
| 156 | |
| 157 | test.provider( |
| 158 | "repeated deploys with no contract drift stay noop, not update (spurious updates would cascade into Prisma.Migrate)", |
| 159 | (stack) => |
| 160 | Effect.gen(function* () { |
| 161 | const ws = yield* stageWorkspace(CONTRACT_SOURCE); |
| 162 | |
| 163 | yield* stack.deploy(Prisma.Contract("app-contract", { config: ws.configPath })); |
| 164 | expect(yield* getStatus("app-contract")).toEqual("created"); |
| 165 | |
| 166 | yield* stack.deploy(Prisma.Contract("app-contract", { config: ws.configPath })); |
| 167 | expect(yield* getStatus("app-contract")).toEqual("created"); |
| 168 | |
| 169 | // No duplicate package was planned (`migration plan` without an |
| 170 | // explicit --from would happily re-plan from empty every run). |
| 171 | expect(yield* readPackageDirs(ws.migrationsDir)).toHaveLength(1); |
| 172 | }), |
| 173 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 174 | ); |
| 175 | |
| 176 | test.provider( |
| 177 | "deploy after a real contract change updates the resource", |
| 178 | (stack) => |
| 179 | Effect.gen(function* () { |
| 180 | const fs = yield* FileSystem.FileSystem; |
| 181 | const ws = yield* stageWorkspace(CONTRACT_SOURCE); |
| 182 | |
| 183 | const initial = yield* stack.deploy( |
| 184 | Prisma.Contract("app-contract", { config: ws.configPath }), |
| 185 | ); |
| 186 | const [initialDir] = yield* readPackageDirs(ws.migrationsDir); |
| 187 | |
| 188 | yield* fs.writeFileString(ws.contractPath, DRIFTED_CONTRACT_SOURCE); |
| 189 | yield* Effect.sleep("1 second"); |
| 190 | |
| 191 | const drifted = yield* stack.deploy( |
| 192 | Prisma.Contract("app-contract", { config: ws.configPath }), |
| 193 | ); |
| 194 | |
| 195 | expect(yield* getStatus("app-contract")).toEqual("updated"); |
| 196 | expect(drifted.contractHash).not.toEqual(initial.contractHash); |
| 197 | |
| 198 | // The new package chains from the previous head. |
| 199 | const dirs = yield* readPackageDirs(ws.migrationsDir); |
| 200 | expect(dirs).toHaveLength(2); |
| 201 | const initialMeta = yield* readPackageMeta(ws.migrationsDir, initialDir!); |
| 202 | const newDir = dirs.find((dir) => dir !== initialDir); |
| 203 | const newMeta = yield* readPackageMeta(ws.migrationsDir, newDir!); |
| 204 | expect(newMeta.from).toEqual(initialMeta.to); |
| 205 | expect(newMeta.to).toEqual(drifted.contractHash); |
| 206 | }), |
| 207 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 208 | ); |
| 209 | |
| 210 | test.provider( |
| 211 | "TypeScript-authored contracts emit, plan, and get resolvable types", |
| 212 | (stack) => |
| 213 | Effect.gen(function* () { |
| 214 | const fs = yield* FileSystem.FileSystem; |
| 215 | const path = yield* Path.Path; |
| 216 | const ws = yield* stageWorkspace(CONTRACT_SOURCE); |
| 217 | // Swap in the TS-authored form of the same workspace. |
| 218 | yield* fs.remove(ws.contractPath); |
| 219 | yield* fs.writeFileString(path.join(ws.root, "prisma.config.ts"), TS_CONFIG_SOURCE); |
| 220 | yield* fs.writeFileString(path.join(ws.root, "contract.ts"), TS_CONTRACT_SOURCE); |
| 221 | |
| 222 | const contract = yield* stack.deploy( |
| 223 | Prisma.Contract("ts-contract", { config: ws.configPath }), |
| 224 | ); |
| 225 | |
| 226 | const dirs = yield* readPackageDirs(ws.migrationsDir); |
| 227 | expect(dirs).toHaveLength(1); |
| 228 | expect((yield* readPackageMeta(ws.migrationsDir, dirs[0]!)).to).toEqual( |
| 229 | contract.contractHash, |
| 230 | ); |
| 231 | |
| 232 | // The CLI emits unpublished @internal/* specifiers for TS-authored |
| 233 | // contracts; the resource must rewrite them to the public subpaths or |
| 234 | // the emitted types cannot resolve in a user project. |
| 235 | const dts = yield* fs.readFileString(path.join(ws.root, "generated", "contract.d.ts")); |
| 236 | expect(dts).not.toContain("@internal/"); |
| 237 | expect(dts).toContain("@prisma/orm-postgres/"); |
| 238 | }), |
| 239 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 240 | ); |
| 241 | |
| 242 | test.provider( |
| 243 | "list returns [] (non-listable local build artifact)", |
| 244 | (stack) => |
| 245 | Effect.gen(function* () { |
| 246 | yield* stack.destroy(); |
| 247 | |
| 248 | const provider = yield* Provider.findProvider(Prisma.Contract); |
| 249 | const all = yield* provider.list(); |
| 250 | expect(all).toEqual([]); |
| 251 | |
| 252 | yield* stack.destroy(); |
| 253 | }), |
| 254 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 255 | ); |
| 256 | |
| 257 | test.provider( |
| 258 | "refuses plans with unfilled placeholders (data transforms need a human decision)", |
| 259 | (stack) => |
| 260 | Effect.gen(function* () { |
| 261 | const fs = yield* FileSystem.FileSystem; |
| 262 | const ws = yield* stageWorkspace(CONTRACT_SOURCE); |
| 263 | |
| 264 | yield* stack.deploy(Prisma.Contract("app-contract", { config: ws.configPath })); |
| 265 | |
| 266 | // name String? -> String needs a backfill for existing NULL rows; the |
| 267 | // planned SQL runs against a real database later in the same deploy, |
| 268 | // so the resource must fail with guidance instead of deciding. |
| 269 | yield* fs.writeFileString(ws.contractPath, PLACEHOLDER_CONTRACT_SOURCE); |
| 270 | yield* Effect.sleep("1 second"); |
| 271 | |
| 272 | const result = yield* Effect.result( |
| 273 | stack.deploy(Prisma.Contract("app-contract", { config: ws.configPath })), |
| 274 | ); |
| 275 | |
| 276 | expect(Result.isFailure(result)).toBe(true); |
| 277 | if (Result.isFailure(result)) { |
| 278 | expect(String(result.failure)).toContain("placeholder"); |
| 279 | expect(String(result.failure)).toContain("migration.ts"); |
| 280 | } |
| 281 | }), |
| 282 | { tags: ["provider:prisma", "provider:prisma:orm", "live"] }, |
| 283 | ); |