From cff0e5eae5ac277bce2e1cdbfd543b879e50c4e1 Mon Sep 17 00:00:00 2001 From: keesvanbemmel Date: Fri, 23 Aug 2024 15:12:04 +0200 Subject: [PATCH] Adds ignore rules to Schema Apply CLI --- api/src/cli/commands/schema/apply.ts | 28 +++++++++++++++++++++++++--- api/src/cli/index.ts | 1 + 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/api/src/cli/commands/schema/apply.ts b/api/src/cli/commands/schema/apply.ts index 75ab7abd10f3b..4fe6774cfd2da 100644 --- a/api/src/cli/commands/schema/apply.ts +++ b/api/src/cli/commands/schema/apply.ts @@ -6,14 +6,32 @@ import { load as loadYaml } from 'js-yaml'; import path from 'path'; import getDatabase, { isInstalled, validateDatabaseConnection } from '../../../database/index.js'; import { useLogger } from '../../../logger/index.js'; -import type { Snapshot } from '../../../types/index.js'; +import type { Snapshot, SnapshotDiff } from '../../../types/index.js'; import { DiffKind } from '../../../types/index.js'; import { isNestedMetaUpdate } from '../../../utils/apply-diff.js'; import { applySnapshot } from '../../../utils/apply-snapshot.js'; import { getSnapshotDiff } from '../../../utils/get-snapshot-diff.js'; import { getSnapshot } from '../../../utils/get-snapshot.js'; -export async function apply(snapshotPath: string, options?: { yes: boolean; dryRun: boolean }): Promise { +function filterSnapshotDiff(snapshot: SnapshotDiff, filters: string[]): SnapshotDiff { + const filterSet = new Set(filters); + + function shouldKeep(item: { collection: string; field?: string }): boolean { + if (filterSet.has(item.collection)) return false; + if (item.field && filterSet.has(`${item.collection}.${item.field}`)) return false; + return true; + } + + const filteredDiff: SnapshotDiff = { + collections: snapshot.collections.filter((item) => shouldKeep(item)), + fields: snapshot.fields.filter((item) => shouldKeep(item)), + relations: snapshot.relations.filter((item) => shouldKeep(item)), + }; + + return filteredDiff; +} + +export async function apply(snapshotPath: string, options?: { yes: boolean; dryRun: boolean, ignoreRules: string }): Promise { const logger = useLogger(); const filename = path.resolve(process.cwd(), snapshotPath); @@ -40,7 +58,11 @@ export async function apply(snapshotPath: string, options?: { yes: boolean; dryR } const currentSnapshot = await getSnapshot({ database }); - const snapshotDiff = getSnapshotDiff(currentSnapshot, snapshot); + let snapshotDiff = getSnapshotDiff(currentSnapshot, snapshot); + + if (ignoreRules) { + snapshotDiff = filterSnapshotDiff(snapshotDiff, ignoreRules.split(',')); + } if ( snapshotDiff.collections.length === 0 && diff --git a/api/src/cli/index.ts b/api/src/cli/index.ts index 6e0fcbc1ee1f5..a9829b66bc850 100644 --- a/api/src/cli/index.ts +++ b/api/src/cli/index.ts @@ -101,6 +101,7 @@ export async function createCli(): Promise { .description('Apply a snapshot file to the current database') .option('-y, --yes', `Assume "yes" as answer to all prompts and run non-interactively`) .option('-d, --dry-run', 'Plan and log changes to be applied', false) + .option('--ignoreRules ', `Comma separated list of collections and or fields to ignore. Format: "products.title,reviews" this will ignore applying changes to the title field in the products collection and the entire reviews collection`) .argument('', 'Path to snapshot file') .action(apply);