feature(ee): add db entries to persist ee feature tables

This commit is contained in:
Jamie Howard 2023-03-09 16:50:51 +00:00
parent 98632e1027
commit 32d019ecbe
2 changed files with 87 additions and 1 deletions

View File

@ -2,11 +2,23 @@
// eslint-disable-next-line node/no-extraneous-require
const { features } = require('@strapi/strapi/lib/utils/ee');
const { mapAsync } = require('@strapi/utils');
const executeCEBootstrap = require('../../server/bootstrap');
const { getService } = require('../../server/utils');
const actions = require('./config/admin-actions');
const {
createReservedTable,
findTablesThatStartWithPrefix,
doesReservedTableEntryExist,
createReservedTableEntries,
} = require('./utils/reserved-tables');
module.exports = async ({ strapi }) => {
const connection = strapi.db.getSchemaConnection();
await createReservedTable(connection);
const tablesToPersist = [];
module.exports = async () => {
const { actionProvider } = getService('permission');
if (features.isEnabled('sso')) {
@ -14,9 +26,26 @@ module.exports = async () => {
}
if (features.isEnabled('audit-logs')) {
const auditLogTables = await findTablesThatStartWithPrefix('strapi_audit_logs');
tablesToPersist.push(...auditLogTables);
await actionProvider.registerMany(actions.auditLogs);
}
if (features.isEnabled('review-workflows')) {
const reviewWorkflowTables = await findTablesThatStartWithPrefix('strapi_workflows');
tablesToPersist.push(...reviewWorkflowTables);
}
let recordsToInsert = await mapAsync(tablesToPersist, (tableName) =>
doesReservedTableEntryExist(tableName)
);
recordsToInsert = recordsToInsert.filter((record) => record);
if (recordsToInsert.length > 0) {
await createReservedTableEntries(recordsToInsert);
}
await getService('seat-enforcement').seatEnforcementWorkflow();
await executeCEBootstrap();

View File

@ -0,0 +1,57 @@
'use strict';
const reservedTableName = 'strapi_reserved_table_names';
/**
* Finds all tables in the database that start with a prefix
* @param {string} prefix
* @returns {Array}
*/
const findTablesThatStartWithPrefix = async (prefix) => {
const tables = await strapi.db.dialect.schemaInspector.getTables();
return tables.filter((tableName) => tableName.startsWith(prefix));
};
/**
* Check whether an entry exists in the DB for a reserved table name
* If no entry does exist return the tableName to be added to the DB
* @param {string} tableName
* @returns {string|undefined}
*/
const doesReservedTableEntryExist = async (tableName) => {
const rows = await strapi.db.getConnection()(reservedTableName).select().where('name', tableName);
if (rows.length === 0) {
return tableName;
}
};
/**
* Create entries in the DB for an array of reserved table names
* @param {Array} tableNames
*/
const createReservedTableEntries = async (tableNames) =>
strapi.db
.getConnection()(reservedTableName)
.insert(tableNames.map((tableName) => ({ name: tableName })));
/**
* Create the DB table 'strapi_reserved_table_names' if it does not exist
* @param {SchemaBuilder} connection to the strapi db
*/
const createReservedTable = async (connection) => {
if (await connection.hasTable(reservedTableName)) {
return;
}
return connection.createTable(reservedTableName, (table) => {
table.increments('id');
table.string('name');
});
};
module.exports = {
findTablesThatStartWithPrefix,
doesReservedTableEntryExist,
createReservedTableEntries,
createReservedTable,
};