Merge pull request #111 from boazpoolman/feature/fix-import-on-bootstrap

Feature/fix import on bootstrap
pull/114/head
Mathijs Schouten 2023-10-21 12:17:50 +02:00 committed by GitHub
commit a4b767f412
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1238 additions and 944 deletions

View File

@ -17,7 +17,7 @@
"eslint": "eslint --max-warnings=0 './**/*.{js,jsx}'",
"eslint:fix": "eslint --fix './**/*.{js,jsx}'",
"test:unit": "jest --verbose",
"test:integration": "cd playground && node_modules/.bin/jest --verbose",
"test:integration": "cd playground && node_modules/.bin/jest --verbose --forceExit --detectOpenHandles",
"plugin:install": "yarn install && rm -rf node_modules/@strapi/helper-plugin",
"playground:install": "cd playground && yarn install",
"playground:build": "cd playground && yarn build",
@ -73,7 +73,7 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^2.3.0",
"jest": "^29.3.1",
"jest": "^29.7.0",
"jest-cli": "^29.3.1",
"jest-styled-components": "^7.0.2",
"lodash": "^4.17.11",

View File

@ -6,7 +6,6 @@ const exec = util.promisify(require('child_process').exec);
jest.setTimeout(20000);
describe('Test the config-sync CLI', () => {
afterAll(async () => {
// Remove the generated files and the DB.
await exec('rm -rf config/sync');
@ -33,8 +32,8 @@ describe('Test the config-sync CLI', () => {
let error;
try {
await exec('yarn cs diff');
} catch(e) {
error = e;
} catch (e) {
error = e;
}
expect(error).toHaveProperty('code', 1);
});

View File

@ -0,0 +1,34 @@
const fs = require('fs');
const Strapi = require('@strapi/strapi');
let instance;
async function setupStrapi() {
if (!instance) {
await Strapi().load();
instance = strapi;
await instance.server.mount();
}
return instance;
}
async function cleanupStrapi() {
const dbSettings = strapi.config.get('database.connection');
// close server to release the db-file.
await strapi.server.httpServer.close();
// close the connection to the database before deletion.
await strapi.db.connection.destroy();
// delete test database after all tests have completed.
if (dbSettings && dbSettings.connection && dbSettings.connection.filename) {
const tmpDbFile = dbSettings.connection.filename;
if (fs.existsSync(tmpDbFile)) {
fs.unlinkSync(tmpDbFile);
}
}
}
module.exports = { setupStrapi, cleanupStrapi };

View File

@ -0,0 +1,48 @@
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { setupStrapi, cleanupStrapi } = require('./helpers');
jest.setTimeout(20000);
afterEach(async () => {
// Disable importOnBootstrap
await exec('sed -i "s/importOnBootstrap: true/importOnBootstrap: false/g" config/plugins.js');
await cleanupStrapi();
await exec('rm -rf config/sync');
});
describe('Test the importOnBootstrap feature', () => {
test('Without a database', async () => {
// Do the initial export and remove the database.
await exec('yarn cs export -y');
await exec('rm -rf .tmp');
// Manually change the plugins.js to enable importOnBoostrap.
await exec('sed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js');
// Start up Strapi to initiate the importOnBootstrap function.
await setupStrapi();
expect(strapi).toBeDefined();
});
test('With a database', async () => {
// Delete any existing database and do an export.
await exec('rm -rf .tmp');
await exec('yarn cs export -y');
// Manually change the plugins.js to enable importOnBoostrap.
await exec('sed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js');
// Remove a config file to make sure the importOnBoostrap
// function actually attempts to import.
await exec('rm -rf config/sync/admin-role.strapi-editor.json');
// Start up Strapi to initiate the importOnBootstrap function.
await setupStrapi();
expect(strapi).toBeDefined();
});
});

View File

@ -0,0 +1,8 @@
module.exports = {
'config-sync': {
enabled: true,
config: {
importOnBootstrap: false,
},
},
};

View File

@ -11,8 +11,9 @@
"cs": "config-sync"
},
"devDependencies": {
"jest": "^26.0.1",
"jest-cli": "^26.0.1"
"jest": "^29.7.0",
"jest-cli": "^29.7.0",
"supertest": "^6.3.3"
},
"dependencies": {
"@strapi/plugin-i18n": "^4.14.4",

View File

@ -1,6 +1,7 @@
const { isEmpty } = require('lodash');
const { logMessage, sanitizeConfig, dynamicSort, noLimit, getCombinedUid, getCombinedUidWhereFilter, getUidParamsFromName } = require('../utils');
const { difference, same } = require('../utils/getArrayDiff');
const queryFallBack = require('../utils/queryFallBack');
const ConfigType = class ConfigType {
constructor({ queryString, configName, uid, jsonFields, relations, components }) {
@ -69,11 +70,11 @@ const ConfigType = class ConfigType {
});
await Promise.all(relations.map(async (relation) => {
await strapi.entityService.delete(queryString, relation.id);
await queryFallBack.delete(queryString, relation.id);
}));
}));
await strapi.entityService.delete(this.queryString, existingConfig.id);
await queryFallBack.delete(this.queryString, existingConfig.id);
return;
}
@ -86,7 +87,7 @@ const ConfigType = class ConfigType {
// Create entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const newEntity = await strapi.entityService.create(this.queryString, {
const newEntity = await queryFallBack.create(this.queryString, {
data: query,
});
@ -94,7 +95,7 @@ const ConfigType = class ConfigType {
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => {
await Promise.all(configContent[relationName].map(async (relationEntity) => {
const relationQuery = { ...relationEntity, [parentName]: newEntity };
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: relationQuery,
});
}));
@ -110,16 +111,7 @@ const ConfigType = class ConfigType {
// Update entity.
this.relations.map(({ relationName }) => delete query[relationName]);
const entity = await queryAPI.findOne({ where: combinedUidWhereFilter });
try {
await strapi.entityService.update(this.queryString, entity.id, {
data: query,
});
} catch (error) {
console.warn(logMessage(`Use Query Engine API instead of Entity Service API for type ${this.configPrefix}`));
await queryAPI.update({ where: combinedUidWhereFilter, data: query });
}
const entity = queryFallBack.update(this.queryString, { where: combinedUidWhereFilter, data: query });
// Delete/create relations.
await Promise.all(this.relations.map(async ({ queryString, relationName, parentName, relationSortFields }) => {
@ -145,7 +137,7 @@ const ConfigType = class ConfigType {
}));
await Promise.all(configToAdd.map(async (config) => {
await strapi.entityService.create(queryString, {
await queryFallBack.create(queryString, {
data: { ...config, [parentName]: entity.id },
});
}));

View File

@ -0,0 +1,32 @@
const queryFallBack = {
create: async (queryString, options) => {
try {
const newEntity = await strapi.entityService.create(queryString, options);
return newEntity;
} catch (e) {
return strapi.query(queryString).create(options);
}
},
update: async (queryString, options) => {
try {
const entity = await strapi.query(queryString).findOne(options.where);
const updatedEntity = await strapi.entityService.update(queryString, entity.id);
return updatedEntity;
} catch (e) {
return strapi.query(queryString).update(options);
}
},
delete: async (queryString, id) => {
try {
await strapi.entityService.delete(queryString, id);
} catch (e) {
await strapi.query(queryString).delete({
where: { id },
});
}
},
};
module.exports = queryFallBack;

2024
yarn.lock

File diff suppressed because it is too large Load Diff