Skip to content

Commit

Permalink
Check schema equality before migration
Browse files Browse the repository at this point in the history
  • Loading branch information
remidej committed Feb 7, 2023
1 parent 337df63 commit a3e5ad4
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions 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';

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');
}

// 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');
}
const isMigratingTable = oldName === 'audit_logs' && newName === 'strapi_audit_logs';

if (!isMigratingTable) {
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__)
);

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');
}
};

Expand Down

0 comments on commit a3e5ad4

Please sign in to comment.