Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+65
-88
@@ -1,105 +1,82 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Footgun guard (R-P6C, AGENTS DoD #16/§Querschnitt): every Prisma model that
|
||||
* carries a `tenantId String` scalar MUST be listed in TENANT_SCOPED_MODELS
|
||||
* (packages/cli/src/commands/tenant/models.ts). A forgotten entry silently leaks
|
||||
* the model out of tenant snapshot/restore (and the withTenantScope contract)
|
||||
* and previously only surfaced via the cryptic packages/cli `models.test.ts`
|
||||
* failure that broke the R-P6 CI run.
|
||||
* CI tenant-boundary drift gate.
|
||||
*
|
||||
* This dedicated check fails loudly and names the offending model + schema so
|
||||
* the fix is one-line obvious, and runs project-wide in CI (.gitea/workflows/
|
||||
* ci.yml) instead of only inside the cli package test bucket.
|
||||
*
|
||||
* The schema-parsing here is kept intentionally in sync with
|
||||
* packages/cli/src/commands/tenant/models.test.ts (same model-block + `tenantId
|
||||
* String` regexes) so both gates agree on what "tenant-scoped" means.
|
||||
*
|
||||
* NOTE on the optional search-registry check: a "models with a packages/search
|
||||
* schema must be in indexed-entities" assertion was deliberately NOT added —
|
||||
* search index schemas (rows, users, entities, hr-directory, …) do not 1:1 map
|
||||
* to a Prisma model and most are intentionally absent from the registry, so
|
||||
* such a check would produce false positives. Out of scope, left to a future CR.
|
||||
* The explicit manifest classifies every Prisma model, including nullable
|
||||
* hybrids, FK-derived children, global catalogs, platform-admin data, and the
|
||||
* Tenant root. The legacy lifecycle registry remains checked separately because
|
||||
* snapshot/restore/delete currently operates on direct `tenantId` fields only.
|
||||
*/
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const root = join(here, "..");
|
||||
const schemaDir = join(root, "prisma", "schema");
|
||||
const modelsFile = join(root, "packages", "cli", "src", "commands", "tenant", "models.ts");
|
||||
import { TENANT_SCOPE_MANIFEST } from "../prisma/tenant-scope-manifest.mjs";
|
||||
import { parsePrismaSchemaDirectory, validateTenantScopeManifest } from "./lib/prisma-tenant-scope.mjs";
|
||||
|
||||
/**
|
||||
* Parse every prisma schema file and return the models that declare a
|
||||
* `tenantId String` scalar (nullable `String?` included, matching the
|
||||
* `^\s*tenantId\s+String` regex used by models.test.ts).
|
||||
*/
|
||||
function listTenantScopedSchemaModels() {
|
||||
const files = readdirSync(schemaDir).filter((f) => f.endsWith(".prisma"));
|
||||
const result = [];
|
||||
for (const file of files) {
|
||||
const text = readFileSync(join(schemaDir, file), "utf8");
|
||||
const modelBlock = /model\s+(\w+)\s*\{([\s\S]*?)\n\}/g;
|
||||
let m;
|
||||
while ((m = modelBlock.exec(text)) !== null) {
|
||||
const name = m[1];
|
||||
const body = m[2];
|
||||
if (/^\s*tenantId\s+String/m.test(body)) {
|
||||
result.push({ model: name, schema: file });
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the model names registered in the TENANT_SCOPED_MODELS array of
|
||||
* models.ts. Only the TENANT_SCOPED_MODELS block is scanned so that the
|
||||
* GLOBAL_REFERENCED_MODELS / referencedBy `model:` keys further down the file
|
||||
* are not picked up.
|
||||
*/
|
||||
function listRegisteredModels() {
|
||||
function listRegisteredLifecycleModels(modelsFile) {
|
||||
const text = readFileSync(modelsFile, "utf8");
|
||||
const start = text.indexOf("export const TENANT_SCOPED_MODELS");
|
||||
if (start === -1) {
|
||||
throw new Error(`could not find TENANT_SCOPED_MODELS in ${modelsFile}`);
|
||||
}
|
||||
if (start === -1) throw new Error(`could not find TENANT_SCOPED_MODELS in ${modelsFile}`);
|
||||
const end = text.indexOf("];", start);
|
||||
if (end === -1) {
|
||||
throw new Error(`could not find end of TENANT_SCOPED_MODELS array in ${modelsFile}`);
|
||||
}
|
||||
const block = text.slice(start, end);
|
||||
if (end === -1) throw new Error(`could not find end of TENANT_SCOPED_MODELS array in ${modelsFile}`);
|
||||
|
||||
const names = new Set();
|
||||
const entry = /model:\s*"([^"]+)"/g;
|
||||
let m;
|
||||
while ((m = entry.exec(block)) !== null) {
|
||||
names.add(m[1]);
|
||||
}
|
||||
for (const match of text.slice(start, end).matchAll(/model:\s*"([^"]+)"/g)) names.add(match[1]);
|
||||
return names;
|
||||
}
|
||||
|
||||
const schemaModels = listTenantScopedSchemaModels();
|
||||
const registered = listRegisteredModels();
|
||||
const missing = schemaModels.filter((m) => !registered.has(m.model));
|
||||
export function inspectCrossCuttingModels(rootDirectory = process.cwd(), options = {}) {
|
||||
const schemaDirectory = path.join(rootDirectory, "prisma", "schema");
|
||||
const modelsFile = path.join(rootDirectory, "packages", "cli", "src", "commands", "tenant", "models.ts");
|
||||
const manifest = options.manifest ?? TENANT_SCOPE_MANIFEST;
|
||||
const models = parsePrismaSchemaDirectory(schemaDirectory);
|
||||
const manifestResult = validateTenantScopeManifest(models, manifest);
|
||||
const registered = options.registeredLifecycleModels ?? listRegisteredLifecycleModels(modelsFile);
|
||||
const directTenantModels = models.filter((model) => model.tenantField);
|
||||
const directNames = new Set(directTenantModels.map((model) => model.model));
|
||||
const missingLifecycleModels = directTenantModels.filter((model) => !registered.has(model.model));
|
||||
const unknownLifecycleModels = [...registered].filter((model) => !directNames.has(model)).sort();
|
||||
const errors = [
|
||||
...manifestResult.errors,
|
||||
...missingLifecycleModels.map((model) => `${model.schema}: ${model.model} has tenantId but is missing from TENANT_SCOPED_MODELS`),
|
||||
...unknownLifecycleModels.map((model) => `${model} is registered in TENANT_SCOPED_MODELS but has no tenantId field`),
|
||||
];
|
||||
|
||||
if (missing.length > 0) {
|
||||
const lines = missing.map(
|
||||
(m) =>
|
||||
`- Modell ${m.model} (schema ${m.schema}) hat tenantId, fehlt aber in ` +
|
||||
`TENANT_SCOPED_MODELS — eintragen in packages/cli/src/commands/tenant/models.ts`,
|
||||
);
|
||||
process.stderr.write(
|
||||
[
|
||||
"FAIL cross-cutting model registry completeness",
|
||||
...lines,
|
||||
`Summary: ${schemaModels.length - missing.length}/${schemaModels.length} tenantId models registered, ${missing.length} missing`,
|
||||
].join("\n") + "\n",
|
||||
);
|
||||
process.exit(1);
|
||||
return {
|
||||
ok: errors.length === 0,
|
||||
errors,
|
||||
models,
|
||||
manifestResult,
|
||||
directTenantModels,
|
||||
missingLifecycleModels,
|
||||
unknownLifecycleModels,
|
||||
manifest,
|
||||
};
|
||||
}
|
||||
|
||||
process.stdout.write(
|
||||
`PASS cross-cutting model registry completeness\n` +
|
||||
`Summary: ${schemaModels.length}/${schemaModels.length} tenantId models registered, 0 missing\n`,
|
||||
);
|
||||
export function formatCrossCuttingModels(result) {
|
||||
const classifiedIndirect = [...result.manifestResult.indirectModels].filter((model) => {
|
||||
const entry = result.manifest[model];
|
||||
return entry?.scope === "derived-child" || entry?.scope === "platform-admin" || entry?.tenantParentBoundary === true;
|
||||
}).length;
|
||||
const lines = [
|
||||
`${result.ok ? "PASS" : "FAIL"} cross-cutting model registry completeness`,
|
||||
`Tenant scope manifest: ${result.manifestResult.classifiedCount}/${result.manifestResult.modelCount} Prisma models classified`,
|
||||
`Indirect tenant paths: ${result.manifestResult.indirectModels.size} detected, ${classifiedIndirect} boundary-classified`,
|
||||
`Lifecycle registry: ${result.directTenantModels.length - result.missingLifecycleModels.length}/${result.directTenantModels.length} direct tenantId models registered`,
|
||||
];
|
||||
for (const error of result.errors) lines.push(`FAIL ${error}`);
|
||||
lines.push(`Summary: ${result.errors.length} missing or inconsistent classifications`);
|
||||
return `${lines.join("\n")}\n`;
|
||||
}
|
||||
|
||||
export function runCrossCuttingModels(rootDirectory = process.cwd()) {
|
||||
const result = inspectCrossCuttingModels(rootDirectory);
|
||||
(result.ok ? process.stdout : process.stderr).write(formatCrossCuttingModels(result));
|
||||
return result.ok ? 0 : 1;
|
||||
}
|
||||
|
||||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||||
process.exitCode = runCrossCuttingModels();
|
||||
}
|
||||
Reference in new issue
Block a user