Revert batch processing on bulk actions - too buggy

- minor fixes and cleanup
This commit is contained in:
Jonathan Labreuille
2016-05-27 14:07:54 +02:00
parent 3c46a5b434
commit 8292e9a744
4 changed files with 41 additions and 61 deletions

View File

@ -121,7 +121,7 @@ const bulk_actions = [
onSuccess: function(response) { onSuccess: function(response) {
MailPoet.Notice.success( MailPoet.Notice.success(
MailPoet.I18n.t('multipleSubscribersMovedToList') MailPoet.I18n.t('multipleSubscribersMovedToList')
.replace('%$1d', ~~response.subscribers) .replace('%$1d', ~~(response.subscribers))
.replace('%$2s', response.segment) .replace('%$2s', response.segment)
); );
} }

View File

@ -5,7 +5,6 @@ if(!defined('ABSPATH')) exit;
class Model extends \Sudzy\ValidModel { class Model extends \Sudzy\ValidModel {
protected $_errors; protected $_errors;
const MAX_BATCH_SIZE = 200;
function __construct() { function __construct() {
$this->_errors = array(); $this->_errors = array();
@ -91,7 +90,7 @@ class Model extends \Sudzy\ValidModel {
} }
function restore() { function restore() {
return $this->set_expr('deleted_at', 'NULl')->save(); return $this->set_expr('deleted_at', 'NULL')->save();
} }
static function bulkRestore($orm) { static function bulkRestore($orm) {
@ -99,7 +98,7 @@ class Model extends \Sudzy\ValidModel {
return self::bulkAction($orm, function($ids) use($model) { return self::bulkAction($orm, function($ids) use($model) {
self::rawExecute(join(' ', array( self::rawExecute(join(' ', array(
'UPDATE `'.$model::$_table.'`', 'UPDATE `'.$model::$_table.'`',
'SET `deleted_at`=NULL', 'SET `deleted_at` = NULL',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')' 'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
)), )),
$ids $ids
@ -112,48 +111,21 @@ class Model extends \Sudzy\ValidModel {
if($total === 0) return false; if($total === 0) return false;
$affected_rows = 0; $rows = $orm->select(static::$_table.'.id')
->offset(null)
->limit(null)
->findArray();
if($total > self::MAX_BATCH_SIZE) { $ids = array_map(function($model) {
for( return (int)$model['id'];
$i = 0, $batches = ceil($total / self::MAX_BATCH_SIZE); }, $rows);
$i < $batches;
$i++
) {
$rows = $orm->select('id')
->offset($i * self::MAX_BATCH_SIZE)
->limit(self::MAX_BATCH_SIZE)
->findArray();
$ids = array_map(function($model) { if($callback !== false) {
return (int)$model['id']; $callback($ids);
}, $rows);
if($callback !== false) {
$callback($ids);
}
// increment number of affected rows
$affected_rows += $orm->get_last_statement()->rowCount();
}
} else {
$rows = $orm->select(static::$_table.'.id')
->offset(null)
->limit(null)
->findArray();
$ids = array_map(function($model) {
return (int)$model['id'];
}, $rows);
if($callback !== false) {
$callback($ids);
}
// get number of affected rows
$affected_rows = $orm->get_last_statement()->rowCount();
} }
return $affected_rows;
// get number of affected rows
return $orm->get_last_statement()->rowCount();
} }
function duplicate($data = array()) { function duplicate($data = array()) {

View File

@ -544,13 +544,13 @@ class Subscriber extends Model {
if($segment === false) return false; if($segment === false) return false;
$subscribers_count = parent::bulkAction($orm, $subscribers_count = $orm->count();
function($subscriber_ids) use($segment) {
SubscriberSegment::deleteManySubscriptions( parent::bulkAction($orm, function($subscriber_ids) use($segment) {
$subscriber_ids, array($segment->id) SubscriberSegment::deleteManySubscriptions(
); $subscriber_ids, array($segment->id)
} );
); });
return array( return array(
'subscribers' => $subscribers_count, 'subscribers' => $subscribers_count,
@ -590,7 +590,9 @@ class Subscriber extends Model {
self::rawExecute(join(' ', array( self::rawExecute(join(' ', array(
'UPDATE `'.self::$_table.'`', 'UPDATE `'.self::$_table.'`',
'SET `deleted_at` = NOW()', 'SET `deleted_at` = NOW()',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($subscriber_ids)), ',').')', 'WHERE `id` IN ('.
rtrim(str_repeat('?,', count($subscriber_ids)), ',')
.')',
'AND `wp_user_id` IS NULL' 'AND `wp_user_id` IS NULL'
)), )),
$subscriber_ids $subscriber_ids

View File

@ -95,18 +95,24 @@ class SubscriberSegment extends Model {
} }
// create many subscriptions to each segment // create many subscriptions to each segment
foreach($segment_ids as $segment_id) { $values = array();
$query = array( $row_count = 0;
'INSERT IGNORE INTO `'.self::$_table.'`', foreach($segment_ids as &$segment_id) {
'(`subscriber_id`, `segment_id`, `status`)', foreach($subscriber_ids as &$subscriber_id) {
'VALUES '.rtrim(str_repeat( $values[] = (int)$subscriber_id;
"(?, ".(int)$segment_id.", '".Subscriber::STATUS_SUBSCRIBED."'), ", $values[] = (int)$segment_id;
count($subscriber_ids) $values[] = Subscriber::STATUS_SUBSCRIBED;
), ', ') $row_count++;
); }
self::rawExecute(join(' ', $query), $subscriber_ids);
} }
$query = array(
'INSERT IGNORE INTO `'.self::$_table.'`',
'(`subscriber_id`, `segment_id`, `status`)',
'VALUES '.rtrim(str_repeat('(?, ?, ?), ', $row_count), ', ')
);
self::rawExecute(join(' ', $query), $values);
return true; return true;
} }