From 41e7d2a5d0962eeb02c1e05f27cc75e3571baeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Mon, 6 Feb 2023 11:45:58 +0100 Subject: [PATCH 1/7] Rename audit logs table name --- .../audit-logs-local/lib/content-types/audit-log/schema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/providers/audit-logs-local/lib/content-types/audit-log/schema.js b/packages/providers/audit-logs-local/lib/content-types/audit-log/schema.js index f8f22616e6..312ae0ba36 100644 --- a/packages/providers/audit-logs-local/lib/content-types/audit-log/schema.js +++ b/packages/providers/audit-logs-local/lib/content-types/audit-log/schema.js @@ -2,7 +2,7 @@ module.exports = { kind: 'collectionType', - collectionName: 'audit_logs', + collectionName: 'strapi_audit_logs', info: { singularName: 'audit-log', pluralName: 'audit-logs', From ee2f5f07b4aed193d151a1eec68329bf0d48c1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Tue, 7 Feb 2023 12:11:28 +0100 Subject: [PATCH 2/7] Add auto db migration --- packages/core/admin/ee/server/register.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index d6e743bdc1..6f03dd937a 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -4,8 +4,31 @@ const { features } = require('@strapi/strapi/lib/utils/ee'); const executeCERegister = require('../../server/register'); const createAuditLogsService = require('./services/audit-logs'); +// Migrate the audit logs table name for users coming from v4.6.0 +const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => { + // Check if the table name needs to be migrated + const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; + const newName = contentTypes['admin::audit-log']?.collectionName; + const shouldMigrate = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; + + if (shouldMigrate) { + // Migrate the name audit logs table + if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { + await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); + } + + // Migrate the link table + if (await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links')) { + await strapi.db + .getSchemaConnection() + .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); + } + } +}; + module.exports = async ({ strapi }) => { if (features.isEnabled('audit-logs')) { + strapi.hook('strapi::content-types.beforeSync').register(migrateAuditLogsTable); const auditLogsService = createAuditLogsService(strapi); strapi.container.register('audit-logs', auditLogsService); await auditLogsService.register(); From d1a1754d3d1c33abceb70afbaf2e969d62f77f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Tue, 7 Feb 2023 12:27:35 +0100 Subject: [PATCH 3/7] Typo --- packages/core/admin/ee/server/register.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index 6f03dd937a..6f45eac9c2 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -12,7 +12,7 @@ const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => { const shouldMigrate = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; if (shouldMigrate) { - // Migrate the name audit logs table + // Migrate the main audit logs table if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); } From 3b0309b06f78e85d25e3635533e9f644bdb9e69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Tue, 7 Feb 2023 16:00:45 +0100 Subject: [PATCH 4/7] Fix test --- .../core/admin/ee/server/services/__tests__/audit-logs.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/admin/ee/server/services/__tests__/audit-logs.test.js b/packages/core/admin/ee/server/services/__tests__/audit-logs.test.js index 685187e0e2..57d289a60b 100644 --- a/packages/core/admin/ee/server/services/__tests__/audit-logs.test.js +++ b/packages/core/admin/ee/server/services/__tests__/audit-logs.test.js @@ -75,6 +75,7 @@ describe('Audit logs service', () => { deleteMany: mockEntityServiceDeleteMany, }, eventHub: createEventHub(), + hook: () => ({ register: jest.fn() }), requestContext: { get() { return { From a3e5ad460b3c18fad5b8de6d222f5082a03bd7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Tue, 7 Feb 2023 20:00:40 +0100 Subject: [PATCH 5/7] Check schema equality before migration --- packages/core/admin/ee/server/register.js | 39 +++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index 6f45eac9c2..8bbce13163 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -1,28 +1,41 @@ 'use strict'; +const { omit, isEqual } = require('lodash/fp'); const { features } = require('@strapi/strapi/lib/utils/ee'); const executeCERegister = require('../../server/register'); const createAuditLogsService = require('./services/audit-logs'); // Migrate the audit logs table name for users coming from v4.6.0 const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => { - // Check if the table name needs to be migrated + // Check if the audit logs table name was changed const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; const newName = contentTypes['admin::audit-log']?.collectionName; - const shouldMigrate = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; + const isMigratingTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; - if (shouldMigrate) { - // Migrate the main audit logs table - if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { - await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); - } + if (!isMigratingTable) { + return; + } - // Migrate the link table - if (await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links')) { - await strapi.db - .getSchemaConnection() - .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); - } + // Make sure the schemas are equal to avoid potential collisions + const schemasAreEqual = isEqual( + omit(['collectionName'], oldContentTypes['admin::audit-log'].__schema__), + omit(['collectionName'], contentTypes['admin::audit-log'].__schema__) + ); + + if (!schemasAreEqual) { + return; + } + + // Migrate the main audit logs table + if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { + await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); + } + + // Migrate the link table + if (await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links')) { + await strapi.db + .getSchemaConnection() + .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); } }; From 3a33df8e7e53547d82d4fb65e9fa8395a01b8b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Tue, 7 Feb 2023 20:35:04 +0100 Subject: [PATCH 6/7] Compare content type schema against db columns --- packages/core/admin/ee/server/register.js | 41 +++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index 8bbce13163..02abdcb05e 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -1,6 +1,5 @@ 'use strict'; -const { omit, isEqual } = require('lodash/fp'); const { features } = require('@strapi/strapi/lib/utils/ee'); const executeCERegister = require('../../server/register'); const createAuditLogsService = require('./services/audit-logs'); @@ -10,33 +9,39 @@ const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => { // Check if the audit logs table name was changed const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; const newName = contentTypes['admin::audit-log']?.collectionName; - const isMigratingTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; + const hasRenamedAuditLogsTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; - if (!isMigratingTable) { + if (!hasRenamedAuditLogsTable) { return; } - // Make sure the schemas are equal to avoid potential collisions - const schemasAreEqual = isEqual( - omit(['collectionName'], oldContentTypes['admin::audit-log'].__schema__), - omit(['collectionName'], contentTypes['admin::audit-log'].__schema__) - ); + // Check if the previous audit log tables exist + const hasAuditLogsTable = await strapi.db.getSchemaConnection().hasTable('audit_logs'); + const hasLinkTable = await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links'); - if (!schemasAreEqual) { + if (!hasAuditLogsTable || !hasLinkTable) { return; } - // Migrate the main audit logs table - if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { - await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); + // Check if the existing tables match the expected schema + const auditLogsColumnInfo = await strapi.db.connection('audit_logs').columnInfo(); + const linkColumnInfo = await strapi.db.connection('audit_logs_user_links').columnInfo(); + + if ( + !auditLogsColumnInfo.action || + !auditLogsColumnInfo.date || + !auditLogsColumnInfo.payload || + !linkColumnInfo.audit_log_id || + !linkColumnInfo.user_id + ) { + return; } - // Migrate the link table - if (await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links')) { - await strapi.db - .getSchemaConnection() - .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); - } + // Do the actual migrations + await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); + await strapi.db + .getSchemaConnection() + .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); }; module.exports = async ({ strapi }) => { From 4bbfab14fe1f4757dec2d1b1b9081e1446d634ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Wed, 8 Feb 2023 11:38:58 +0100 Subject: [PATCH 7/7] Move migration to dedicated folder --- .../ee/server/migrations/audit-logs-table.js | 45 +++++++++++++++++++ packages/core/admin/ee/server/register.js | 41 +---------------- 2 files changed, 46 insertions(+), 40 deletions(-) create mode 100644 packages/core/admin/ee/server/migrations/audit-logs-table.js diff --git a/packages/core/admin/ee/server/migrations/audit-logs-table.js b/packages/core/admin/ee/server/migrations/audit-logs-table.js new file mode 100644 index 0000000000..519fdbf363 --- /dev/null +++ b/packages/core/admin/ee/server/migrations/audit-logs-table.js @@ -0,0 +1,45 @@ +'use strict'; + +/** + * Migrate the audit logs table name for users coming from v4.6.0 + */ +async function migrateAuditLogsTable({ oldContentTypes, contentTypes }) { + // Check if the audit logs table name was changed + const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; + const newName = contentTypes['admin::audit-log']?.collectionName; + const hasRenamedAuditLogsTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; + + if (!hasRenamedAuditLogsTable) { + return; + } + + // Check if the previous audit log tables exist + const hasAuditLogsTable = await strapi.db.getSchemaConnection().hasTable('audit_logs'); + const hasLinkTable = await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links'); + + if (!hasAuditLogsTable || !hasLinkTable) { + return; + } + + // Check if the existing tables match the expected schema + const auditLogsColumnInfo = await strapi.db.connection('audit_logs').columnInfo(); + const linkColumnInfo = await strapi.db.connection('audit_logs_user_links').columnInfo(); + + if ( + !auditLogsColumnInfo.action || + !auditLogsColumnInfo.date || + !auditLogsColumnInfo.payload || + !linkColumnInfo.audit_log_id || + !linkColumnInfo.user_id + ) { + return; + } + + // Do the actual migrations + await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); + await strapi.db + .getSchemaConnection() + .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); +} + +module.exports = migrateAuditLogsTable; diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index 02abdcb05e..ed63e3be94 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -2,48 +2,9 @@ const { features } = require('@strapi/strapi/lib/utils/ee'); const executeCERegister = require('../../server/register'); +const migrateAuditLogsTable = require('./migrations/audit-logs-table'); const createAuditLogsService = require('./services/audit-logs'); -// Migrate the audit logs table name for users coming from v4.6.0 -const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => { - // Check if the audit logs table name was changed - const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; - const newName = contentTypes['admin::audit-log']?.collectionName; - const hasRenamedAuditLogsTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs'; - - if (!hasRenamedAuditLogsTable) { - return; - } - - // Check if the previous audit log tables exist - const hasAuditLogsTable = await strapi.db.getSchemaConnection().hasTable('audit_logs'); - const hasLinkTable = await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links'); - - if (!hasAuditLogsTable || !hasLinkTable) { - return; - } - - // Check if the existing tables match the expected schema - const auditLogsColumnInfo = await strapi.db.connection('audit_logs').columnInfo(); - const linkColumnInfo = await strapi.db.connection('audit_logs_user_links').columnInfo(); - - if ( - !auditLogsColumnInfo.action || - !auditLogsColumnInfo.date || - !auditLogsColumnInfo.payload || - !linkColumnInfo.audit_log_id || - !linkColumnInfo.user_id - ) { - return; - } - - // Do the actual migrations - await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); - await strapi.db - .getSchemaConnection() - .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); -}; - module.exports = async ({ strapi }) => { if (features.isEnabled('audit-logs')) { strapi.hook('strapi::content-types.beforeSync').register(migrateAuditLogsTable);