Fix schema apply for changes in field meta options & display_options (#12712)

* Fix schema apply for changes in field meta options

* take display_options into account

* check changes in meta in general
This commit is contained in:
Azri Kahar
2022-04-16 01:27:47 +08:00
committed by GitHub
parent 763a993bca
commit 953eb99eb3
2 changed files with 19 additions and 8 deletions

View File

@@ -1,4 +1,4 @@
import { Snapshot, SnapshotDiff } from '../types';
import { Snapshot, SnapshotDiff, SnapshotField } from '../types';
import { getSnapshot } from './get-snapshot';
import { getSnapshotDiff } from './get-snapshot-diff';
import { Knex } from 'knex';
@@ -6,7 +6,7 @@ import getDatabase from '../database';
import { getSchema } from './get-schema';
import { CollectionsService, FieldsService, RelationsService } from '../services';
import { set, merge } from 'lodash';
import { DiffNew } from 'deep-diff';
import { Diff, DiffNew } from 'deep-diff';
import { Field, Relation, SchemaOverview } from '@directus/shared/types';
import logger from '../logger';
@@ -96,7 +96,7 @@ export async function applySnapshot(
const fieldsService = new FieldsService({ knex: trx, schema: await getSchema({ database: trx }) });
for (const { collection, field, diff } of snapshotDiff.fields) {
if (diff?.[0].kind === 'N') {
if (diff?.[0].kind === 'N' && !isNestedMetaUpdate(diff?.[0])) {
try {
await fieldsService.createField(collection, (diff[0] as DiffNew<Field>).rhs);
} catch (err) {
@@ -105,7 +105,7 @@ export async function applySnapshot(
}
}
if (diff?.[0].kind === 'E' || diff?.[0].kind === 'A') {
if (diff?.[0].kind === 'E' || diff?.[0].kind === 'A' || isNestedMetaUpdate(diff?.[0])) {
const newValues = snapshot.fields.find((snapshotField) => {
return snapshotField.collection === collection && snapshotField.field === field;
});
@@ -122,7 +122,7 @@ export async function applySnapshot(
}
}
if (diff?.[0].kind === 'D') {
if (diff?.[0].kind === 'D' && !isNestedMetaUpdate(diff?.[0])) {
try {
await fieldsService.deleteField(collection, field);
} catch (err) {
@@ -182,3 +182,10 @@ export async function applySnapshot(
}
});
}
export function isNestedMetaUpdate(diff: Diff<SnapshotField | undefined>): boolean {
if (!diff) return false;
if (diff.kind !== 'N' && diff.kind !== 'D') return false;
if (!diff.path || diff.path.length < 2 || diff.path[0] !== 'meta') return false;
return true;
}