Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/docs/getting-started/config-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 45 additions & 0 deletions playground/__tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 3 additions & 1 deletion server/config/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -28,6 +28,7 @@ const ConfigType = class ConfigType {
this.jsonFields = jsonFields || [];
this.relations = relations || [];
this.components = components || null;
this.ignoreDocumentId = ignoreDocumentId || false;
}

/**
Expand Down Expand Up @@ -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 },
Expand Down
6 changes: 6 additions & 0 deletions server/config/types.js
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -13,6 +16,7 @@ const types = (strapi) => {
configName: 'admin-role',
queryString: 'admin::role',
uid: 'code',
ignoreDocumentId: true,
relations: [{
queryString: 'admin::permission',
relationName: 'permissions',
Expand All @@ -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',
Expand All @@ -43,6 +48,7 @@ const types = (strapi) => {
configName: 'i18n-locale',
queryString: 'plugin::i18n.locale',
uid: 'code',
ignoreDocumentId: true,
});
}

Expand Down
12 changes: 11 additions & 1 deletion server/services/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;
}));

Expand Down