mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Removed duplicate offer redemptions (#14011)
refs https://github.com/TryGhost/Team/issues/1257 As well as fixing the code so that we do not create duplicate offer redemptions going forward, we need to clean up the existing database. We loop in code because the query to find and delete duplicates is complicated, and will introduce more risk
This commit is contained in:
parent
a565da06b2
commit
8a81cd0a36
1 changed files with 46 additions and 0 deletions
|
@ -0,0 +1,46 @@
|
|||
const logging = require('@tryghost/logging');
|
||||
|
||||
const {createTransactionalMigration} = require('../../utils');
|
||||
|
||||
module.exports = createTransactionalMigration(
|
||||
async function up(knex) {
|
||||
if (knex.client.config.client !== 'mysql') {
|
||||
logging.warn('Skipping cleanup of duplicate offer redemptions - database is not MySQL');
|
||||
return;
|
||||
}
|
||||
logging.info('Looking for duplicate offer redemptions.');
|
||||
|
||||
const duplicates = await knex('offer_redemptions')
|
||||
.select('subscription_id')
|
||||
.count('subscription_id as count')
|
||||
.groupBy('subscription_id')
|
||||
.having('count', '>', 1);
|
||||
|
||||
if (!duplicates.length) {
|
||||
logging.info('No duplicate offer redemptions found.');
|
||||
return;
|
||||
}
|
||||
|
||||
logging.info(`Found ${duplicates.length} duplicate offer redemptions.`);
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const duplicate of duplicates) {
|
||||
const offerRedemptions = await knex('offer_redemptions')
|
||||
.select('id')
|
||||
.where('subscription_id', duplicate.subscription_id);
|
||||
|
||||
const [offerRedemptionToKeep, ...offerRedemptionsToDelete] = offerRedemptions;
|
||||
|
||||
logging.info(`Keeping offer redemption ${offerRedemptionToKeep.id}`);
|
||||
|
||||
logging.info(`Deleting ${offerRedemptionsToDelete.length} duplicates`);
|
||||
await knex('offer_redemptions')
|
||||
.whereIn('id', offerRedemptionsToDelete.map(x => x.id))
|
||||
.del();
|
||||
}
|
||||
},
|
||||
async function down() {
|
||||
logging.warn('Not recreating duplicate offer redemptions');
|
||||
return;
|
||||
}
|
||||
);
|
Loading…
Add table
Reference in a new issue