Compare content type schema against db columns

This commit is contained in:
Rémi de Juvigny 2023-02-07 20:35:04 +01:00
parent a3e5ad460b
commit 3a33df8e7e

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { omit, isEqual } = require('lodash/fp');
const { features } = require('@strapi/strapi/lib/utils/ee'); const { features } = require('@strapi/strapi/lib/utils/ee');
const executeCERegister = require('../../server/register'); const executeCERegister = require('../../server/register');
const createAuditLogsService = require('./services/audit-logs'); const createAuditLogsService = require('./services/audit-logs');
@ -10,33 +9,39 @@ const migrateAuditLogsTable = async ({ oldContentTypes, contentTypes }) => {
// Check if the audit logs table name was changed // Check if the audit logs table name was changed
const oldName = oldContentTypes?.['admin::audit-log']?.collectionName; const oldName = oldContentTypes?.['admin::audit-log']?.collectionName;
const newName = contentTypes['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; return;
} }
// Make sure the schemas are equal to avoid potential collisions // Check if the previous audit log tables exist
const schemasAreEqual = isEqual( const hasAuditLogsTable = await strapi.db.getSchemaConnection().hasTable('audit_logs');
omit(['collectionName'], oldContentTypes['admin::audit-log'].__schema__), const hasLinkTable = await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links');
omit(['collectionName'], contentTypes['admin::audit-log'].__schema__)
);
if (!schemasAreEqual) { if (!hasAuditLogsTable || !hasLinkTable) {
return; return;
} }
// Migrate the main audit logs table // Check if the existing tables match the expected schema
if (await strapi.db.getSchemaConnection().hasTable('audit_logs')) { const auditLogsColumnInfo = await strapi.db.connection('audit_logs').columnInfo();
await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs'); 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 // Do the actual migrations
if (await strapi.db.getSchemaConnection().hasTable('audit_logs_user_links')) { await strapi.db.getSchemaConnection().renameTable('audit_logs', 'strapi_audit_logs');
await strapi.db await strapi.db
.getSchemaConnection() .getSchemaConnection()
.renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links'); .renameTable('audit_logs_user_links', 'strapi_audit_logs_user_links');
}
}; };
module.exports = async ({ strapi }) => { module.exports = async ({ strapi }) => {