@prisma-next/target-mongo 0.5.0-dev.5 → 0.5.0-dev.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/control.d.mts +17 -13
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +16 -45
- package/dist/control.mjs.map +1 -1
- package/dist/{migration-factories-gwi81C8u.mjs → migration-factories-Dbk5afMU.mjs} +1 -1
- package/dist/{migration-factories-gwi81C8u.mjs.map → migration-factories-Dbk5afMU.mjs.map} +1 -1
- package/dist/migration.d.mts +1 -1
- package/dist/migration.mjs +1 -1
- package/dist/{op-factory-call-BjNAcPSF.d.mts → op-factory-call-CVgzmLJh.d.mts} +1 -1
- package/dist/{op-factory-call-BjNAcPSF.d.mts.map → op-factory-call-CVgzmLJh.d.mts.map} +1 -1
- package/dist/schema-verify.d.mts +22 -0
- package/dist/schema-verify.d.mts.map +1 -0
- package/dist/schema-verify.mjs +3 -0
- package/dist/verify-mongo-schema-P0TRBJNs.mjs +582 -0
- package/dist/verify-mongo-schema-P0TRBJNs.mjs.map +1 -0
- package/package.json +15 -14
- package/src/core/mongo-runner.ts +38 -26
- package/src/core/schema-diff.ts +402 -0
- package/src/core/schema-verify/canonicalize-introspection.ts +389 -0
- package/src/core/schema-verify/verify-mongo-schema.ts +60 -0
- package/src/exports/schema-verify.ts +2 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"verify-mongo-schema-P0TRBJNs.mjs","names":["issues: SchemaIssue[]","collectionChildren: SchemaVerificationNode[]","nodes: SchemaVerificationNode[]","MongoSchemaIRCtor","MongoSchemaCollectionCtor","MongoSchemaIndexCtor","projectedKeys: IndexKey[]","MongoSchemaCollectionOptionsCtor","out: Record<string, unknown>"],"sources":["../src/core/contract-to-schema.ts","../src/core/schema-diff.ts","../src/core/schema-verify/canonicalize-introspection.ts","../src/core/schema-verify/verify-mongo-schema.ts"],"sourcesContent":["import type {\n MongoContract,\n MongoStorageCollection,\n MongoStorageCollectionOptions,\n MongoStorageIndex,\n MongoStorageValidator,\n} from '@prisma-next/mongo-contract';\nimport {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\n\nfunction convertIndex(index: MongoStorageIndex): MongoSchemaIndex {\n return new MongoSchemaIndex({\n keys: index.keys,\n unique: index.unique,\n sparse: index.sparse,\n expireAfterSeconds: index.expireAfterSeconds,\n partialFilterExpression: index.partialFilterExpression,\n wildcardProjection: index.wildcardProjection,\n collation: index.collation,\n weights: index.weights,\n default_language: index.default_language,\n language_override: index.language_override,\n });\n}\n\nfunction convertValidator(v: MongoStorageValidator): MongoSchemaValidator {\n return new MongoSchemaValidator({\n jsonSchema: v.jsonSchema,\n validationLevel: v.validationLevel,\n validationAction: v.validationAction,\n });\n}\n\nfunction convertOptions(o: MongoStorageCollectionOptions): MongoSchemaCollectionOptions {\n return new MongoSchemaCollectionOptions(o);\n}\n\nfunction convertCollection(name: string, def: MongoStorageCollection): MongoSchemaCollection {\n const indexes = (def.indexes ?? []).map(convertIndex);\n return new MongoSchemaCollection({\n name,\n indexes,\n ...(def.validator != null && { validator: convertValidator(def.validator) }),\n ...(def.options != null && { options: convertOptions(def.options) }),\n });\n}\n\nexport function contractToMongoSchemaIR(contract: MongoContract | null): MongoSchemaIR {\n if (!contract) {\n return new MongoSchemaIR([]);\n }\n\n const collections = Object.entries(contract.storage.collections).map(([name, def]) =>\n convertCollection(name, def),\n );\n\n return new MongoSchemaIR(collections);\n}\n","import type {\n SchemaIssue,\n SchemaVerificationNode,\n} from '@prisma-next/framework-components/control';\nimport type {\n MongoSchemaCollection,\n MongoSchemaIndex,\n MongoSchemaIR,\n} from '@prisma-next/mongo-schema-ir';\nimport { canonicalize, deepEqual } from '@prisma-next/mongo-schema-ir';\n\nexport function diffMongoSchemas(\n live: MongoSchemaIR,\n expected: MongoSchemaIR,\n strict: boolean,\n): {\n root: SchemaVerificationNode;\n issues: SchemaIssue[];\n counts: { pass: number; warn: number; fail: number; totalNodes: number };\n} {\n const issues: SchemaIssue[] = [];\n const collectionChildren: SchemaVerificationNode[] = [];\n let pass = 0;\n let warn = 0;\n let fail = 0;\n\n const allNames = new Set([...live.collectionNames, ...expected.collectionNames]);\n\n for (const name of [...allNames].sort()) {\n const liveColl = live.collection(name);\n const expectedColl = expected.collection(name);\n\n if (!liveColl && expectedColl) {\n issues.push({\n kind: 'missing_table',\n table: name,\n message: `Collection \"${name}\" is missing from the database`,\n });\n collectionChildren.push({\n status: 'fail',\n kind: 'collection',\n name,\n contractPath: `storage.collections.${name}`,\n code: 'MISSING_COLLECTION',\n message: `Collection \"${name}\" is missing`,\n expected: name,\n actual: null,\n children: [],\n });\n fail++;\n continue;\n }\n\n if (liveColl && !expectedColl) {\n const status = strict ? 'fail' : 'warn';\n issues.push({\n kind: 'extra_table',\n table: name,\n message: `Extra collection \"${name}\" exists in the database but not in the contract`,\n });\n collectionChildren.push({\n status,\n kind: 'collection',\n name,\n contractPath: `storage.collections.${name}`,\n code: 'EXTRA_COLLECTION',\n message: `Extra collection \"${name}\" found`,\n expected: null,\n actual: name,\n children: [],\n });\n if (status === 'fail') fail++;\n else warn++;\n continue;\n }\n\n const lc = liveColl as MongoSchemaCollection;\n const ec = expectedColl as MongoSchemaCollection;\n const indexChildren = diffIndexes(name, lc, ec, strict, issues);\n const validatorChildren = diffValidator(name, lc, ec, strict, issues);\n const optionsChildren = diffOptions(name, lc, ec, strict, issues);\n const children = [...indexChildren, ...validatorChildren, ...optionsChildren];\n\n const worstStatus = children.reduce<'pass' | 'warn' | 'fail'>(\n (s, c) => (c.status === 'fail' ? 'fail' : c.status === 'warn' && s !== 'fail' ? 'warn' : s),\n 'pass',\n );\n\n for (const c of children) {\n if (c.status === 'pass') pass++;\n else if (c.status === 'warn') warn++;\n else fail++;\n }\n\n if (children.length === 0) {\n pass++;\n }\n\n collectionChildren.push({\n status: worstStatus,\n kind: 'collection',\n name,\n contractPath: `storage.collections.${name}`,\n code: worstStatus === 'pass' ? 'MATCH' : 'DRIFT',\n message:\n worstStatus === 'pass' ? `Collection \"${name}\" matches` : `Collection \"${name}\" has drift`,\n expected: name,\n actual: name,\n children,\n });\n }\n\n const rootStatus = fail > 0 ? 'fail' : warn > 0 ? 'warn' : 'pass';\n const totalNodes = pass + warn + fail + collectionChildren.length;\n\n const root: SchemaVerificationNode = {\n status: rootStatus,\n kind: 'root',\n name: 'mongo-schema',\n contractPath: 'storage',\n code: rootStatus === 'pass' ? 'MATCH' : 'DRIFT',\n message: rootStatus === 'pass' ? 'Schema matches' : 'Schema has drift',\n expected: null,\n actual: null,\n children: collectionChildren,\n };\n\n return { root, issues, counts: { pass, warn, fail, totalNodes } };\n}\n\nfunction buildIndexLookupKey(index: MongoSchemaIndex): string {\n const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');\n const opts = [\n index.unique ? 'unique' : '',\n index.sparse ? 'sparse' : '',\n index.expireAfterSeconds != null ? `ttl:${index.expireAfterSeconds}` : '',\n index.partialFilterExpression ? `pfe:${canonicalize(index.partialFilterExpression)}` : '',\n index.wildcardProjection ? `wp:${canonicalize(index.wildcardProjection)}` : '',\n index.collation ? `col:${canonicalize(index.collation)}` : '',\n index.weights ? `wt:${canonicalize(index.weights)}` : '',\n index.default_language ? `dl:${index.default_language}` : '',\n index.language_override ? `lo:${index.language_override}` : '',\n ]\n .filter(Boolean)\n .join(';');\n return opts ? `${keys}|${opts}` : keys;\n}\n\nfunction formatIndexName(index: MongoSchemaIndex): string {\n return index.keys.map((k) => `${k.field}:${k.direction}`).join(', ');\n}\n\nfunction diffIndexes(\n collName: string,\n live: MongoSchemaCollection,\n expected: MongoSchemaCollection,\n strict: boolean,\n issues: SchemaIssue[],\n): SchemaVerificationNode[] {\n const nodes: SchemaVerificationNode[] = [];\n const liveLookup = new Map<string, MongoSchemaIndex>();\n for (const idx of live.indexes) liveLookup.set(buildIndexLookupKey(idx), idx);\n\n const expectedLookup = new Map<string, MongoSchemaIndex>();\n for (const idx of expected.indexes) expectedLookup.set(buildIndexLookupKey(idx), idx);\n\n for (const [key, idx] of expectedLookup) {\n if (liveLookup.has(key)) {\n nodes.push({\n status: 'pass',\n kind: 'index',\n name: formatIndexName(idx),\n contractPath: `storage.collections.${collName}.indexes`,\n code: 'MATCH',\n message: `Index ${formatIndexName(idx)} matches`,\n expected: key,\n actual: key,\n children: [],\n });\n } else {\n issues.push({\n kind: 'index_mismatch',\n table: collName,\n indexOrConstraint: formatIndexName(idx),\n message: `Index ${formatIndexName(idx)} missing on collection \"${collName}\"`,\n });\n nodes.push({\n status: 'fail',\n kind: 'index',\n name: formatIndexName(idx),\n contractPath: `storage.collections.${collName}.indexes`,\n code: 'MISSING_INDEX',\n message: `Index ${formatIndexName(idx)} missing`,\n expected: key,\n actual: null,\n children: [],\n });\n }\n }\n\n for (const [key, idx] of liveLookup) {\n if (!expectedLookup.has(key)) {\n const status = strict ? 'fail' : 'warn';\n issues.push({\n kind: 'extra_index',\n table: collName,\n indexOrConstraint: formatIndexName(idx),\n message: `Extra index ${formatIndexName(idx)} on collection \"${collName}\"`,\n });\n nodes.push({\n status,\n kind: 'index',\n name: formatIndexName(idx),\n contractPath: `storage.collections.${collName}.indexes`,\n code: 'EXTRA_INDEX',\n message: `Extra index ${formatIndexName(idx)}`,\n expected: null,\n actual: key,\n children: [],\n });\n }\n }\n\n return nodes;\n}\n\nfunction diffValidator(\n collName: string,\n live: MongoSchemaCollection,\n expected: MongoSchemaCollection,\n strict: boolean,\n issues: SchemaIssue[],\n): SchemaVerificationNode[] {\n if (!live.validator && !expected.validator) return [];\n\n if (expected.validator && !live.validator) {\n issues.push({\n kind: 'type_missing',\n table: collName,\n message: `Validator missing on collection \"${collName}\"`,\n });\n return [\n {\n status: 'fail',\n kind: 'validator',\n name: 'validator',\n contractPath: `storage.collections.${collName}.validator`,\n code: 'MISSING_VALIDATOR',\n message: 'Validator missing',\n expected: canonicalize(expected.validator.jsonSchema),\n actual: null,\n children: [],\n },\n ];\n }\n\n if (!expected.validator && live.validator) {\n const status = strict ? 'fail' : 'warn';\n issues.push({\n kind: 'extra_validator',\n table: collName,\n message: `Extra validator on collection \"${collName}\"`,\n });\n return [\n {\n status,\n kind: 'validator',\n name: 'validator',\n contractPath: `storage.collections.${collName}.validator`,\n code: 'EXTRA_VALIDATOR',\n message: 'Extra validator found',\n expected: null,\n actual: canonicalize(live.validator.jsonSchema),\n children: [],\n },\n ];\n }\n\n const liveVal = live.validator as NonNullable<typeof live.validator>;\n const expectedVal = expected.validator as NonNullable<typeof expected.validator>;\n const liveSchema = canonicalize(liveVal.jsonSchema);\n const expectedSchema = canonicalize(expectedVal.jsonSchema);\n\n if (\n liveSchema !== expectedSchema ||\n liveVal.validationLevel !== expectedVal.validationLevel ||\n liveVal.validationAction !== expectedVal.validationAction\n ) {\n issues.push({\n kind: 'type_mismatch',\n table: collName,\n expected: expectedSchema,\n actual: liveSchema,\n message: `Validator mismatch on collection \"${collName}\"`,\n });\n return [\n {\n status: 'fail',\n kind: 'validator',\n name: 'validator',\n contractPath: `storage.collections.${collName}.validator`,\n code: 'VALIDATOR_MISMATCH',\n message: 'Validator mismatch',\n expected: {\n jsonSchema: expectedVal.jsonSchema,\n validationLevel: expectedVal.validationLevel,\n validationAction: expectedVal.validationAction,\n },\n actual: {\n jsonSchema: liveVal.jsonSchema,\n validationLevel: liveVal.validationLevel,\n validationAction: liveVal.validationAction,\n },\n children: [],\n },\n ];\n }\n\n return [\n {\n status: 'pass',\n kind: 'validator',\n name: 'validator',\n contractPath: `storage.collections.${collName}.validator`,\n code: 'MATCH',\n message: 'Validator matches',\n expected: expectedSchema,\n actual: liveSchema,\n children: [],\n },\n ];\n}\n\nfunction diffOptions(\n collName: string,\n live: MongoSchemaCollection,\n expected: MongoSchemaCollection,\n strict: boolean,\n issues: SchemaIssue[],\n): SchemaVerificationNode[] {\n if (!live.options && !expected.options) return [];\n\n if (!expected.options && live.options) {\n const status = strict ? 'fail' : 'warn';\n issues.push({\n kind: 'type_mismatch',\n table: collName,\n actual: canonicalize(live.options),\n message: `Extra collection options on \"${collName}\"`,\n });\n return [\n {\n status,\n kind: 'options',\n name: 'options',\n contractPath: `storage.collections.${collName}.options`,\n code: 'EXTRA_OPTIONS',\n message: 'Extra collection options found',\n expected: null,\n actual: live.options,\n children: [],\n },\n ];\n }\n\n if (deepEqual(live.options, expected.options)) {\n return [\n {\n status: 'pass',\n kind: 'options',\n name: 'options',\n contractPath: `storage.collections.${collName}.options`,\n code: 'MATCH',\n message: 'Collection options match',\n expected: canonicalize(expected.options),\n actual: canonicalize(live.options),\n children: [],\n },\n ];\n }\n\n issues.push({\n kind: 'type_mismatch',\n table: collName,\n expected: canonicalize(expected.options),\n actual: canonicalize(live.options),\n message: `Collection options mismatch on \"${collName}\"`,\n });\n return [\n {\n status: 'fail',\n kind: 'options',\n name: 'options',\n contractPath: `storage.collections.${collName}.options`,\n code: 'OPTIONS_MISMATCH',\n message: 'Collection options mismatch',\n expected: expected.options,\n actual: live.options,\n children: [],\n },\n ];\n}\n","/**\n * Canonicalizes a live (introspected) `MongoSchemaIR` against the expected\n * (contract-built) IR before diffing. MongoDB applies server-side defaults\n * to several option/index families that are absent from authored contracts,\n * which would otherwise cause `verifyMongoSchema` to report false-positive\n * drift on a fresh `migration apply`.\n *\n * The normalization is contract-aware where it has to be: server defaults\n * are stripped from the live IR for fields the contract did not specify, so\n * a contract that *does* specify a value still gets compared faithfully.\n *\n * Symmetric defaults — like `changeStreamPreAndPostImages: { enabled: false }`,\n * which is equivalent to \"absent\" on both sides — are stripped from both IRs\n * so either authoring style verifies.\n */\n\nimport type {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n} from '@prisma-next/mongo-schema-ir';\nimport {\n MongoSchemaCollection as MongoSchemaCollectionCtor,\n MongoSchemaCollectionOptions as MongoSchemaCollectionOptionsCtor,\n MongoSchemaIndex as MongoSchemaIndexCtor,\n MongoSchemaIR as MongoSchemaIRCtor,\n} from '@prisma-next/mongo-schema-ir';\nimport { ifDefined } from '@prisma-next/utils/defined';\n\nexport interface CanonicalizedSchemas {\n readonly live: MongoSchemaIR;\n readonly expected: MongoSchemaIR;\n}\n\nexport function canonicalizeSchemasForVerification(\n live: MongoSchemaIR,\n expected: MongoSchemaIR,\n): CanonicalizedSchemas {\n const expectedByName = new Map<string, MongoSchemaCollection>();\n for (const c of expected.collections) expectedByName.set(c.name, c);\n\n const liveByName = new Map<string, MongoSchemaCollection>();\n for (const c of live.collections) liveByName.set(c.name, c);\n\n const canonicalLive = live.collections.map((c) =>\n canonicalizeLiveCollection(c, expectedByName.get(c.name)),\n );\n const canonicalExpected = expected.collections.map((c) =>\n canonicalizeExpectedCollection(c, liveByName.get(c.name)),\n );\n\n return {\n live: new MongoSchemaIRCtor(canonicalLive),\n expected: new MongoSchemaIRCtor(canonicalExpected),\n };\n}\n\nfunction canonicalizeLiveCollection(\n liveColl: MongoSchemaCollection,\n expectedColl: MongoSchemaCollection | undefined,\n): MongoSchemaCollection {\n const expectedIndexes = expectedColl?.indexes ?? [];\n const indexes = liveColl.indexes.map((idx) =>\n canonicalizeLiveIndex(idx, findExpectedIndexCounterpart(idx, expectedIndexes)),\n );\n\n const options = liveColl.options\n ? canonicalizeLiveOptions(liveColl.options, expectedColl?.options)\n : undefined;\n\n return new MongoSchemaCollectionCtor({\n name: liveColl.name,\n indexes,\n ...ifDefined('validator', liveColl.validator),\n ...ifDefined('options', options),\n });\n}\n\nfunction canonicalizeExpectedCollection(\n expectedColl: MongoSchemaCollection,\n liveColl: MongoSchemaCollection | undefined,\n): MongoSchemaCollection {\n // Symmetric text-index key ordering: a contract-shaped text index preserves\n // the user-authored field order, but the introspected counterpart comes\n // back from MongoDB with `weights` keys in alphabetical order, so we\n // canonicalize both sides to alphabetical text-key order. The order of\n // text fields within the text block is semantically irrelevant — relevance\n // is governed by `weights`, not key order.\n const indexes = expectedColl.indexes.map(canonicalizeTextIndexKeyOrder);\n\n const options = expectedColl.options\n ? canonicalizeExpectedOptions(expectedColl.options, liveColl?.options)\n : undefined;\n\n return new MongoSchemaCollectionCtor({\n name: expectedColl.name,\n indexes,\n ...ifDefined('validator', expectedColl.validator),\n ...ifDefined('options', options),\n });\n}\n\nfunction canonicalizeTextIndexKeyOrder(index: MongoSchemaIndex): MongoSchemaIndex {\n const hasTextKey = index.keys.some((k) => k.direction === 'text');\n if (!hasTextKey) return index;\n return new MongoSchemaIndexCtor({\n keys: sortTextKeys(index.keys),\n unique: index.unique,\n ...ifDefined('sparse', index.sparse),\n ...ifDefined('expireAfterSeconds', index.expireAfterSeconds),\n ...ifDefined('partialFilterExpression', index.partialFilterExpression),\n ...ifDefined('wildcardProjection', index.wildcardProjection),\n ...ifDefined('collation', index.collation),\n ...ifDefined('weights', index.weights),\n ...ifDefined('default_language', index.default_language),\n ...ifDefined('language_override', index.language_override),\n });\n}\n\n/**\n * Returns a copy of `keys` with text-direction entries sorted alphabetically\n * while preserving the relative position of non-text entries. Compound text\n * indexes (`{a: 1, _fts: 'text', _ftsx: 1, b: 1}`) keep their scalar\n * prefix/suffix layout; only the contiguous text block is reordered.\n */\nfunction sortTextKeys(\n keys: ReadonlyArray<{\n readonly field: string;\n readonly direction: 'text' | 1 | -1 | '2dsphere' | '2d' | 'hashed';\n }>,\n): ReadonlyArray<{\n readonly field: string;\n readonly direction: 'text' | 1 | -1 | '2dsphere' | '2d' | 'hashed';\n}> {\n const textEntries = keys.filter((k) => k.direction === 'text');\n if (textEntries.length <= 1) return keys;\n const sortedText = [...textEntries].sort((a, b) => a.field.localeCompare(b.field));\n let textIdx = 0;\n return keys.map((k) => {\n if (k.direction !== 'text') return k;\n const next = sortedText[textIdx++];\n /* v8 ignore next 3 -- @preserve invariant guard: textIdx is always < sortedText.length here because we only consume sortedText for text-direction entries and sortedText is built from the same filter. */\n if (next === undefined) {\n throw new Error('sortTextKeys: text-key counts mismatched');\n }\n return next;\n });\n}\n\nfunction canonicalizeLiveIndex(\n liveIndex: MongoSchemaIndex,\n expectedIndex: MongoSchemaIndex | undefined,\n): MongoSchemaIndex {\n const projectedKeys = sortTextKeys(projectTextIndexKeys(liveIndex));\n const collation = liveIndex.collation\n ? stripUnspecifiedFields(liveIndex.collation, expectedIndex?.collation)\n : liveIndex.collation;\n\n // Text-index server defaults: when the contract did not set\n // `weights`/`default_language`/`language_override`, MongoDB applies\n // `weights = {<field>: 1, ...}` (uniform), `'english'`, and `'language'`\n // respectively. Strip them from live *only* when the live value matches\n // those defaults — preserving non-default live values lets the verifier\n // surface drift when the live index is tampered (e.g. weights tuned\n // out-of-band, custom `default_language`/`language_override`) even though\n // the contract authored neither.\n const weights =\n expectedIndex?.weights === undefined && hasDefaultTextWeights(projectedKeys, liveIndex.weights)\n ? undefined\n : liveIndex.weights;\n const default_language =\n expectedIndex?.default_language === undefined && liveIndex.default_language === 'english'\n ? undefined\n : liveIndex.default_language;\n const language_override =\n expectedIndex?.language_override === undefined && liveIndex.language_override === 'language'\n ? undefined\n : liveIndex.language_override;\n\n return new MongoSchemaIndexCtor({\n keys: projectedKeys,\n unique: liveIndex.unique,\n ...ifDefined('sparse', liveIndex.sparse),\n ...ifDefined('expireAfterSeconds', liveIndex.expireAfterSeconds),\n ...ifDefined('partialFilterExpression', liveIndex.partialFilterExpression),\n ...ifDefined('wildcardProjection', liveIndex.wildcardProjection),\n ...ifDefined('collation', collation),\n ...ifDefined('weights', weights),\n ...ifDefined('default_language', default_language),\n ...ifDefined('language_override', language_override),\n });\n}\n\n/**\n * Locate the contract-side index that corresponds to a live index for the\n * purpose of contract-aware normalization. We deliberately match by the\n * *projected* (contract-shaped) key list — so a live `_fts/_ftsx` index\n * resolves to a contract `{title: 'text', body: 'text'}` index — and pick\n * the first match. Contracts very rarely contain duplicate-key indexes; if\n * we have no counterpart we fall back to no normalization for that index.\n */\nfunction findExpectedIndexCounterpart(\n liveIndex: MongoSchemaIndex,\n expectedIndexes: ReadonlyArray<MongoSchemaIndex>,\n): MongoSchemaIndex | undefined {\n const projectedLiveKeys = sortTextKeys(projectTextIndexKeys(liveIndex));\n const liveKeySig = projectedLiveKeys.map((k) => `${k.field}:${k.direction}`).join(',');\n for (const expected of expectedIndexes) {\n const expectedKeySig = sortTextKeys(expected.keys)\n .map((k) => `${k.field}:${k.direction}`)\n .join(',');\n if (expectedKeySig === liveKeySig) return expected;\n }\n return undefined;\n}\n\n/**\n * MongoDB expands a contract-shaped text index like\n * `[{title: 'text'}, {body: 'text'}]` into its internal weighted vector\n * representation `[{_fts: 'text'}, {_ftsx: 1}]`. We project back to\n * contract-shaped keys via `weights`, iterating in whatever order MongoDB\n * returns them (alphabetical, in practice). `sortTextKeys` is applied\n * downstream to canonicalize the order on both sides, so this projection\n * does not depend on a specific iteration order.\n */\nfunction projectTextIndexKeys(liveIndex: MongoSchemaIndex): ReadonlyArray<{\n readonly field: string;\n readonly direction: 'text' | 1 | -1 | '2dsphere' | '2d' | 'hashed';\n}> {\n const isTextIndex =\n liveIndex.keys.length >= 1 &&\n liveIndex.keys.some((k) => k.field === '_fts' && k.direction === 'text');\n\n if (!isTextIndex || !liveIndex.weights) return liveIndex.keys;\n\n const textKeys = Object.keys(liveIndex.weights).map((field) => ({\n field,\n direction: 'text' as const,\n }));\n\n // Splice the projected text fields into the original `_fts/_ftsx` slot so\n // compound text indexes that mix scalar prefixes *and* suffixes — e.g.\n // `[prefix, _fts, _ftsx, suffix]` — keep their original layout. Flattening\n // scalars first would yield `[prefix, suffix, ...text]`, which `sortTextKeys`\n // (downstream) cannot recover because it only reorders text-direction\n // entries within their existing positions. MongoDB always emits exactly one\n // `_fts`/`_ftsx` pair per index, so we don't need to guard against\n // duplicates.\n type IndexKey = (typeof liveIndex.keys)[number];\n const projectedKeys: IndexKey[] = [];\n for (const key of liveIndex.keys) {\n if (key.field === '_ftsx') continue;\n if (key.field === '_fts') {\n projectedKeys.push(...textKeys);\n continue;\n }\n projectedKeys.push(key);\n }\n return projectedKeys;\n}\n\n/**\n * MongoDB's server-default `weights` for an authored-without-weights text\n * index assigns `1` to every text-direction field. Returns `true` only when\n * `liveWeights` is exactly that uniform shape (every projected text-direction\n * key weighted at `1`) so the canonicalizer leaves non-default weights —\n * including out-of-band relevance tweaks — visible to the verifier.\n *\n * `projectTextIndexKeys` derives text-direction keys from the live weights\n * map, so the count is guaranteed to match; we only have to check the value\n * shape.\n */\nfunction hasDefaultTextWeights(\n projectedKeys: ReadonlyArray<{\n readonly field: string;\n readonly direction: 'text' | 1 | -1 | '2dsphere' | '2d' | 'hashed';\n }>,\n liveWeights: MongoSchemaIndex['weights'],\n): boolean {\n if (liveWeights === undefined) return false;\n const textFields = projectedKeys.filter((k) => k.direction === 'text').map((k) => k.field);\n return textFields.every((field) => liveWeights[field] === 1);\n}\n\nfunction canonicalizeLiveOptions(\n liveOptions: MongoSchemaCollectionOptions,\n expectedOptions: MongoSchemaCollectionOptions | undefined,\n): MongoSchemaCollectionOptions | undefined {\n const collation = liveOptions.collation\n ? stripUnspecifiedFields(liveOptions.collation, expectedOptions?.collation)\n : undefined;\n\n // Timeseries: drop `bucketMaxSpanSeconds` (and any other server-applied\n // extras) when the contract did not specify them.\n const timeseries = liveOptions.timeseries\n ? (stripUnspecifiedFields(\n liveOptions.timeseries as Record<string, unknown>,\n expectedOptions?.timeseries as Record<string, unknown> | undefined,\n ) as MongoSchemaCollectionOptions['timeseries'])\n : undefined;\n\n // ClusteredIndex: drop `key`, `unique`, `v` and any other server-applied\n // extras when the contract did not specify them.\n const clusteredIndex = liveOptions.clusteredIndex\n ? (stripUnspecifiedFields(\n liveOptions.clusteredIndex as Record<string, unknown>,\n expectedOptions?.clusteredIndex as Record<string, unknown> | undefined,\n ) as MongoSchemaCollectionOptions['clusteredIndex'])\n : undefined;\n\n // changeStreamPreAndPostImages: `{enabled: false}` is equivalent to\n // \"absent\". Strip it from live so it round-trips with a contract that\n // omits the field, and is symmetric with the expected-side stripping.\n const changeStreamPreAndPostImages = isDisabledChangeStream(\n liveOptions.changeStreamPreAndPostImages,\n )\n ? undefined\n : liveOptions.changeStreamPreAndPostImages;\n\n const hasMeaningful =\n liveOptions.capped || timeseries || collation || changeStreamPreAndPostImages || clusteredIndex;\n if (!hasMeaningful) return undefined;\n\n return new MongoSchemaCollectionOptionsCtor({\n ...ifDefined('capped', liveOptions.capped),\n ...ifDefined('timeseries', timeseries),\n ...ifDefined('collation', collation),\n ...ifDefined('changeStreamPreAndPostImages', changeStreamPreAndPostImages),\n ...ifDefined('clusteredIndex', clusteredIndex),\n });\n}\n\nfunction canonicalizeExpectedOptions(\n expectedOptions: MongoSchemaCollectionOptions,\n _liveOptions: MongoSchemaCollectionOptions | undefined,\n): MongoSchemaCollectionOptions | undefined {\n // Symmetric: a contract `{enabled: false}` is equivalent to absent.\n const changeStreamPreAndPostImages = isDisabledChangeStream(\n expectedOptions.changeStreamPreAndPostImages,\n )\n ? undefined\n : expectedOptions.changeStreamPreAndPostImages;\n\n const hasMeaningful =\n expectedOptions.capped ||\n expectedOptions.timeseries ||\n expectedOptions.collation ||\n changeStreamPreAndPostImages ||\n expectedOptions.clusteredIndex;\n if (!hasMeaningful) return undefined;\n\n return new MongoSchemaCollectionOptionsCtor({\n ...ifDefined('capped', expectedOptions.capped),\n ...ifDefined('timeseries', expectedOptions.timeseries),\n ...ifDefined('collation', expectedOptions.collation),\n ...ifDefined('changeStreamPreAndPostImages', changeStreamPreAndPostImages),\n ...ifDefined('clusteredIndex', expectedOptions.clusteredIndex),\n });\n}\n\nfunction isDisabledChangeStream(value: { enabled: boolean } | undefined): boolean {\n return value !== undefined && value.enabled === false;\n}\n\n/**\n * Returns a copy of `live` containing only the keys that `expected` defines.\n * Used for option families whose individual sub-fields are server-extended\n * with platform defaults (collation, timeseries, clusteredIndex), so the\n * verifier should compare only what the contract actually authored.\n *\n * When `expected` is `undefined` — i.e. the contract authored nothing for\n * this whole option family but the live IR has it — we return `live`\n * unchanged so the verifier still sees the entire live block and can\n * surface it as drift. (Returning `undefined` here would silently strip a\n * server-attached collation/timeseries/clusteredIndex that the contract\n * never asked for, hiding real drift.)\n */\nfunction stripUnspecifiedFields(\n live: Record<string, unknown>,\n expected: Record<string, unknown> | undefined,\n): Record<string, unknown> {\n if (expected === undefined) return live;\n const out: Record<string, unknown> = {};\n for (const key of Object.keys(expected)) {\n if (Object.hasOwn(live, key)) out[key] = live[key];\n }\n return out;\n}\n","import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';\nimport type {\n OperationContext,\n VerifyDatabaseSchemaResult,\n} from '@prisma-next/framework-components/control';\nimport { VERIFY_CODE_SCHEMA_FAILURE } from '@prisma-next/framework-components/control';\nimport type { MongoContract } from '@prisma-next/mongo-contract';\nimport type { MongoSchemaIR } from '@prisma-next/mongo-schema-ir';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport { contractToMongoSchemaIR } from '../contract-to-schema';\nimport { diffMongoSchemas } from '../schema-diff';\nimport { canonicalizeSchemasForVerification } from './canonicalize-introspection';\n\nexport interface VerifyMongoSchemaOptions {\n readonly contract: MongoContract;\n readonly schema: MongoSchemaIR;\n readonly strict: boolean;\n readonly context?: OperationContext;\n /**\n * Active framework components participating in this composition. Mongo\n * verification does not currently consult them, but the parameter exists\n * for parity with `verifySqlSchema` so callers can pass the same envelope.\n */\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', string>>;\n}\n\nexport function verifyMongoSchema(options: VerifyMongoSchemaOptions): VerifyDatabaseSchemaResult {\n const { contract, schema, strict, context } = options;\n const startTime = Date.now();\n\n const expectedIR = contractToMongoSchemaIR(contract);\n // Strip server-applied defaults (and authored equivalents) before diffing so\n // the verifier compares like-with-like — see `canonicalize-introspection.ts`.\n const { live: canonicalLive, expected: canonicalExpected } = canonicalizeSchemasForVerification(\n schema,\n expectedIR,\n );\n const { root, issues, counts } = diffMongoSchemas(canonicalLive, canonicalExpected, strict);\n\n const ok = counts.fail === 0;\n const profileHash = typeof contract.profileHash === 'string' ? contract.profileHash : '';\n\n return {\n ok,\n ...ifDefined('code', ok ? undefined : VERIFY_CODE_SCHEMA_FAILURE),\n summary: ok ? 'Schema matches contract' : `Schema verification found ${counts.fail} issue(s)`,\n contract: {\n storageHash: contract.storage.storageHash,\n ...(profileHash ? { profileHash } : {}),\n },\n target: { expected: contract.target },\n schema: { issues, root, counts },\n meta: {\n strict,\n ...ifDefined('contractPath', context?.contractPath),\n ...ifDefined('configPath', context?.configPath),\n },\n timings: { total: Date.now() - startTime },\n };\n}\n"],"mappings":";;;;;AAeA,SAAS,aAAa,OAA4C;AAChE,QAAO,IAAI,iBAAiB;EAC1B,MAAM,MAAM;EACZ,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,oBAAoB,MAAM;EAC1B,yBAAyB,MAAM;EAC/B,oBAAoB,MAAM;EAC1B,WAAW,MAAM;EACjB,SAAS,MAAM;EACf,kBAAkB,MAAM;EACxB,mBAAmB,MAAM;EAC1B,CAAC;;AAGJ,SAAS,iBAAiB,GAAgD;AACxE,QAAO,IAAI,qBAAqB;EAC9B,YAAY,EAAE;EACd,iBAAiB,EAAE;EACnB,kBAAkB,EAAE;EACrB,CAAC;;AAGJ,SAAS,eAAe,GAAgE;AACtF,QAAO,IAAI,6BAA6B,EAAE;;AAG5C,SAAS,kBAAkB,MAAc,KAAoD;AAE3F,QAAO,IAAI,sBAAsB;EAC/B;EACA,UAHe,IAAI,WAAW,EAAE,EAAE,IAAI,aAAa;EAInD,GAAI,IAAI,aAAa,QAAQ,EAAE,WAAW,iBAAiB,IAAI,UAAU,EAAE;EAC3E,GAAI,IAAI,WAAW,QAAQ,EAAE,SAAS,eAAe,IAAI,QAAQ,EAAE;EACpE,CAAC;;AAGJ,SAAgB,wBAAwB,UAA+C;AACrF,KAAI,CAAC,SACH,QAAO,IAAI,cAAc,EAAE,CAAC;AAO9B,QAAO,IAAI,cAJS,OAAO,QAAQ,SAAS,QAAQ,YAAY,CA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,cAAc,SAAS,WAAW;GAChD;EACD,SAAS,EAAE,OAAO,KAAK,KAAK,GAAG,WAAW;EAC3C"}
|
package/package.json
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/target-mongo",
|
|
3
|
-
"version": "0.5.0-dev.
|
|
3
|
+
"version": "0.5.0-dev.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "MongoDB target pack for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"arktype": "^2.1.29",
|
|
9
9
|
"mongodb": "^6.16.0",
|
|
10
|
-
"@prisma-next/
|
|
11
|
-
"@prisma-next/
|
|
12
|
-
"@prisma-next/
|
|
13
|
-
"@prisma-next/
|
|
14
|
-
"@prisma-next/
|
|
15
|
-
"@prisma-next/mongo-
|
|
16
|
-
"@prisma-next/
|
|
17
|
-
"@prisma-next/mongo-query-ast": "0.5.0-dev.
|
|
18
|
-
"@prisma-next/mongo-schema-ir": "0.5.0-dev.
|
|
19
|
-
"@prisma-next/mongo-value": "0.5.0-dev.
|
|
20
|
-
"@prisma-next/utils": "0.5.0-dev.
|
|
10
|
+
"@prisma-next/contract": "0.5.0-dev.6",
|
|
11
|
+
"@prisma-next/errors": "0.5.0-dev.6",
|
|
12
|
+
"@prisma-next/framework-components": "0.5.0-dev.6",
|
|
13
|
+
"@prisma-next/migration-tools": "0.5.0-dev.6",
|
|
14
|
+
"@prisma-next/ts-render": "0.5.0-dev.6",
|
|
15
|
+
"@prisma-next/mongo-contract": "0.5.0-dev.6",
|
|
16
|
+
"@prisma-next/mongo-lowering": "0.5.0-dev.6",
|
|
17
|
+
"@prisma-next/mongo-query-ast": "0.5.0-dev.6",
|
|
18
|
+
"@prisma-next/mongo-schema-ir": "0.5.0-dev.6",
|
|
19
|
+
"@prisma-next/mongo-value": "0.5.0-dev.6",
|
|
20
|
+
"@prisma-next/utils": "0.5.0-dev.6"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"mongodb-memory-server": "10.4.3",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"tsdown": "0.18.4",
|
|
26
26
|
"typescript": "5.9.3",
|
|
27
27
|
"vitest": "4.0.17",
|
|
28
|
-
"@prisma-next/tsdown": "0.0.0",
|
|
29
28
|
"@prisma-next/tsconfig": "0.0.0",
|
|
30
|
-
"@prisma-next/test-utils": "0.0.1"
|
|
29
|
+
"@prisma-next/test-utils": "0.0.1",
|
|
30
|
+
"@prisma-next/tsdown": "0.0.0"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"dist",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"./control": "./dist/control.mjs",
|
|
39
39
|
"./migration": "./dist/migration.mjs",
|
|
40
40
|
"./pack": "./dist/pack.mjs",
|
|
41
|
+
"./schema-verify": "./dist/schema-verify.mjs",
|
|
41
42
|
"./package.json": "./package.json"
|
|
42
43
|
},
|
|
43
44
|
"repository": {
|
package/src/core/mongo-runner.ts
CHANGED
|
@@ -8,7 +8,9 @@ import type {
|
|
|
8
8
|
MigrationRunnerExecutionChecks,
|
|
9
9
|
MigrationRunnerFailure,
|
|
10
10
|
MigrationRunnerResult,
|
|
11
|
+
OperationContext,
|
|
11
12
|
} from '@prisma-next/framework-components/control';
|
|
13
|
+
import type { MongoContract } from '@prisma-next/mongo-contract';
|
|
12
14
|
import type { MongoAdapter, MongoDriver } from '@prisma-next/mongo-lowering';
|
|
13
15
|
import type {
|
|
14
16
|
AnyMongoMigrationOperation,
|
|
@@ -19,21 +21,13 @@ import type {
|
|
|
19
21
|
MongoMigrationCheck,
|
|
20
22
|
MongoMigrationPlanOperation,
|
|
21
23
|
} from '@prisma-next/mongo-query-ast/control';
|
|
24
|
+
import type { MongoSchemaIR } from '@prisma-next/mongo-schema-ir';
|
|
22
25
|
import { notOk, ok } from '@prisma-next/utils/result';
|
|
23
|
-
|
|
24
|
-
const READ_ONLY_CHECK_COMMAND_KINDS: ReadonlySet<string> = new Set(['aggregate', 'rawAggregate']);
|
|
25
|
-
|
|
26
|
-
function hasProfileHash(value: unknown): value is { readonly profileHash: string } {
|
|
27
|
-
return (
|
|
28
|
-
typeof value === 'object' &&
|
|
29
|
-
value !== null &&
|
|
30
|
-
Object.hasOwn(value, 'profileHash') &&
|
|
31
|
-
typeof (value as { profileHash: unknown }).profileHash === 'string'
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
26
|
import { FilterEvaluator } from './filter-evaluator';
|
|
36
27
|
import { deserializeMongoOps } from './mongo-ops-serializer';
|
|
28
|
+
import { verifyMongoSchema } from './schema-verify/verify-mongo-schema';
|
|
29
|
+
|
|
30
|
+
const READ_ONLY_CHECK_COMMAND_KINDS: ReadonlySet<string> = new Set(['aggregate', 'rawAggregate']);
|
|
37
31
|
|
|
38
32
|
export interface MarkerOperations {
|
|
39
33
|
readMarker(): Promise<ContractMarkerRecord | null>;
|
|
@@ -58,6 +52,21 @@ export interface MongoRunnerDependencies {
|
|
|
58
52
|
readonly adapter: MongoAdapter;
|
|
59
53
|
readonly driver: MongoDriver;
|
|
60
54
|
readonly markerOps: MarkerOperations;
|
|
55
|
+
readonly introspectSchema: () => Promise<MongoSchemaIR>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface MongoMigrationRunnerExecuteOptions {
|
|
59
|
+
readonly plan: MigrationPlan;
|
|
60
|
+
readonly destinationContract: MongoContract;
|
|
61
|
+
readonly policy: MigrationOperationPolicy;
|
|
62
|
+
readonly callbacks?: {
|
|
63
|
+
onOperationStart?(op: MigrationPlanOperation): void;
|
|
64
|
+
onOperationComplete?(op: MigrationPlanOperation): void;
|
|
65
|
+
};
|
|
66
|
+
readonly executionChecks?: MigrationRunnerExecutionChecks;
|
|
67
|
+
readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;
|
|
68
|
+
readonly strictVerification?: boolean;
|
|
69
|
+
readonly context?: OperationContext;
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
function runnerFailure(
|
|
@@ -75,17 +84,7 @@ function runnerFailure(
|
|
|
75
84
|
export class MongoMigrationRunner {
|
|
76
85
|
constructor(private readonly deps: MongoRunnerDependencies) {}
|
|
77
86
|
|
|
78
|
-
async execute(options: {
|
|
79
|
-
readonly plan: MigrationPlan;
|
|
80
|
-
readonly destinationContract: unknown;
|
|
81
|
-
readonly policy: MigrationOperationPolicy;
|
|
82
|
-
readonly callbacks?: {
|
|
83
|
-
onOperationStart?(op: MigrationPlanOperation): void;
|
|
84
|
-
onOperationComplete?(op: MigrationPlanOperation): void;
|
|
85
|
-
};
|
|
86
|
-
readonly executionChecks?: MigrationRunnerExecutionChecks;
|
|
87
|
-
readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;
|
|
88
|
-
}): Promise<MigrationRunnerResult> {
|
|
87
|
+
async execute(options: MongoMigrationRunnerExecuteOptions): Promise<MigrationRunnerResult> {
|
|
89
88
|
const { commandExecutor, inspectionExecutor, adapter, driver, markerOps } = this.deps;
|
|
90
89
|
const operations = deserializeMongoOps(options.plan.operations as readonly unknown[]);
|
|
91
90
|
|
|
@@ -176,9 +175,7 @@ export class MongoMigrationRunner {
|
|
|
176
175
|
}
|
|
177
176
|
|
|
178
177
|
const destination = options.plan.destination;
|
|
179
|
-
const profileHash =
|
|
180
|
-
? options.destinationContract.profileHash
|
|
181
|
-
: destination.storageHash;
|
|
178
|
+
const profileHash = options.destinationContract.profileHash ?? destination.storageHash;
|
|
182
179
|
|
|
183
180
|
if (
|
|
184
181
|
operationsExecuted === 0 &&
|
|
@@ -188,6 +185,21 @@ export class MongoMigrationRunner {
|
|
|
188
185
|
return ok({ operationsPlanned: operations.length, operationsExecuted });
|
|
189
186
|
}
|
|
190
187
|
|
|
188
|
+
const liveSchema = await this.deps.introspectSchema();
|
|
189
|
+
const verifyResult = verifyMongoSchema({
|
|
190
|
+
contract: options.destinationContract,
|
|
191
|
+
schema: liveSchema,
|
|
192
|
+
strict: options.strictVerification ?? true,
|
|
193
|
+
frameworkComponents: options.frameworkComponents,
|
|
194
|
+
...(options.context ? { context: options.context } : {}),
|
|
195
|
+
});
|
|
196
|
+
if (!verifyResult.ok) {
|
|
197
|
+
return runnerFailure('SCHEMA_VERIFY_FAILED', verifyResult.summary, {
|
|
198
|
+
why: 'The resulting database schema does not satisfy the destination contract.',
|
|
199
|
+
meta: { issues: verifyResult.schema.issues },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
191
203
|
if (existingMarker) {
|
|
192
204
|
const updated = await markerOps.updateMarker(existingMarker.storageHash, {
|
|
193
205
|
storageHash: destination.storageHash,
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SchemaIssue,
|
|
3
|
+
SchemaVerificationNode,
|
|
4
|
+
} from '@prisma-next/framework-components/control';
|
|
5
|
+
import type {
|
|
6
|
+
MongoSchemaCollection,
|
|
7
|
+
MongoSchemaIndex,
|
|
8
|
+
MongoSchemaIR,
|
|
9
|
+
} from '@prisma-next/mongo-schema-ir';
|
|
10
|
+
import { canonicalize, deepEqual } from '@prisma-next/mongo-schema-ir';
|
|
11
|
+
|
|
12
|
+
export function diffMongoSchemas(
|
|
13
|
+
live: MongoSchemaIR,
|
|
14
|
+
expected: MongoSchemaIR,
|
|
15
|
+
strict: boolean,
|
|
16
|
+
): {
|
|
17
|
+
root: SchemaVerificationNode;
|
|
18
|
+
issues: SchemaIssue[];
|
|
19
|
+
counts: { pass: number; warn: number; fail: number; totalNodes: number };
|
|
20
|
+
} {
|
|
21
|
+
const issues: SchemaIssue[] = [];
|
|
22
|
+
const collectionChildren: SchemaVerificationNode[] = [];
|
|
23
|
+
let pass = 0;
|
|
24
|
+
let warn = 0;
|
|
25
|
+
let fail = 0;
|
|
26
|
+
|
|
27
|
+
const allNames = new Set([...live.collectionNames, ...expected.collectionNames]);
|
|
28
|
+
|
|
29
|
+
for (const name of [...allNames].sort()) {
|
|
30
|
+
const liveColl = live.collection(name);
|
|
31
|
+
const expectedColl = expected.collection(name);
|
|
32
|
+
|
|
33
|
+
if (!liveColl && expectedColl) {
|
|
34
|
+
issues.push({
|
|
35
|
+
kind: 'missing_table',
|
|
36
|
+
table: name,
|
|
37
|
+
message: `Collection "${name}" is missing from the database`,
|
|
38
|
+
});
|
|
39
|
+
collectionChildren.push({
|
|
40
|
+
status: 'fail',
|
|
41
|
+
kind: 'collection',
|
|
42
|
+
name,
|
|
43
|
+
contractPath: `storage.collections.${name}`,
|
|
44
|
+
code: 'MISSING_COLLECTION',
|
|
45
|
+
message: `Collection "${name}" is missing`,
|
|
46
|
+
expected: name,
|
|
47
|
+
actual: null,
|
|
48
|
+
children: [],
|
|
49
|
+
});
|
|
50
|
+
fail++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (liveColl && !expectedColl) {
|
|
55
|
+
const status = strict ? 'fail' : 'warn';
|
|
56
|
+
issues.push({
|
|
57
|
+
kind: 'extra_table',
|
|
58
|
+
table: name,
|
|
59
|
+
message: `Extra collection "${name}" exists in the database but not in the contract`,
|
|
60
|
+
});
|
|
61
|
+
collectionChildren.push({
|
|
62
|
+
status,
|
|
63
|
+
kind: 'collection',
|
|
64
|
+
name,
|
|
65
|
+
contractPath: `storage.collections.${name}`,
|
|
66
|
+
code: 'EXTRA_COLLECTION',
|
|
67
|
+
message: `Extra collection "${name}" found`,
|
|
68
|
+
expected: null,
|
|
69
|
+
actual: name,
|
|
70
|
+
children: [],
|
|
71
|
+
});
|
|
72
|
+
if (status === 'fail') fail++;
|
|
73
|
+
else warn++;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const lc = liveColl as MongoSchemaCollection;
|
|
78
|
+
const ec = expectedColl as MongoSchemaCollection;
|
|
79
|
+
const indexChildren = diffIndexes(name, lc, ec, strict, issues);
|
|
80
|
+
const validatorChildren = diffValidator(name, lc, ec, strict, issues);
|
|
81
|
+
const optionsChildren = diffOptions(name, lc, ec, strict, issues);
|
|
82
|
+
const children = [...indexChildren, ...validatorChildren, ...optionsChildren];
|
|
83
|
+
|
|
84
|
+
const worstStatus = children.reduce<'pass' | 'warn' | 'fail'>(
|
|
85
|
+
(s, c) => (c.status === 'fail' ? 'fail' : c.status === 'warn' && s !== 'fail' ? 'warn' : s),
|
|
86
|
+
'pass',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
for (const c of children) {
|
|
90
|
+
if (c.status === 'pass') pass++;
|
|
91
|
+
else if (c.status === 'warn') warn++;
|
|
92
|
+
else fail++;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (children.length === 0) {
|
|
96
|
+
pass++;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
collectionChildren.push({
|
|
100
|
+
status: worstStatus,
|
|
101
|
+
kind: 'collection',
|
|
102
|
+
name,
|
|
103
|
+
contractPath: `storage.collections.${name}`,
|
|
104
|
+
code: worstStatus === 'pass' ? 'MATCH' : 'DRIFT',
|
|
105
|
+
message:
|
|
106
|
+
worstStatus === 'pass' ? `Collection "${name}" matches` : `Collection "${name}" has drift`,
|
|
107
|
+
expected: name,
|
|
108
|
+
actual: name,
|
|
109
|
+
children,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const rootStatus = fail > 0 ? 'fail' : warn > 0 ? 'warn' : 'pass';
|
|
114
|
+
const totalNodes = pass + warn + fail + collectionChildren.length;
|
|
115
|
+
|
|
116
|
+
const root: SchemaVerificationNode = {
|
|
117
|
+
status: rootStatus,
|
|
118
|
+
kind: 'root',
|
|
119
|
+
name: 'mongo-schema',
|
|
120
|
+
contractPath: 'storage',
|
|
121
|
+
code: rootStatus === 'pass' ? 'MATCH' : 'DRIFT',
|
|
122
|
+
message: rootStatus === 'pass' ? 'Schema matches' : 'Schema has drift',
|
|
123
|
+
expected: null,
|
|
124
|
+
actual: null,
|
|
125
|
+
children: collectionChildren,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return { root, issues, counts: { pass, warn, fail, totalNodes } };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function buildIndexLookupKey(index: MongoSchemaIndex): string {
|
|
132
|
+
const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');
|
|
133
|
+
const opts = [
|
|
134
|
+
index.unique ? 'unique' : '',
|
|
135
|
+
index.sparse ? 'sparse' : '',
|
|
136
|
+
index.expireAfterSeconds != null ? `ttl:${index.expireAfterSeconds}` : '',
|
|
137
|
+
index.partialFilterExpression ? `pfe:${canonicalize(index.partialFilterExpression)}` : '',
|
|
138
|
+
index.wildcardProjection ? `wp:${canonicalize(index.wildcardProjection)}` : '',
|
|
139
|
+
index.collation ? `col:${canonicalize(index.collation)}` : '',
|
|
140
|
+
index.weights ? `wt:${canonicalize(index.weights)}` : '',
|
|
141
|
+
index.default_language ? `dl:${index.default_language}` : '',
|
|
142
|
+
index.language_override ? `lo:${index.language_override}` : '',
|
|
143
|
+
]
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.join(';');
|
|
146
|
+
return opts ? `${keys}|${opts}` : keys;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function formatIndexName(index: MongoSchemaIndex): string {
|
|
150
|
+
return index.keys.map((k) => `${k.field}:${k.direction}`).join(', ');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function diffIndexes(
|
|
154
|
+
collName: string,
|
|
155
|
+
live: MongoSchemaCollection,
|
|
156
|
+
expected: MongoSchemaCollection,
|
|
157
|
+
strict: boolean,
|
|
158
|
+
issues: SchemaIssue[],
|
|
159
|
+
): SchemaVerificationNode[] {
|
|
160
|
+
const nodes: SchemaVerificationNode[] = [];
|
|
161
|
+
const liveLookup = new Map<string, MongoSchemaIndex>();
|
|
162
|
+
for (const idx of live.indexes) liveLookup.set(buildIndexLookupKey(idx), idx);
|
|
163
|
+
|
|
164
|
+
const expectedLookup = new Map<string, MongoSchemaIndex>();
|
|
165
|
+
for (const idx of expected.indexes) expectedLookup.set(buildIndexLookupKey(idx), idx);
|
|
166
|
+
|
|
167
|
+
for (const [key, idx] of expectedLookup) {
|
|
168
|
+
if (liveLookup.has(key)) {
|
|
169
|
+
nodes.push({
|
|
170
|
+
status: 'pass',
|
|
171
|
+
kind: 'index',
|
|
172
|
+
name: formatIndexName(idx),
|
|
173
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
174
|
+
code: 'MATCH',
|
|
175
|
+
message: `Index ${formatIndexName(idx)} matches`,
|
|
176
|
+
expected: key,
|
|
177
|
+
actual: key,
|
|
178
|
+
children: [],
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
issues.push({
|
|
182
|
+
kind: 'index_mismatch',
|
|
183
|
+
table: collName,
|
|
184
|
+
indexOrConstraint: formatIndexName(idx),
|
|
185
|
+
message: `Index ${formatIndexName(idx)} missing on collection "${collName}"`,
|
|
186
|
+
});
|
|
187
|
+
nodes.push({
|
|
188
|
+
status: 'fail',
|
|
189
|
+
kind: 'index',
|
|
190
|
+
name: formatIndexName(idx),
|
|
191
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
192
|
+
code: 'MISSING_INDEX',
|
|
193
|
+
message: `Index ${formatIndexName(idx)} missing`,
|
|
194
|
+
expected: key,
|
|
195
|
+
actual: null,
|
|
196
|
+
children: [],
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
for (const [key, idx] of liveLookup) {
|
|
202
|
+
if (!expectedLookup.has(key)) {
|
|
203
|
+
const status = strict ? 'fail' : 'warn';
|
|
204
|
+
issues.push({
|
|
205
|
+
kind: 'extra_index',
|
|
206
|
+
table: collName,
|
|
207
|
+
indexOrConstraint: formatIndexName(idx),
|
|
208
|
+
message: `Extra index ${formatIndexName(idx)} on collection "${collName}"`,
|
|
209
|
+
});
|
|
210
|
+
nodes.push({
|
|
211
|
+
status,
|
|
212
|
+
kind: 'index',
|
|
213
|
+
name: formatIndexName(idx),
|
|
214
|
+
contractPath: `storage.collections.${collName}.indexes`,
|
|
215
|
+
code: 'EXTRA_INDEX',
|
|
216
|
+
message: `Extra index ${formatIndexName(idx)}`,
|
|
217
|
+
expected: null,
|
|
218
|
+
actual: key,
|
|
219
|
+
children: [],
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return nodes;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function diffValidator(
|
|
228
|
+
collName: string,
|
|
229
|
+
live: MongoSchemaCollection,
|
|
230
|
+
expected: MongoSchemaCollection,
|
|
231
|
+
strict: boolean,
|
|
232
|
+
issues: SchemaIssue[],
|
|
233
|
+
): SchemaVerificationNode[] {
|
|
234
|
+
if (!live.validator && !expected.validator) return [];
|
|
235
|
+
|
|
236
|
+
if (expected.validator && !live.validator) {
|
|
237
|
+
issues.push({
|
|
238
|
+
kind: 'type_missing',
|
|
239
|
+
table: collName,
|
|
240
|
+
message: `Validator missing on collection "${collName}"`,
|
|
241
|
+
});
|
|
242
|
+
return [
|
|
243
|
+
{
|
|
244
|
+
status: 'fail',
|
|
245
|
+
kind: 'validator',
|
|
246
|
+
name: 'validator',
|
|
247
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
248
|
+
code: 'MISSING_VALIDATOR',
|
|
249
|
+
message: 'Validator missing',
|
|
250
|
+
expected: canonicalize(expected.validator.jsonSchema),
|
|
251
|
+
actual: null,
|
|
252
|
+
children: [],
|
|
253
|
+
},
|
|
254
|
+
];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (!expected.validator && live.validator) {
|
|
258
|
+
const status = strict ? 'fail' : 'warn';
|
|
259
|
+
issues.push({
|
|
260
|
+
kind: 'extra_validator',
|
|
261
|
+
table: collName,
|
|
262
|
+
message: `Extra validator on collection "${collName}"`,
|
|
263
|
+
});
|
|
264
|
+
return [
|
|
265
|
+
{
|
|
266
|
+
status,
|
|
267
|
+
kind: 'validator',
|
|
268
|
+
name: 'validator',
|
|
269
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
270
|
+
code: 'EXTRA_VALIDATOR',
|
|
271
|
+
message: 'Extra validator found',
|
|
272
|
+
expected: null,
|
|
273
|
+
actual: canonicalize(live.validator.jsonSchema),
|
|
274
|
+
children: [],
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const liveVal = live.validator as NonNullable<typeof live.validator>;
|
|
280
|
+
const expectedVal = expected.validator as NonNullable<typeof expected.validator>;
|
|
281
|
+
const liveSchema = canonicalize(liveVal.jsonSchema);
|
|
282
|
+
const expectedSchema = canonicalize(expectedVal.jsonSchema);
|
|
283
|
+
|
|
284
|
+
if (
|
|
285
|
+
liveSchema !== expectedSchema ||
|
|
286
|
+
liveVal.validationLevel !== expectedVal.validationLevel ||
|
|
287
|
+
liveVal.validationAction !== expectedVal.validationAction
|
|
288
|
+
) {
|
|
289
|
+
issues.push({
|
|
290
|
+
kind: 'type_mismatch',
|
|
291
|
+
table: collName,
|
|
292
|
+
expected: expectedSchema,
|
|
293
|
+
actual: liveSchema,
|
|
294
|
+
message: `Validator mismatch on collection "${collName}"`,
|
|
295
|
+
});
|
|
296
|
+
return [
|
|
297
|
+
{
|
|
298
|
+
status: 'fail',
|
|
299
|
+
kind: 'validator',
|
|
300
|
+
name: 'validator',
|
|
301
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
302
|
+
code: 'VALIDATOR_MISMATCH',
|
|
303
|
+
message: 'Validator mismatch',
|
|
304
|
+
expected: {
|
|
305
|
+
jsonSchema: expectedVal.jsonSchema,
|
|
306
|
+
validationLevel: expectedVal.validationLevel,
|
|
307
|
+
validationAction: expectedVal.validationAction,
|
|
308
|
+
},
|
|
309
|
+
actual: {
|
|
310
|
+
jsonSchema: liveVal.jsonSchema,
|
|
311
|
+
validationLevel: liveVal.validationLevel,
|
|
312
|
+
validationAction: liveVal.validationAction,
|
|
313
|
+
},
|
|
314
|
+
children: [],
|
|
315
|
+
},
|
|
316
|
+
];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return [
|
|
320
|
+
{
|
|
321
|
+
status: 'pass',
|
|
322
|
+
kind: 'validator',
|
|
323
|
+
name: 'validator',
|
|
324
|
+
contractPath: `storage.collections.${collName}.validator`,
|
|
325
|
+
code: 'MATCH',
|
|
326
|
+
message: 'Validator matches',
|
|
327
|
+
expected: expectedSchema,
|
|
328
|
+
actual: liveSchema,
|
|
329
|
+
children: [],
|
|
330
|
+
},
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function diffOptions(
|
|
335
|
+
collName: string,
|
|
336
|
+
live: MongoSchemaCollection,
|
|
337
|
+
expected: MongoSchemaCollection,
|
|
338
|
+
strict: boolean,
|
|
339
|
+
issues: SchemaIssue[],
|
|
340
|
+
): SchemaVerificationNode[] {
|
|
341
|
+
if (!live.options && !expected.options) return [];
|
|
342
|
+
|
|
343
|
+
if (!expected.options && live.options) {
|
|
344
|
+
const status = strict ? 'fail' : 'warn';
|
|
345
|
+
issues.push({
|
|
346
|
+
kind: 'type_mismatch',
|
|
347
|
+
table: collName,
|
|
348
|
+
actual: canonicalize(live.options),
|
|
349
|
+
message: `Extra collection options on "${collName}"`,
|
|
350
|
+
});
|
|
351
|
+
return [
|
|
352
|
+
{
|
|
353
|
+
status,
|
|
354
|
+
kind: 'options',
|
|
355
|
+
name: 'options',
|
|
356
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
357
|
+
code: 'EXTRA_OPTIONS',
|
|
358
|
+
message: 'Extra collection options found',
|
|
359
|
+
expected: null,
|
|
360
|
+
actual: live.options,
|
|
361
|
+
children: [],
|
|
362
|
+
},
|
|
363
|
+
];
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (deepEqual(live.options, expected.options)) {
|
|
367
|
+
return [
|
|
368
|
+
{
|
|
369
|
+
status: 'pass',
|
|
370
|
+
kind: 'options',
|
|
371
|
+
name: 'options',
|
|
372
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
373
|
+
code: 'MATCH',
|
|
374
|
+
message: 'Collection options match',
|
|
375
|
+
expected: canonicalize(expected.options),
|
|
376
|
+
actual: canonicalize(live.options),
|
|
377
|
+
children: [],
|
|
378
|
+
},
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
issues.push({
|
|
383
|
+
kind: 'type_mismatch',
|
|
384
|
+
table: collName,
|
|
385
|
+
expected: canonicalize(expected.options),
|
|
386
|
+
actual: canonicalize(live.options),
|
|
387
|
+
message: `Collection options mismatch on "${collName}"`,
|
|
388
|
+
});
|
|
389
|
+
return [
|
|
390
|
+
{
|
|
391
|
+
status: 'fail',
|
|
392
|
+
kind: 'options',
|
|
393
|
+
name: 'options',
|
|
394
|
+
contractPath: `storage.collections.${collName}.options`,
|
|
395
|
+
code: 'OPTIONS_MISMATCH',
|
|
396
|
+
message: 'Collection options mismatch',
|
|
397
|
+
expected: expected.options,
|
|
398
|
+
actual: live.options,
|
|
399
|
+
children: [],
|
|
400
|
+
},
|
|
401
|
+
];
|
|
402
|
+
}
|