Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Prevent premature deletion of complimentary subscriptions on SQLite installs. #22178

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ObjectId = require('bson-objectid').default;
const {chunk: chunkArray} = require('lodash');
const debug = require('@tryghost/debug')('jobs:clean-expired-comped');
const moment = require('moment');
const DatabaseInfo = require('@tryghost/database-info');

// recurring job to clean expired complimentary subscriptions

Expand Down Expand Up @@ -31,10 +32,16 @@ if (parentPort) {
const cleanupStartDate = new Date();
const db = require('../../../data/db');
debug(`Starting cleanup of expired comp subscriptions`);
const expiredCompedRows = await db.knex('members_products')
.where('expiry_at', '<', moment.utc().startOf('day').toISOString())
.select('*');

let expiredCompedRows = [];
if (DatabaseInfo.isMySQL(db.knex)) {
expiredCompedRows = await db.knex('members_products')
.where('expiry_at', '<', moment.utc().startOf('day').toISOString())
.select('*');
} else {
expiredCompedRows = await db.knex('members_products')
.where('expiry_at', '<', moment.utc().startOf('day').valueOf())
.select('*');
}
Comment on lines +36 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cathysarisky I think it could be useful to leave a comment here explaining why we need to do this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also maybe reduce a little duplication here:

const expiryDate = DatabaseInfo.isMySQL(db.knex) 
    ? moment.utc().startOf('day').toISOString()
    : moment.utc().startOf('day').valueOf();

const expiredCompedRows = await db.knex('members_products')
        .where('expiry_at', '<', expiryDate)
        .select('*');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a test that verifies this change does what is intended? It looks like there isn't much coverage around this area, but this may be a good reference to base the test on

let deletedExpiredSubs = 0;
let updatedMembers = 0;

Expand Down