From 3211bbea52351da8f71bfd9f84456486afc72141 Mon Sep 17 00:00:00 2001 From: Marcel Pfeifer Date: Mon, 23 Nov 2026 10:14:29 +0100 Subject: [PATCH] fix: don't sync the documentId of the default roles and locale Strapi creates the default admin roles, the users-permissions roles and the default i18n locale itself at bootstrap, each with a random documentId that an import can't change (documents().update keeps the documentId). So on any database other than the one the files were exported from, these configs showed as Different after every import, and import on bootstrap imported them on every start. Config types get an ignoreDocumentId option that leaves the documentId out of the export and the comparison; it's set for admin-role, user-role and i18n-locale, which are matched by their uid. The relations in a config file are now sorted by their relationSortFields like the database side, so a file that lists its permissions in another order (edited by hand, or merged) isn't Different either. --- docs/docs/getting-started/config-types.md | 8 ++++ playground/__tests__/cli.test.js | 45 +++++++++++++++++++++++ server/config/type.js | 4 +- server/config/types.js | 6 +++ server/services/main.js | 12 +++++- 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/docs/docs/getting-started/config-types.md b/docs/docs/getting-started/config-types.md index 2f4480f..2d7a7f6 100644 --- a/docs/docs/getting-started/config-types.md +++ b/docs/docs/getting-started/config-types.md @@ -123,3 +123,11 @@ This property can accept an array of field names from the type. It is meant to s ##### Key: `jsonFields` > `required:` NO | `type:` array + +### Ignore document ID + +Leaves the `documentId` out of the sync for this type: it is not exported and not compared. Use it for entries that each environment creates by itself, with its own random `documentId`. They are matched by their UID only. The plugin does this for the admin roles, user roles and i18n locales, which Strapi creates at bootstrap. + +##### Key: `ignoreDocumentId` + +> `required:` NO | `type:` boolean diff --git a/playground/__tests__/cli.test.js b/playground/__tests__/cli.test.js index 965bc31..0c81304 100644 --- a/playground/__tests__/cli.test.js +++ b/playground/__tests__/cli.test.js @@ -32,6 +32,51 @@ describe('Test the config-sync CLI', () => { expect(diffOutput).toContain('No differences between DB and sync directory'); }); + test('Roles and locales Strapi creates itself match their files on a new database', async () => { + // A new database gets new random documentIds for the default roles and locale. + await exec('rm -rf .tmp'); + const { stdout: importOutput } = await exec('yarn cs import -y'); + expect(importOutput).toContain('Finished import'); + const { stdout: diffOutput } = await exec('yarn cs diff'); + expect(diffOutput).toContain('No differences between DB and sync directory'); + ['admin-role.strapi-editor', 'user-role.public', 'i18n-locale.en'].forEach((name) => { + expect(JSON.parse(fs.readFileSync(`config/sync/${name}.json`, 'utf8'))).not.toHaveProperty('documentId'); + }); + }); + + test('A documentId left in a role or locale file from an older export is not a difference', async () => { + const names = ['admin-role.strapi-editor', 'user-role.public', 'i18n-locale.en']; + const originals = names.map((name) => fs.readFileSync(`config/sync/${name}.json`, 'utf8')); + names.forEach((name, i) => { + fs.writeFileSync(`config/sync/${name}.json`, JSON.stringify({ ...JSON.parse(originals[i]), documentId: 'olddocumentid' })); + }); + + try { + const { stdout: diffOutput } = await exec('yarn cs diff'); + expect(diffOutput).toContain('No differences between DB and sync directory'); + } finally { + names.forEach((name, i) => fs.writeFileSync(`config/sync/${name}.json`, originals[i])); + } + }); + + test.each([ + 'admin-role.strapi-editor', + 'user-role.public', + ])('Permissions listed in another order in %s are not a difference', async (name) => { + const file = `config/sync/${name}.json`; + const original = fs.readFileSync(file, 'utf8'); + const config = JSON.parse(original); + config.permissions.reverse(); + fs.writeFileSync(file, JSON.stringify(config)); + + try { + const { stdout: diffOutput } = await exec('yarn cs diff'); + expect(diffOutput).toContain('No differences between DB and sync directory'); + } finally { + fs.writeFileSync(file, original); + } + }); + test('Partial export only exports the selected config', async () => { const selectedConfigBefore = fs.readFileSync(selectedConfigPath, 'utf8'); const otherConfigBefore = fs.readFileSync(otherConfigPath, 'utf8'); diff --git a/server/config/type.js b/server/config/type.js index d4fad8a..f6d2dfe 100644 --- a/server/config/type.js +++ b/server/config/type.js @@ -5,7 +5,7 @@ import queryFallBack from '../utils/queryFallBack'; import InvalidConfigFileError from '../utils/invalidConfigFileError'; const ConfigType = class ConfigType { - constructor({ queryString, configName, uid, jsonFields, relations, components }) { + constructor({ queryString, configName, uid, jsonFields, relations, components, ignoreDocumentId }) { if (!configName) { strapi.log.error(logMessage('A config type was registered without a config name.')); process.exit(0); @@ -28,6 +28,7 @@ const ConfigType = class ConfigType { this.jsonFields = jsonFields || []; this.relations = relations || []; this.components = components || null; + this.ignoreDocumentId = ignoreDocumentId || false; } /** @@ -238,6 +239,7 @@ const ConfigType = class ConfigType { if (shouldExclude) return; const formattedConfig = { ...sanitizeConfig({ config, configName }) }; + if (this.ignoreDocumentId) delete formattedConfig.documentId; await Promise.all(this.relations.map(async ({ queryString, relationName, relationSortFields, parentName }) => { const relations = await noLimit(strapi.query(queryString), { where: { [parentName]: combinedUidWhereFilter }, diff --git a/server/config/types.js b/server/config/types.js index 1f5edfa..8cd5822 100644 --- a/server/config/types.js +++ b/server/config/types.js @@ -1,5 +1,8 @@ 'use strict'; +// Strapi creates the default roles and locale itself at bootstrap, each with a +// random documentId that an update can't change. These types are matched by +// their uid, so their documentId is left out of the sync (ignoreDocumentId). const types = (strapi) => { // Initiate Strapi 'core-store' and 'admin-role' types. const typesArray = [ @@ -13,6 +16,7 @@ const types = (strapi) => { configName: 'admin-role', queryString: 'admin::role', uid: 'code', + ignoreDocumentId: true, relations: [{ queryString: 'admin::permission', relationName: 'permissions', @@ -28,6 +32,7 @@ const types = (strapi) => { configName: 'user-role', queryString: 'plugin::users-permissions.role', uid: 'type', + ignoreDocumentId: true, relations: [{ queryString: 'plugin::users-permissions.permission', relationName: 'permissions', @@ -43,6 +48,7 @@ const types = (strapi) => { configName: 'i18n-locale', queryString: 'plugin::i18n.locale', uid: 'code', + ignoreDocumentId: true, }); } diff --git a/server/services/main.js b/server/services/main.js index 97c0163..18ad215 100644 --- a/server/services/main.js +++ b/server/services/main.js @@ -7,7 +7,7 @@ import AdmZip from 'adm-zip'; import { parse as parseJsonWithErrors } from 'jsonc-parser'; import difference from '../utils/getObjectDiff'; -import { logMessage } from '../utils'; +import { logMessage, dynamicSort } from '../utils'; import InvalidConfigFileError from '../utils/invalidConfigFileError'; /** @@ -178,6 +178,16 @@ export default () => ({ return; } + // Formatted like the database config, so only real changes show up. + const { ignoreDocumentId, relations } = strapi.plugin('config-sync').types[type]; + if (ignoreDocumentId) delete fileContents.documentId; + relations.forEach(({ relationName, relationSortFields }) => { + if (!Array.isArray(fileContents[relationName])) return; + relationSortFields.forEach((sortField) => { + fileContents[relationName].sort(dynamicSort(sortField)); + }); + }); + fileConfigs[`${type}.${formattedName}`] = fileContents; }));