Skip to content

Commit

Permalink
Adds ignore rules to Schema Apply CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
keesvanbemmel committed Aug 23, 2024
1 parent 103a4c9 commit cff0e5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions api/src/cli/commands/schema/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<void> {
const logger = useLogger();

const filename = path.resolve(process.cwd(), snapshotPath);
Expand All @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions api/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export async function createCli(): Promise<Command> {
.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 <value>', `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>', 'Path to snapshot file')
.action(apply);

Expand Down

0 comments on commit cff0e5e

Please sign in to comment.