57 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-12-06 12:14:33 +01:00
'use strict';
2022-12-12 18:09:24 +01:00
const { features } = require('@strapi/strapi/lib/utils/ee');
2022-12-07 14:43:28 +01:00
const executeCERegister = require('../../server/register');
2022-12-12 18:09:24 +01:00
const createAuditLogsService = require('./services/audit-logs');
2022-12-06 12:14:33 +01:00
2023-02-07 12:11:28 +01:00
// Migrate the audit logs table name for users coming from v4.6.0
const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => {
2023-02-07 20:00:40 +01:00
// Check if the audit logs table name was changed
2023-02-07 12:11:28 +01:00
const oldName = oldContentTypes?.['admin::audit-log']?.collectionName;
const newName = contentTypes['admin::audit-log']?.collectionName;
const hasRenamedAuditLogsTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs';
2023-02-07 20:00:40 +01:00
if (!hasRenamedAuditLogsTable) {
2023-02-07 20:00:40 +01:00
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');
2023-02-07 20:00:40 +01:00
if (!hasAuditLogsTable || !hasLinkTable) {
2023-02-07 20:00:40 +01:00
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;
2023-02-07 20:00:40 +01:00
}
// 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');
2023-02-07 12:11:28 +01:00
};
2022-12-06 12:14:33 +01:00
module.exports = async ({ strapi }) => {
2022-12-12 18:09:24 +01:00
if (features.isEnabled('audit-logs')) {
2023-02-07 12:11:28 +01:00
strapi.hook('strapi::content-types.beforeSync').register(migrateAuditLogsTable);
2022-12-12 18:09:24 +01:00
const auditLogsService = createAuditLogsService(strapi);
strapi.container.register('audit-logs', auditLogsService);
2022-12-21 12:37:47 +01:00
await auditLogsService.register();
2022-12-12 18:09:24 +01:00
}
2022-12-21 18:39:15 +01:00
await executeCERegister({ strapi });
2022-12-06 12:14:33 +01:00
};