Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cms/config/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ module.exports = ({ env }) => ({
auth: {
secret: env("ADMIN_JWT_SECRET", "9e07dfd3396d88a43609466615259199"),
},
transfer: {
token: {
salt: env("TRANSFER_TOKEN_SALT","WQ4H4aPPLF2eL4IEaqGOKQ=="),
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

/**
* Get the link tables names that need to be updated
*/
const getLinkTables = ({ strapi }) => {
const contentTypes = strapi.db.metadata;
const tablesToUpdate = {};

contentTypes.forEach((contentType) => {
// Get attributes
const attributes = contentType.attributes;

// For each relation type, add the joinTable name to tablesToUpdate
Object.values(attributes).forEach((attribute) => {
if (
attribute.type === 'relation' &&
attribute.joinTable &&
!attribute.relation.startsWith('morph') // Ignore polymorphic relations
) {
tablesToUpdate[attribute.joinTable.name] = attribute.joinTable;
}
});
});

return Object.values(tablesToUpdate);
};

async function up(trx) {
const linkTablesToUpdate = getLinkTables({ strapi });

// Remove duplicates from link tables
for (const table of linkTablesToUpdate) {
const tableExists = await trx.schema.hasTable(table.name);
if (!tableExists) continue;

strapi.log.info(`Deleting duplicates of table ${table.name}...`);

try {
// Query to delete duplicates from a link table
let query = `
CREATE TEMPORARY TABLE tmp as SELECT DISTINCT t2.id as id
FROM ?? as t1 JOIN ?? as t2
ON t1.id < t2.id
`;
const pivotWhereParams = [];

// For each pivot column, add a on condition to the query
table.pivotColumns.forEach(column => {
query += ` AND t1.?? = t2.??`;
pivotWhereParams.push(column, column);
});

// Create temporary table with the ids of the repeated rows
await trx.raw(query, [table.name, table.name, ...pivotWhereParams]);

// Delete repeated rows from the original table
await trx.raw(`DELETE FROM ?? WHERE id in (SELECT * FROM tmp)`, [table.name]);
} finally {
// Drop temporary table
await trx.raw(`DROP TABLE IF EXISTS tmp `);
}
}
}

async function down() {}

module.exports = { up, down };
File renamed without changes
8 changes: 4 additions & 4 deletions cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"devDependencies": {},
"dependencies": {
"@octokit/core": "^3.5.1",
"@strapi/plugin-i18n": "4.1.11",
"@strapi/plugin-users-permissions": "4.1.11",
"@strapi/strapi": "4.1.11",
"@strapi/plugin-i18n": "4.9.2",
"@strapi/plugin-users-permissions": "4.9.2",
"@strapi/strapi": "4.9.2",
"axios": "^0.27.2",
"sqlite3": "^5.0.8"
"better-sqlite3": "7.4.6"
},
"author": {
"name": "A Strapi developer"
Expand Down
Loading