|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { dirname, resolve } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Guards the double-authoring rule for run-graph schema changes. |
| 8 | + * |
| 9 | + * The run-ops database is on its own migration history, so a change to a run-graph model has to be |
| 10 | + * written twice: once in `@trigger.dev/database` (which targets the control-plane and legacy RDS |
| 11 | + * databases) and once here. Nothing about the two histories forces that, and when it is missed the |
| 12 | + * run-ops status job truthfully reports "up to date" against its own history — so the change simply |
| 13 | + * never lands, silently. |
| 14 | + * |
| 15 | + * This compares the PHYSICAL shape of every model the run-ops schema declares against its |
| 16 | + * control-plane counterpart: scalar fields with their attributes, plus `@@index` / `@@unique` / |
| 17 | + * `@@id` / `@@map`. Relation navigation fields are excluded, because the run-ops schema deliberately |
| 18 | + * drops relations that would cross into the control-plane database while keeping the scalar FK |
| 19 | + * column. |
| 20 | + */ |
| 21 | + |
| 22 | +const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); |
| 23 | +const RUN_OPS_SCHEMA = resolve(packageRoot, "prisma/schema.prisma"); |
| 24 | +const CONTROL_PLANE_SCHEMA = resolve(packageRoot, "../database/prisma/schema.prisma"); |
| 25 | + |
| 26 | +/** |
| 27 | + * Models that exist only in the run-ops schema, with no control-plane counterpart to compare |
| 28 | + * against. Both replace a control-plane implicit many-to-many relation with an explicit, FK-free |
| 29 | + * join model, because an implicit m2m carries a foreign key that cannot resolve across databases. |
| 30 | + */ |
| 31 | +const RUN_OPS_ONLY_MODELS = new Set(["CompletedWaitpoint", "WaitpointRunConnection"]); |
| 32 | + |
| 33 | +type Schema = { |
| 34 | + models: Map<string, string[]>; |
| 35 | + modelNames: Set<string>; |
| 36 | +}; |
| 37 | + |
| 38 | +function parseSchema(path: string): Schema { |
| 39 | + const lines = readFileSync(path, "utf8").split("\n"); |
| 40 | + const models = new Map<string, string[]>(); |
| 41 | + let current: string | null = null; |
| 42 | + let body: string[] = []; |
| 43 | + |
| 44 | + for (const line of lines) { |
| 45 | + const open = line.match(/^model\s+(\w+)\s*\{/); |
| 46 | + if (open) { |
| 47 | + current = open[1]!; |
| 48 | + body = []; |
| 49 | + continue; |
| 50 | + } |
| 51 | + if (current !== null && /^\}/.test(line)) { |
| 52 | + models.set(current, body); |
| 53 | + current = null; |
| 54 | + continue; |
| 55 | + } |
| 56 | + if (current !== null) { |
| 57 | + const trimmed = line.trim(); |
| 58 | + if (trimmed.length > 0 && !trimmed.startsWith("//")) { |
| 59 | + body.push(trimmed); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return { models, modelNames: new Set(models.keys()) }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Keeps the lines that describe physical database structure and drops the ones that describe Prisma |
| 69 | + * relations. A field is a relation navigation field when its type resolves to a model, so scalar FK |
| 70 | + * columns (`String`) and enum-typed columns are both retained. |
| 71 | + */ |
| 72 | +function physicalLines(body: string[], modelNames: Set<string>): Set<string> { |
| 73 | + const kept = new Set<string>(); |
| 74 | + |
| 75 | + for (const line of body) { |
| 76 | + if (line.startsWith("@@")) { |
| 77 | + if (/^@@(index|unique|id|map)\b/.test(line)) { |
| 78 | + kept.add(line.replace(/\s+/g, " ")); |
| 79 | + } |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + const field = line.match(/^(\w+)\s+(\w+)(\[\]|\?)?/); |
| 84 | + if (!field) continue; |
| 85 | + |
| 86 | + const [, , type] = field; |
| 87 | + if (modelNames.has(type!)) continue; |
| 88 | + if (line.includes("@relation(")) continue; |
| 89 | + |
| 90 | + kept.add(line.replace(/\s+/g, " ")); |
| 91 | + } |
| 92 | + |
| 93 | + return kept; |
| 94 | +} |
| 95 | + |
| 96 | +describe("run-ops schema parity with the control-plane schema", () => { |
| 97 | + const runOps = parseSchema(RUN_OPS_SCHEMA); |
| 98 | + const controlPlane = parseSchema(CONTROL_PLANE_SCHEMA); |
| 99 | + |
| 100 | + const sharedModels = [...runOps.models.keys()] |
| 101 | + .filter((name) => !RUN_OPS_ONLY_MODELS.has(name)) |
| 102 | + .sort(); |
| 103 | + |
| 104 | + it("finds models to compare", () => { |
| 105 | + expect(sharedModels.length).toBeGreaterThan(10); |
| 106 | + }); |
| 107 | + |
| 108 | + it("declares every run-ops-only model in the exception list, and no others", () => { |
| 109 | + const missingFromControlPlane = [...runOps.models.keys()] |
| 110 | + .filter((name) => !controlPlane.models.has(name)) |
| 111 | + .sort(); |
| 112 | + |
| 113 | + expect(missingFromControlPlane).toEqual([...RUN_OPS_ONLY_MODELS].sort()); |
| 114 | + }); |
| 115 | + |
| 116 | + it.each(sharedModels)("%s has the same physical shape in both schemas", (modelName) => { |
| 117 | + const runOpsBody = runOps.models.get(modelName); |
| 118 | + const controlPlaneBody = controlPlane.models.get(modelName); |
| 119 | + |
| 120 | + expect(runOpsBody, `${modelName} missing from the run-ops schema`).toBeDefined(); |
| 121 | + expect(controlPlaneBody, `${modelName} missing from the control-plane schema`).toBeDefined(); |
| 122 | + |
| 123 | + const runOpsShape = physicalLines(runOpsBody!, runOps.modelNames); |
| 124 | + const controlPlaneShape = physicalLines(controlPlaneBody!, controlPlane.modelNames); |
| 125 | + |
| 126 | + const onlyInControlPlane = [...controlPlaneShape].filter((l) => !runOpsShape.has(l)).sort(); |
| 127 | + const onlyInRunOps = [...runOpsShape].filter((l) => !controlPlaneShape.has(l)).sort(); |
| 128 | + |
| 129 | + expect( |
| 130 | + { onlyInControlPlane, onlyInRunOps }, |
| 131 | + `${modelName} differs physically between the two schemas. A run-graph change was written to ` + |
| 132 | + `one schema and not the other; add the missing side plus a migration in that package.` |
| 133 | + ).toEqual({ onlyInControlPlane: [], onlyInRunOps: [] }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments