mirror of
https://github.com/strapi/strapi.git
synced 2025-09-14 19:19:43 +00:00
Merge pull request #16433 from strapi/review-workflow/do-not-persist-when-CT-is-deleted
This commit is contained in:
commit
a38fcb7af4
3
packages/core/admin/ee/server/bootstrap.js
vendored
3
packages/core/admin/ee/server/bootstrap.js
vendored
@ -5,7 +5,7 @@ const { features } = require('@strapi/strapi/lib/utils/ee');
|
|||||||
const executeCEBootstrap = require('../../server/bootstrap');
|
const executeCEBootstrap = require('../../server/bootstrap');
|
||||||
const { getService } = require('../../server/utils');
|
const { getService } = require('../../server/utils');
|
||||||
const actions = require('./config/admin-actions');
|
const actions = require('./config/admin-actions');
|
||||||
const { persistTablesWithPrefix, persistTablesWithSuffix } = require('./utils/persisted-tables');
|
const { persistTablesWithPrefix } = require('./utils/persisted-tables');
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async () => {
|
||||||
const { actionProvider } = getService('permission');
|
const { actionProvider } = getService('permission');
|
||||||
@ -22,7 +22,6 @@ module.exports = async () => {
|
|||||||
|
|
||||||
if (features.isEnabled('review-workflows')) {
|
if (features.isEnabled('review-workflows')) {
|
||||||
await persistTablesWithPrefix('strapi_workflows');
|
await persistTablesWithPrefix('strapi_workflows');
|
||||||
await persistTablesWithSuffix('_strapi_review_workflows_stage_links');
|
|
||||||
|
|
||||||
const { bootstrap: rwBootstrap } = getService('review-workflows');
|
const { bootstrap: rwBootstrap } = getService('review-workflows');
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ const defaultWorkflow = require('../../constants/default-workflow.json');
|
|||||||
const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows');
|
const { ENTITY_STAGE_ATTRIBUTE } = require('../../constants/workflows');
|
||||||
|
|
||||||
const { getDefaultWorkflow } = require('../../utils/review-workflows');
|
const { getDefaultWorkflow } = require('../../utils/review-workflows');
|
||||||
|
const { persistTable, removePersistedTablesWithSuffix } = require('../../utils/persisted-tables');
|
||||||
|
|
||||||
async function initDefaultWorkflow({ workflowsService, stagesService, strapi }) {
|
async function initDefaultWorkflow({ workflowsService, stagesService, strapi }) {
|
||||||
const wfCount = await workflowsService.count();
|
const wfCount = await workflowsService.count();
|
||||||
@ -73,12 +74,20 @@ function enableReviewWorkflow({ strapi }) {
|
|||||||
const stagesService = getService('stages', { strapi });
|
const stagesService = getService('stages', { strapi });
|
||||||
|
|
||||||
const up = async (contentTypeUID) => {
|
const up = async (contentTypeUID) => {
|
||||||
|
// Persist the stage join table
|
||||||
|
const { attributes, tableName } = strapi.db.metadata.get(contentTypeUID);
|
||||||
|
const joinTableName = attributes[ENTITY_STAGE_ATTRIBUTE].joinTable.name;
|
||||||
|
await persistTable(joinTableName, [tableName]);
|
||||||
|
|
||||||
|
// Update CT entities stage
|
||||||
return stagesService.updateEntitiesStage(contentTypeUID, {
|
return stagesService.updateEntitiesStage(contentTypeUID, {
|
||||||
fromStageId: null,
|
fromStageId: null,
|
||||||
toStageId: firstStage.id,
|
toStageId: firstStage.id,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
await removePersistedTablesWithSuffix('_strapi_review_workflows_stage_links');
|
||||||
|
|
||||||
return pipe([
|
return pipe([
|
||||||
getContentTypeUIDsWithActivatedReviewWorkflows,
|
getContentTypeUIDsWithActivatedReviewWorkflows,
|
||||||
// Iterate over UIDs to extend the content-type
|
// Iterate over UIDs to extend the content-type
|
||||||
|
@ -1,5 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { differenceWith, isEqual } = require('lodash/fp');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform table name to the object format
|
||||||
|
* @param {Array<string|{ table: string; dependsOn?: Array<{ table: string;}> }>} table
|
||||||
|
* @returns Array<{ table: string; dependsOn?: Array<{ table: string;}> }>
|
||||||
|
*/
|
||||||
|
const transformTableName = (table) => {
|
||||||
|
if (typeof table === 'string') {
|
||||||
|
return { name: table };
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds all tables in the database matching the regular expression
|
* Finds all tables in the database matching the regular expression
|
||||||
* @param {Object} ctx
|
* @param {Object} ctx
|
||||||
@ -16,22 +30,59 @@ async function findTables({ strapi }, regex) {
|
|||||||
* Add tables name to the reserved tables in core store
|
* Add tables name to the reserved tables in core store
|
||||||
* @param {Object} ctx
|
* @param {Object} ctx
|
||||||
* @param {Strapi} ctx.strapi
|
* @param {Strapi} ctx.strapi
|
||||||
* @param {string[]} tableNames
|
* @param {Array<string|{ table: string; dependsOn?: Array<{ table: string;}> }>} tableNames
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async function addPersistTables({ strapi }, tableNames) {
|
async function addPersistTables({ strapi }, tableNames) {
|
||||||
const persistedTables = await getPersistedTables({ strapi });
|
const persistedTables = await getPersistedTables({ strapi });
|
||||||
const notReservedTableNames = tableNames.filter((name) => !persistedTables.includes(name));
|
const tables = tableNames.map(transformTableName);
|
||||||
|
|
||||||
if (!notReservedTableNames.length) {
|
// Get new tables to be persisted, remove tables if they already were persisted
|
||||||
|
const notPersistedTableNames = differenceWith(isEqual, tables, persistedTables);
|
||||||
|
// Remove tables that are going to be changed
|
||||||
|
const tablesToPersist = differenceWith(
|
||||||
|
(t1, t2) => t1.name === t2.name,
|
||||||
|
persistedTables,
|
||||||
|
notPersistedTableNames
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!notPersistedTableNames.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
persistedTables.push(...notReservedTableNames);
|
tablesToPersist.push(...notPersistedTableNames);
|
||||||
await strapi.store.set({
|
await strapi.store.set({
|
||||||
type: 'core',
|
type: 'core',
|
||||||
key: 'persisted_tables',
|
key: 'persisted_tables',
|
||||||
value: persistedTables,
|
value: tablesToPersist,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove tables name from the reserved tables in core store
|
||||||
|
* @param {Object} ctx
|
||||||
|
* @param {Strapi} ctx.strapi
|
||||||
|
* @param {Array<string>} tableNames
|
||||||
|
* @return {Promise<void>}
|
||||||
|
*/
|
||||||
|
async function removePersistedTables({ strapi }, tableNames) {
|
||||||
|
const persistedTables = await getPersistedTables({ strapi });
|
||||||
|
|
||||||
|
// Get new tables to be persisted, remove tables if they already were persisted
|
||||||
|
const newPersistedTables = differenceWith(
|
||||||
|
(t1, t2) => t1.name === t2,
|
||||||
|
persistedTables,
|
||||||
|
tableNames
|
||||||
|
);
|
||||||
|
|
||||||
|
if (newPersistedTables.length === persistedTables.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await strapi.store.set({
|
||||||
|
type: 'core',
|
||||||
|
key: 'persisted_tables',
|
||||||
|
value: newPersistedTables,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,11 +92,15 @@ async function addPersistTables({ strapi }, tableNames) {
|
|||||||
* @param {Strapi} ctx.strapi
|
* @param {Strapi} ctx.strapi
|
||||||
* @returns {Promise<string[]>}
|
* @returns {Promise<string[]>}
|
||||||
*/
|
*/
|
||||||
const getPersistedTables = async ({ strapi }) =>
|
|
||||||
(await strapi.store.get({
|
async function getPersistedTables({ strapi }) {
|
||||||
|
const persistedTables = await strapi.store.get({
|
||||||
type: 'core',
|
type: 'core',
|
||||||
key: 'persisted_tables',
|
key: 'persisted_tables',
|
||||||
})) ?? [];
|
});
|
||||||
|
|
||||||
|
return (persistedTables || []).map(transformTableName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add all table names that start with a prefix to the reserved tables in
|
* Add all table names that start with a prefix to the reserved tables in
|
||||||
@ -62,19 +117,25 @@ const persistTablesWithPrefix = async (tableNamePrefix) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add all table names that end with a suffix to the reserved tables in core store
|
* Remove all table names that end with a suffix from the reserved tables in core store
|
||||||
* @param {string} tableNameSuffix
|
* @param {string} tableNameSuffix
|
||||||
* @return {Promise<void>}
|
* @return {Promise<void>}
|
||||||
*/
|
*/
|
||||||
const persistTablesWithSuffix = async (tableNameSuffix) => {
|
const removePersistedTablesWithSuffix = async (tableNameSuffix) => {
|
||||||
const tableNameRegex = new RegExp(`.*${tableNameSuffix}$`);
|
const tableNameRegex = new RegExp(`.*${tableNameSuffix}$`);
|
||||||
const tableNames = await findTables({ strapi }, tableNameRegex);
|
const tableNames = await findTables({ strapi }, tableNameRegex);
|
||||||
|
await removePersistedTables({ strapi }, tableNames);
|
||||||
|
};
|
||||||
|
|
||||||
await addPersistTables({ strapi }, tableNames);
|
const persistTable = async (tableName, dependsOn) => {
|
||||||
|
await addPersistTables({ strapi }, [
|
||||||
|
{ name: tableName, dependsOn: dependsOn?.map((depTableName) => ({ name: depTableName })) },
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
persistTablesWithPrefix,
|
persistTablesWithPrefix,
|
||||||
persistTablesWithSuffix,
|
removePersistedTablesWithSuffix,
|
||||||
|
persistTable,
|
||||||
findTables,
|
findTables,
|
||||||
};
|
};
|
||||||
|
@ -344,6 +344,13 @@ module.exports = (db) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const parsePersistedTable = (persistedTable) => {
|
||||||
|
if (typeof persistedTable === 'string') {
|
||||||
|
return persistedTable;
|
||||||
|
}
|
||||||
|
return persistedTable.name;
|
||||||
|
};
|
||||||
|
|
||||||
const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings')
|
const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings')
|
||||||
? (await strapi.store.get({
|
? (await strapi.store.get({
|
||||||
type: 'core',
|
type: 'core',
|
||||||
@ -351,12 +358,19 @@ module.exports = (db) => {
|
|||||||
})) ?? []
|
})) ?? []
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
|
const reservedTables = [...RESERVED_TABLE_NAMES, ...persistedTables.map(parsePersistedTable)];
|
||||||
|
|
||||||
for (const srcTable of srcSchema.tables) {
|
for (const srcTable of srcSchema.tables) {
|
||||||
if (
|
if (!helpers.hasTable(destSchema, srcTable.name) && !reservedTables.includes(srcTable.name)) {
|
||||||
!helpers.hasTable(destSchema, srcTable.name) &&
|
const dependencies = persistedTables
|
||||||
![...RESERVED_TABLE_NAMES, ...persistedTables].includes(srcTable.name)
|
.filter((table) => {
|
||||||
) {
|
return table?.dependsOn?.some((table) => table.name === srcTable.name);
|
||||||
removedTables.push(srcTable);
|
})
|
||||||
|
.map((dependsOnTable) => {
|
||||||
|
return srcSchema.tables.find((srcTable) => srcTable.name === dependsOnTable.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
removedTables.push(srcTable, ...dependencies);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user