diff --git a/packages/core/admin/ee/server/bootstrap.js b/packages/core/admin/ee/server/bootstrap.js index 293cfab84c..657d50e3d5 100644 --- a/packages/core/admin/ee/server/bootstrap.js +++ b/packages/core/admin/ee/server/bootstrap.js @@ -5,6 +5,7 @@ const { features } = require('@strapi/strapi/lib/utils/ee'); const executeCEBootstrap = require('../../server/bootstrap'); const { getService } = require('../../server/utils'); const actions = require('./config/admin-actions'); +const { persistTablesWithPrefix } = require('./utils/persisted-tables'); module.exports = async () => { const { actionProvider } = getService('permission'); @@ -14,6 +15,8 @@ module.exports = async () => { } if (features.isEnabled('audit-logs')) { + await persistTablesWithPrefix('strapi_audit_logs'); + await actionProvider.registerMany(actions.auditLogs); } diff --git a/packages/core/admin/ee/server/utils/persisted-tables.js b/packages/core/admin/ee/server/utils/persisted-tables.js new file mode 100644 index 0000000000..f811e9a4b8 --- /dev/null +++ b/packages/core/admin/ee/server/utils/persisted-tables.js @@ -0,0 +1,49 @@ +'use strict'; + +/** + * 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)); +}; + +/** + * Get all reserved table names from the core store + * @returns {Array} + */ +const getPersistedTables = async () => + (await strapi.store.get({ + type: 'core', + key: 'persisted_tables', + })) ?? []; + +/** + * Add all table names that start with a prefix to the reserved tables in + * core store + * @param {string} tableNamePrefix + */ + +const persistTablesWithPrefix = async (tableNamePrefix) => { + const persistedTables = await getPersistedTables(); + const tableNames = await findTablesThatStartWithPrefix(tableNamePrefix); + const notReservedTableNames = tableNames.filter((name) => !persistedTables.includes(name)); + + if (!notReservedTableNames.length) { + return; + } + + persistedTables.push(...notReservedTableNames); + await strapi.store.set({ + type: 'core', + key: 'persisted_tables', + value: persistedTables, + }); +}; + +module.exports = { + persistTablesWithPrefix, + findTablesThatStartWithPrefix, +}; diff --git a/packages/core/database/lib/schema/__tests__/schema-diff.test.js b/packages/core/database/lib/schema/__tests__/schema-diff.test.js index 8b68f90b90..c01812ecf3 100644 --- a/packages/core/database/lib/schema/__tests__/schema-diff.test.js +++ b/packages/core/database/lib/schema/__tests__/schema-diff.test.js @@ -14,9 +14,15 @@ describe('diffSchemas', () => { }); diffSchemas = schemaDiff.diff.bind(schemaDiff); + + global.strapi = { + store: { + get: () => [], + }, + }; }); - test('New Table', () => { + test('New Table', async () => { const testTable = { name: 'my_table', }; @@ -29,7 +35,7 @@ describe('diffSchemas', () => { tables: [testTable], }; - expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({ + expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({ status: 'CHANGED', diff: { tables: { @@ -42,7 +48,7 @@ describe('diffSchemas', () => { }); }); - test('Removed Table', () => { + test('Removed Table', async () => { const testTable = { name: 'my_table', }; @@ -55,7 +61,7 @@ describe('diffSchemas', () => { tables: [], }; - expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({ + expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({ status: 'CHANGED', diff: { tables: { @@ -68,7 +74,7 @@ describe('diffSchemas', () => { }); }); - test('Unchanged Table', () => { + test('Unchanged Table', async () => { const testTable = { name: 'my_table', columns: [], @@ -84,7 +90,7 @@ describe('diffSchemas', () => { tables: [testTable], }; - expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({ + expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({ status: 'UNCHANGED', diff: { tables: { @@ -98,7 +104,7 @@ describe('diffSchemas', () => { }); describe('Changed table', () => { - test('added column', () => { + test('added column', async () => { const srcSchema = { tables: [ { @@ -125,7 +131,7 @@ describe('diffSchemas', () => { ], }; - expect(diffSchemas(srcSchema, destSchema)).toStrictEqual({ + expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({ status: 'CHANGED', diff: { tables: { @@ -178,4 +184,48 @@ describe('diffSchemas', () => { test.todo('unchanged foreign key'); test.todo('removed foreign key'); }); + + test('With persisted DB tables', async () => { + const testTables = [ + { + name: 'my_table', + }, + { + name: 'my_table_1', + }, + ]; + + const coreStoreTable = { + name: 'strapi_core_store_settings', + columns: [], + indexes: [], + foreignKeys: [], + }; + + global.strapi = { + store: { + get: async () => [testTables[0].name, 'table2'], + }, + }; + + const srcSchema = { + tables: [...testTables, coreStoreTable], + }; + + const destSchema = { + tables: [coreStoreTable], + }; + + expect(await diffSchemas(srcSchema, destSchema)).toStrictEqual({ + status: 'CHANGED', + diff: { + tables: { + added: [], + updated: [], + unchanged: [coreStoreTable], + removed: [testTables[1]], + }, + }, + }); + }); }); diff --git a/packages/core/database/lib/schema/diff.js b/packages/core/database/lib/schema/diff.js index 63c9eeef06..96f7018621 100644 --- a/packages/core/database/lib/schema/diff.js +++ b/packages/core/database/lib/schema/diff.js @@ -322,7 +322,7 @@ module.exports = (db) => { }; }; - const diffSchemas = (srcSchema, destSchema) => { + const diffSchemas = async (srcSchema, destSchema) => { const addedTables = []; const updatedTables = []; const unchangedTables = []; @@ -344,10 +344,17 @@ module.exports = (db) => { } } + const persistedTables = helpers.hasTable(srcSchema, 'strapi_core_store_settings') + ? (await strapi.store.get({ + type: 'core', + key: 'persisted_tables', + })) ?? [] + : []; + for (const srcTable of srcSchema.tables) { if ( !helpers.hasTable(destSchema, srcTable.name) && - !RESERVED_TABLE_NAMES.includes(srcTable.name) + ![...RESERVED_TABLE_NAMES, ...persistedTables].includes(srcTable.name) ) { removedTables.push(srcTable); } diff --git a/packages/core/database/lib/schema/index.js b/packages/core/database/lib/schema/index.js index b439dbb05b..a1dca13b23 100644 --- a/packages/core/database/lib/schema/index.js +++ b/packages/core/database/lib/schema/index.js @@ -50,7 +50,7 @@ const createSchemaProvider = (db) => { const DBSchema = await db.dialect.schemaInspector.getSchema(); - const { status, diff } = this.schemaDiff.diff(DBSchema, schema); + const { status, diff } = await this.schemaDiff.diff(DBSchema, schema); if (status === 'CHANGED') { await this.builder.updateSchema(diff);