Skip to content

Commit

Permalink
feat(CLI): in sanity dataset import cmd, add --allow-system-documents…
Browse files Browse the repository at this point in the history
… to explicitly permit system document import, which should be ignore otherwise
  • Loading branch information
j33ty committed Feb 7, 2024
1 parent b7101d5 commit a2bb259
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/@sanity/import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions packages/@sanity/import/src/importFromArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/import/src/validateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function validateOptions(input, opts) {
allowAssetsInDifferentDataset: false,
replaceAssets: false,
skipCrossDatasetReferences: false,
allowSystemDocuments: false,
})

if (!isValidInput(input)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -56,6 +58,7 @@ interface ParsedImportFlags {
allowFailingAssets?: boolean
assetConcurrency?: boolean
skipCrossDatasetReferences?: boolean
allowSystemDocuments?: boolean
replaceAssets?: boolean
replace?: boolean
missing?: boolean
Expand All @@ -82,13 +85,15 @@ 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 {
allowAssetsInDifferentDataset,
allowFailingAssets,
assetConcurrency,
skipCrossDatasetReferences,
allowSystemDocuments,
replaceAssets,
replace,
missing,
Expand All @@ -110,6 +115,7 @@ const importDatasetCommand: CliCommandDefinition = {
allowFailingAssets,
assetConcurrency,
skipCrossDatasetReferences,
allowSystemDocuments,
replaceAssets,
} = flags

Expand Down Expand Up @@ -248,6 +254,7 @@ const importDatasetCommand: CliCommandDefinition = {
allowFailingAssets,
allowAssetsInDifferentDataset,
skipCrossDatasetReferences,
allowSystemDocuments,
assetConcurrency,
replaceAssets,
})
Expand Down

0 comments on commit a2bb259

Please sign in to comment.