From a2bb259ea8a16ca7ddccf09f4518130cb1dbf960 Mon Sep 17 00:00:00 2001 From: j33ty <31261252+j33ty@users.noreply.github.com> Date: Wed, 7 Feb 2024 14:50:16 +0100 Subject: [PATCH] feat(CLI): in sanity dataset import cmd, add --allow-system-documents to explicitly permit system document import, which should be ignore otherwise --- packages/@sanity/import/README.md | 10 ++++++++++ packages/@sanity/import/src/importFromArray.js | 5 +++++ packages/@sanity/import/src/validateOptions.js | 1 + .../cli/commands/dataset/importDatasetCommand.ts | 7 +++++++ 4 files changed, 23 insertions(+) diff --git a/packages/@sanity/import/README.md b/packages/@sanity/import/README.md index 0d92a12ba2af..8d39cddef7ed 100644 --- a/packages/@sanity/import/README.md +++ b/packages/@sanity/import/README.md @@ -85,6 +85,16 @@ const options = { * Optional, defaults to `false`. */ skipCrossDatasetReferences: false, + + /** + * Whether or not to import system documents (like permissions and custom retention). This + * is usually not necessary, and may cause conflicts if the target dataset + * already contains these documents. On a new dataset, it is recommended that roles are re-created + * manually, and that any custom retention policies are re-created manually. + * + * Optional, defaults to `false`. + */ + allowSystemDocuments: false } sanityImport(input, options) diff --git a/packages/@sanity/import/src/importFromArray.js b/packages/@sanity/import/src/importFromArray.js index e98b75dce010..d0e03780f147 100644 --- a/packages/@sanity/import/src/importFromArray.js +++ b/packages/@sanity/import/src/importFromArray.js @@ -30,6 +30,11 @@ async function importDocuments(documents, options) { validateCdrDatasets(documents, options) } + // Always filter out system documents unless explicitly allowed. + if (options.allowSystemDocuments !== true) { + documents.filter((doc) => !doc._id.startsWith('_.')) + } + // Replace relative asset paths if one is defined // (file://./images/foo-bar.png -> file:///abs/olute/images/foo-bar.png) const absPathed = documents.map((doc) => absolutifyPaths(doc, options.assetsBase)) diff --git a/packages/@sanity/import/src/validateOptions.js b/packages/@sanity/import/src/validateOptions.js index fe0ad13861b3..db036cdfa6f2 100644 --- a/packages/@sanity/import/src/validateOptions.js +++ b/packages/@sanity/import/src/validateOptions.js @@ -14,6 +14,7 @@ function validateOptions(input, opts) { allowAssetsInDifferentDataset: false, replaceAssets: false, skipCrossDatasetReferences: false, + allowSystemDocuments: false, }) if (!isValidInput(input)) { diff --git a/packages/sanity/src/_internal/cli/commands/dataset/importDatasetCommand.ts b/packages/sanity/src/_internal/cli/commands/dataset/importDatasetCommand.ts index 6fbb31b0af1c..c5cdd8558ddb 100644 --- a/packages/sanity/src/_internal/cli/commands/dataset/importDatasetCommand.ts +++ b/packages/sanity/src/_internal/cli/commands/dataset/importDatasetCommand.ts @@ -23,6 +23,7 @@ Options Rarely used options (should generally not be used) --allow-assets-in-different-dataset Allow asset documents to reference different project/dataset + --allow-system-documents Allow system documents like dataset permissions and custom retention to be imported Examples # Import "moviedb.ndjson" from the current directory to the dataset called "moviedb" @@ -47,6 +48,7 @@ interface ImportFlags { 'asset-concurrency'?: boolean 'replace-assets'?: boolean 'skip-cross-dataset-references'?: boolean + 'allow-system-documents'?: boolean replace?: boolean missing?: boolean } @@ -56,6 +58,7 @@ interface ParsedImportFlags { allowFailingAssets?: boolean assetConcurrency?: boolean skipCrossDatasetReferences?: boolean + allowSystemDocuments?: boolean replaceAssets?: boolean replace?: boolean missing?: boolean @@ -82,6 +85,7 @@ function parseFlags(rawFlags: ImportFlags): ParsedImportFlags { const assetConcurrency = toBoolIfSet(rawFlags['asset-concurrency']) const replaceAssets = toBoolIfSet(rawFlags['replace-assets']) const skipCrossDatasetReferences = toBoolIfSet(rawFlags['skip-cross-dataset-references']) + const allowSystemDocuments = toBoolIfSet(rawFlags['allow-system-documents']) const replace = toBoolIfSet(rawFlags.replace) const missing = toBoolIfSet(rawFlags.missing) return { @@ -89,6 +93,7 @@ function parseFlags(rawFlags: ImportFlags): ParsedImportFlags { allowFailingAssets, assetConcurrency, skipCrossDatasetReferences, + allowSystemDocuments, replaceAssets, replace, missing, @@ -110,6 +115,7 @@ const importDatasetCommand: CliCommandDefinition = { allowFailingAssets, assetConcurrency, skipCrossDatasetReferences, + allowSystemDocuments, replaceAssets, } = flags @@ -248,6 +254,7 @@ const importDatasetCommand: CliCommandDefinition = { allowFailingAssets, allowAssetsInDifferentDataset, skipCrossDatasetReferences, + allowSystemDocuments, assetConcurrency, replaceAssets, })