From 201bef31f0248841678bb876790c6b1709776b00 Mon Sep 17 00:00:00 2001 From: Naz Gargol Date: Wed, 27 Nov 2019 17:00:27 +0700 Subject: [PATCH] Added transaction support to pagination plugin (#11421) Adds transaction support to `fetchPage` method. This is needed to be able to count members during the post publish transaction. This is the next iteration over initial quick-fix: 90905b02125b138db18898b02bc305f20392f0b0 * Added transaction support to pagination plugin - This support is needed to be able to use `fetchPage` method in transactional context (example usecase was counting members when publishing post for emails) * Passed transaction related options during email creation - Without this SQLite would hang in a transaction and eventually timeout * Updated parameter name for consistency --- core/server/models/plugins/pagination.js | 11 +++++++++-- core/server/services/mega/mega.js | 9 ++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/core/server/models/plugins/pagination.js b/core/server/models/plugins/pagination.js index 93cddb1d4f..53da976c2e 100644 --- a/core/server/models/plugins/pagination.js +++ b/core/server/models/plugins/pagination.js @@ -144,11 +144,18 @@ pagination = function pagination(bookshelf) { // Get the table name and idAttribute for this model var tableName = _.result(this.constructor.prototype, 'tableName'), idAttribute = _.result(this.constructor.prototype, 'idAttribute'), - self = this, + self = this; + + let countPromise; + if (options.transacting) { + countPromise = this.query().clone().transacting(options.transacting).select( + bookshelf.knex.raw('count(distinct ' + tableName + '.' + idAttribute + ') as aggregate') + ); + } else { countPromise = this.query().clone().select( bookshelf.knex.raw('count(distinct ' + tableName + '.' + idAttribute + ') as aggregate') ); - + } // #### Pre count clauses // Add any where or join clauses which need to be included with the aggregate query diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index 580e5c6632..9044ea2e37 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -51,16 +51,11 @@ const sendTestEmail = async (postModel, toEmails) => { * * @param {object} postModel Post Model Object */ + const addEmail = async (postModel, options) => { const knexOptions = _.pick(options, ['transacting', 'forUpdate']); - // TODO: this is using the Member model directly rather than the members - // service because the service is hardcoded to Member.findPage and our - // pagination plugin does not currently work with transactions - const members = await models.Member - .findAll(Object.assign({filter: 'subscribed:true'}, knexOptions)) - .map(member => member.toJSON(options)); - + const {members} = await membersService.api.members.list(Object.assign(knexOptions, {filter: 'subscribed:true'}, {limit: 'all'})); const {emailTmpl, emails} = await getEmailData(postModel, members); // NOTE: don't create email object when there's nobody to send the email to