Migration_20221028_105818_App.php
3 years ago
Migration_20230109_144830.php
3 years ago
Migration_20230131_121621.php
3 years ago
Migration_20230419_080000.php
3 years ago
Migration_20230425_211517.php
3 years ago
Migration_20230712_180341.php
3 years ago
Migration_20230803_200413_App.php
2 years ago
Migration_20230825_093531_App.php
1 year ago
Migration_20231128_120355_App.php
1 year ago
Migration_20240202_130053_App.php
2 years ago
Migration_20240207_105912_App.php
1 year ago
Migration_20240322_110443_App.php
2 years ago
Migration_20240730_212419_App.php
2 years ago
Migration_20241015_105511_App.php
1 year ago
Migration_20241128_114257_App.php
1 year ago
Migration_20250120_094614_App.php
1 year ago
Migration_20250501_114655_App.php
11 months ago
Migration_20260421_155908_App.php
3 months ago
Migration_20260515_120000_App.php
3 months ago
Migration_20260623_120000_App.php
1 month ago
Migration_20260805_120000_App.php
1 week ago
index.php
3 years ago
Migration_20231128_120355_App.php
87 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\App; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Doctrine\WPDB\Connection as WPDBConnection; |
| 9 | use MailPoet\Entities\NewsletterEntity; |
| 10 | use MailPoet\Entities\ScheduledTaskEntity; |
| 11 | use MailPoet\Entities\SendingQueueEntity; |
| 12 | use MailPoet\Migrator\AppMigration; |
| 13 | use MailPoet\WooCommerce\Helper; |
| 14 | use MailPoetVendor\Doctrine\DBAL\ArrayParameterType; |
| 15 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 16 | |
| 17 | /** |
| 18 | * Due to a bug https://mailpoet.atlassian.net/browse/MAILPOET-5719 we need to fix already existing data. |
| 19 | * The performance optimization we changed the method for updating counts in the sending queue after finishing the scheduled task. |
| 20 | * This change affected counts in automatic emails, because the value of processed emails has min and max value calculated from the total count. |
| 21 | */ |
| 22 | class Migration_20231128_120355_App extends AppMigration { |
| 23 | public function run(): void { |
| 24 | $wooCommerceHelper = $this->container->get(Helper::class); |
| 25 | |
| 26 | // If Woo is not active and the table doesn't exist, we can skip this migration |
| 27 | if (!$wooCommerceHelper->isWooCommerceActive()) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Temporarily skip the queries in WP Playground. |
| 32 | // UPDATE with JOIN is not yet supported by the SQLite integration. |
| 33 | if (WPDBConnection::isSQLite()) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | $connection = $this->container->get(Connection::class); |
| 38 | |
| 39 | // Fix data for completed tasks |
| 40 | $sendingQueuesTable = $this->getTableName(SendingQueueEntity::class); |
| 41 | $scheduledTasksTable = $this->getTableName(ScheduledTaskEntity::class); |
| 42 | $newslettersTable = $this->getTableName(NewsletterEntity::class); |
| 43 | $newsletterTypes = [NewsletterEntity::TYPE_AUTOMATIC, NewsletterEntity::TYPE_WELCOME]; |
| 44 | $statusCompleted = ScheduledTaskEntity::STATUS_COMPLETED; |
| 45 | $connection->executeStatement(" |
| 46 | UPDATE {$sendingQueuesTable} |
| 47 | JOIN {$scheduledTasksTable} ON {$scheduledTasksTable}.id = {$sendingQueuesTable}.task_id |
| 48 | JOIN {$newslettersTable} ON {$newslettersTable}.id = {$sendingQueuesTable}.newsletter_id |
| 49 | SET {$sendingQueuesTable}.count_total = 1, |
| 50 | {$sendingQueuesTable}.count_processed = 1, |
| 51 | {$sendingQueuesTable}.count_to_process = 0 |
| 52 | WHERE {$newslettersTable}.type IN (:newsletterTypes) |
| 53 | AND {$scheduledTasksTable}.status = :taskStatus |
| 54 | ", [ |
| 55 | 'newsletterTypes' => $newsletterTypes, |
| 56 | 'taskStatus' => $statusCompleted, |
| 57 | ], [ |
| 58 | 'newsletterTypes' => ArrayParameterType::STRING, |
| 59 | ]); |
| 60 | |
| 61 | // Fix data for scheduled tasks |
| 62 | $statusScheduled = ScheduledTaskEntity::STATUS_SCHEDULED; |
| 63 | $connection->executeStatement(" |
| 64 | UPDATE {$sendingQueuesTable} |
| 65 | JOIN {$scheduledTasksTable} ON {$scheduledTasksTable}.id = {$sendingQueuesTable}.task_id |
| 66 | JOIN {$newslettersTable} ON {$newslettersTable}.id = {$sendingQueuesTable}.newsletter_id |
| 67 | SET {$sendingQueuesTable}.count_total = 1, |
| 68 | {$sendingQueuesTable}.count_processed = 0, |
| 69 | {$sendingQueuesTable}.count_to_process = 1 |
| 70 | WHERE {$newslettersTable}.type IN (:newsletterTypes) |
| 71 | AND {$scheduledTasksTable}.status = :taskStatus |
| 72 | ", [ |
| 73 | 'newsletterTypes' => $newsletterTypes, |
| 74 | 'taskStatus' => $statusScheduled, |
| 75 | ], [ |
| 76 | 'newsletterTypes' => ArrayParameterType::STRING, |
| 77 | ]); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param class-string $entityClassName |
| 82 | */ |
| 83 | private function getTableName(string $entityClassName): string { |
| 84 | return $this->entityManager->getClassMetadata($entityClassName)->getTableName(); |
| 85 | } |
| 86 | } |
| 87 |