-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add SCHEMA_SYNC_SAFE flag to prevent destructive schema imports #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,41 @@ | ||
| import type { Snapshot, SnapshotField, SnapshotRelation } from '@directus/api/dist/types'; | ||
| import type { ApiExtensionContext } from '@directus/extensions'; | ||
| import type { Collection, ExtensionsServices } from '@directus/types'; | ||
| import type { Collection, ExtensionsServices, SnapshotDiff } from '@directus/types'; | ||
| import { mkdir, readFile, rm, writeFile } from 'fs/promises'; | ||
| import { glob } from 'glob'; | ||
| import { condenseAction } from './condenseAction.js'; | ||
| import { exportHook } from './schemaExporterHooks.js'; | ||
| import type { IExporter } from './types'; | ||
| import { ExportHelper } from './utils.js'; | ||
|
|
||
| /** | ||
| * Removes all destructive (DELETE) operations from a schema diff. | ||
| * Used with SCHEMA_SYNC_SAFE=true to prevent project-specific collections, | ||
| * fields, and relations from being dropped when importing a base snapshot. | ||
| */ | ||
| function filterNonDestructive(diff: SnapshotDiff): SnapshotDiff { | ||
| const isTopLevelDelete = (diffs: ReadonlyArray<{ kind: string; path?: unknown }>) => | ||
| diffs.some(d => d.kind === 'D' && !d.path); | ||
|
|
||
| const deletedCollections = new Set( | ||
| diff.collections.filter(c => isTopLevelDelete(c.diff)).map(c => c.collection) | ||
| ); | ||
|
|
||
| return { | ||
| ...diff, | ||
| collections: diff.collections.filter(c => !deletedCollections.has(c.collection)), | ||
| fields: diff.fields.filter( | ||
| f => !deletedCollections.has(f.collection) && !isTopLevelDelete(f.diff) | ||
| ), | ||
| systemFields: (diff.systemFields ?? []).filter( | ||
| f => !deletedCollections.has(f.collection) && !isTopLevelDelete(f.diff) | ||
| ), | ||
| relations: diff.relations.filter( | ||
| r => !deletedCollections.has(r.collection) && !isTopLevelDelete(r.diff) | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| export class SchemaExporter implements IExporter { | ||
| protected _filePath: string; | ||
| protected _exportHandler = condenseAction(() => this.createAndSaveSnapshot()); | ||
|
|
@@ -16,7 +44,7 @@ export class SchemaExporter implements IExporter { | |
| constructor( | ||
| protected getSchemaService: () => Promise<InstanceType<ExtensionsServices['SchemaService']>>, | ||
| protected logger: ApiExtensionContext['logger'], | ||
| protected options = { split: true } | ||
| protected options = { split: true, safe: false } | ||
| ) { | ||
|
Comment on lines
44
to
48
|
||
| this._filePath = `${ExportHelper.dataDir}/schema.json`; | ||
| } | ||
|
|
@@ -113,8 +141,12 @@ export class SchemaExporter implements IExporter { | |
| } | ||
|
|
||
| this.logger.info(`Diffing schema with hash: ${currentHash} and hash: ${hash}`); | ||
| const diff = await svc.diff(snapshot, { currentSnapshot, force: true }); | ||
| let diff = await svc.diff(snapshot, { currentSnapshot, force: true }); | ||
| if (diff !== null) { | ||
| if (this.options.safe) { | ||
| diff = filterNonDestructive(diff); | ||
| this.logger.info('SCHEMA_SYNC_SAFE: filtered destructive operations from diff'); | ||
| } | ||
| this.logger.info(`Applying schema diff...`); | ||
| await svc.apply({ diff, hash: currentHash }); | ||
| this.logger.info(`Schema updated`); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schemaOptionsnow includessafe, but the CLIexport-schemacommand later passes the rawargsobject (typed as{ split: boolean }) intonew SchemaExporter(...). After this change, thatargsobject is missing thesafeproperty and will not satisfy the constructor’s inferred option type understrictTS. Update the CLI path to merge defaults (e.g. pass{ split: args.split, safe: schemaOptions.safe }) or change the constructor to accept partial options.