Migration_20221028_105818.php
3 months ago
Migration_20221110_151621.php
3 years ago
Migration_20230111_120000.php
2 years ago
Migration_20230111_130000.php
3 years ago
Migration_20230215_050813.php
3 months ago
Migration_20230221_200520.php
3 years ago
Migration_20230421_135915.php
3 years ago
Migration_20230503_210945.php
3 years ago
Migration_20230605_174836.php
3 years ago
Migration_20230703_105957.php
3 years ago
Migration_20230716_130221_Db.php
2 years ago
Migration_20230824_054259_Db.php
2 years ago
Migration_20230831_124214_Db.php
2 years ago
Migration_20230831_143755_Db.php
1 year ago
Migration_20240119_113943_Db.php
2 years ago
Migration_20240617_122847_Db.php
2 years ago
Migration_20240725_182318_Db.php
2 years ago
Migration_20241007_170437_Db.php
1 year ago
Migration_20241108_103249_Db.php
3 months ago
Migration_20250903_151331_Db.php
11 months ago
Migration_20250926_153050_Db.php
10 months ago
Migration_20260415_090055_Db.php
3 months ago
Migration_20260427_100000.php
3 months ago
Migration_20260428_120000.php
3 months ago
Migration_20260430_103000_Db.php
3 months ago
Migration_20260430_120000.php
3 months ago
Migration_20260504_120000_Db.php
3 months ago
Migration_20260514_120000_Db.php
3 months ago
Migration_20260609_120000_Db.php
2 months ago
Migration_20260610_120000_Db.php
1 month ago
Migration_20260622_120000_Db.php
1 month ago
Migration_20260709_120000_Db.php
1 month ago
Migration_20260715_100000_Db.php
1 month ago
index.php
3 years ago
Migration_20230215_050813.php
97 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrations\Db; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Migrator\DbMigration; |
| 9 | |
| 10 | class Migration_20230215_050813 extends DbMigration { |
| 11 | public function run(): void { |
| 12 | $this->subjectsMigration(); |
| 13 | $this->addMetaColumnToAutomations(); |
| 14 | } |
| 15 | |
| 16 | private function addMetaColumnToAutomations(): void { |
| 17 | |
| 18 | global $wpdb; |
| 19 | $tableName = esc_sql($wpdb->prefix . 'mailpoet_automations'); |
| 20 | if ($this->columnExists($tableName, 'meta')) { |
| 21 | return; |
| 22 | } |
| 23 | $this->connection->executeQuery("ALTER TABLE $tableName ADD COLUMN `meta` LONGTEXT DEFAULT NULL AFTER `status`"); |
| 24 | $this->connection->executeQuery("UPDATE $tableName SET `meta` = '{\"mailpoet:run-once-per-subscriber\":true}'"); |
| 25 | } |
| 26 | |
| 27 | private function subjectsMigration(): void { |
| 28 | $this->createTable('automation_run_subjects', [ |
| 29 | '`id` int(11) unsigned NOT NULL AUTO_INCREMENT', |
| 30 | '`automation_run_id` int(11) unsigned NOT NULL', |
| 31 | '`key` varchar(191)', |
| 32 | '`args` longtext', |
| 33 | '`hash` varchar(191)', |
| 34 | 'PRIMARY KEY (id)', |
| 35 | 'index (automation_run_id)', |
| 36 | 'index (hash)', |
| 37 | ]); |
| 38 | $this->moveSubjectData(); |
| 39 | $this->dropSubjectColumn(); |
| 40 | } |
| 41 | |
| 42 | private function moveSubjectData(): void { |
| 43 | global $wpdb; |
| 44 | $runTable = $wpdb->prefix . 'mailpoet_automation_runs'; |
| 45 | $subjectTable = $wpdb->prefix . 'mailpoet_automation_run_subjects'; |
| 46 | if (!$this->columnExists($runTable, 'subjects')) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $results = $wpdb->get_results($wpdb->prepare("SELECT id,subjects FROM %i", $runTable), ARRAY_A); |
| 51 | if (!is_array($results) || !$results) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | foreach ($results as $result) { |
| 56 | if (!is_array($result)) { |
| 57 | continue; |
| 58 | } |
| 59 | $subjects = $result['subjects'] ?? null; |
| 60 | if (!is_string($subjects) || $subjects === '') { |
| 61 | continue; |
| 62 | } |
| 63 | $subjects = json_decode($subjects, true); |
| 64 | if (!is_array($subjects) || !$subjects) { |
| 65 | continue; |
| 66 | } |
| 67 | $resultId = is_numeric($result['id'] ?? null) ? (int)$result['id'] : 0; |
| 68 | $values = []; |
| 69 | foreach ($subjects as $subject) { |
| 70 | if (!is_array($subject)) { |
| 71 | continue; |
| 72 | } |
| 73 | $key = is_string($subject['key'] ?? null) ? $subject['key'] : ''; |
| 74 | $values[] = (string)$wpdb->prepare("(%d,%s,%s)", $resultId, $key, (string)json_encode($subject['args'] ?? null)); |
| 75 | } |
| 76 | if (!$values) { |
| 77 | continue; |
| 78 | } |
| 79 | if ($wpdb->query($wpdb->prepare("INSERT INTO %i (`automation_run_id`, `key`, `args`) VALUES %s", $subjectTable, implode(',', $values))) === false) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | $wpdb->query($wpdb->prepare("UPDATE %i SET subjects = NULL WHERE id = %d", $runTable, $resultId)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private function dropSubjectColumn(): void { |
| 88 | global $wpdb; |
| 89 | $tableName = esc_sql($wpdb->prefix . 'mailpoet_automation_runs'); |
| 90 | if (!$this->columnExists($tableName, 'subjects')) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $wpdb->query($wpdb->prepare("ALTER TABLE %i DROP COLUMN subjects", $tableName)); |
| 95 | } |
| 96 | } |
| 97 |