MCPcopy
hub / github.com/directus/directus / applyDiff

Function applyDiff

api/src/utils/apply-diff.ts:37–372  ·  view source on GitHub ↗
(
	currentSnapshot: Snapshot,
	snapshotDiff: SnapshotDiff,
	options?: { database?: Knex; schema?: SchemaOverview },
)

Source from the content-addressed store, hash-verified

35const logger = useLogger();
36
37export async function applyDiff(
38 currentSnapshot: Snapshot,
39 snapshotDiff: SnapshotDiff,
40 options?: { database?: Knex; schema?: SchemaOverview },
41): Promise<void> {
42 const database = options?.database ?? getDatabase();
43 const helpers = getHelpers(database);
44 const schema = options?.schema ?? (await getSchema({ database, bypassCache: true }));
45
46 const nestedActionEvents: ActionEventParams[] = [];
47
48 const mutationOptions: MutationOptions = {
49 autoPurgeSystemCache: false,
50 bypassEmitAction: (params) => nestedActionEvents.push(params),
51 bypassLimits: true,
52 };
53
54 const runPostColumnChange = await helpers.schema.preColumnChange();
55
56 await transaction(database, async (trx) => {
57 const collectionsService = new CollectionsService({ knex: trx, schema });
58
59 const getNestedCollectionsToCreate = (currentLevelCollection: string) =>
60 snapshotDiff.collections.filter(
61 ({ diff }) => (diff[0] as DiffNew<Collection>).rhs?.meta?.group === currentLevelCollection,
62 ) as CollectionDelta[];
63
64 const createCollections = async (collections: CollectionDelta[]) => {
65 for (const { collection, diff } of collections) {
66 if (diff?.[0]?.kind === DiffKind.NEW && diff[0].rhs) {
67 // We'll nest the to-be-created fields in the same collection creation, to prevent
68 // creating a collection without a primary key
69 const fields = snapshotDiff.fields
70 .filter((fieldDiff) => fieldDiff.collection === collection)
71 .map((fieldDiff) => (fieldDiff.diff[0] as DiffNew<Field>).rhs)
72 .map((fieldDiff) => {
73 // Casts field type to UUID when applying non-PostgreSQL schema onto PostgreSQL database.
74 // This is needed because they snapshots UUID fields as char/varchar with length 36.
75 if (
76 ['char', 'varchar'].includes(String(fieldDiff.schema?.data_type).toLowerCase()) &&
77 fieldDiff.schema?.max_length === 36 &&
78 (fieldDiff.schema?.is_primary_key ||
79 (fieldDiff.schema?.foreign_key_table && fieldDiff.schema?.foreign_key_column))
80 ) {
81 return merge(fieldDiff, { type: 'uuid', schema: { data_type: 'uuid', max_length: null } });
82 } else {
83 return fieldDiff;
84 }
85 });
86
87 try {
88 await collectionsService.createOne(
89 {
90 ...diff[0].rhs,
91 fields,
92 },
93 mutationOptions,
94 );

Callers 3

apply-diff.test.tsFile · 0.85
applySnapshotFunction · 0.85
applyMethod · 0.85

Calls 15

updateOneMethod · 0.95
createFieldMethod · 0.95
updateFieldMethod · 0.95
deleteFieldMethod · 0.95
createOneMethod · 0.95
updateOneMethod · 0.95
deleteOneMethod · 0.95
getDatabaseFunction · 0.85
getSchemaFunction · 0.85
transactionFunction · 0.85
createCollectionsFunction · 0.85
deleteCollectionsFunction · 0.85

Tested by

no test coverage detected