← All changes
|
src/Campaigns/Migrations/RevenueTable/AddIndexes.php
+19
-1
4.16.3
→
4.17.0
View file →
| @@ -37,8 +37,12 @@ | ||
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * @inheritDoc |
| 41 | + * | |
| 42 | + * @since 4.17.0 Only add indexes that do not exist yet, so re-running the migration is safe. | |
| 43 | + * @since 4.0.0 | |
| 44 | + * | |
| 41 | 45 | * @throws DatabaseMigrationException |
| 42 | 46 | */ |
| 43 | 47 | public function run() |
| 44 | 48 | { |
| @@ -44,9 +48,23 @@ | ||
| 44 | 48 | { |
| 45 | 49 | global $wpdb; |
| 46 | 50 | |
| 47 | 51 | try { |
| 48 | - DB::query("ALTER TABLE {$wpdb->give_revenue} ADD INDEX (form_id), ADD INDEX (campaign_id)"); | |
| 52 | + $existing = DB::get_col("SHOW INDEX FROM {$wpdb->give_revenue}", 4); | |
| 53 | + | |
| 54 | + $indexes = array_filter(['form_id', 'campaign_id'], static function ($column) use ($existing) { | |
| 55 | + return !in_array($column, $existing, true); | |
| 56 | + }); | |
| 57 | + | |
| 58 | + if (!$indexes) { | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + $clauses = array_map(static function ($column) { | |
| 63 | + return "ADD INDEX ($column)"; | |
| 64 | + }, $indexes); | |
| 65 | + | |
| 66 | + DB::query("ALTER TABLE {$wpdb->give_revenue} " . implode(', ', $clauses)); | |
| 49 | 67 | } catch (DatabaseQueryException $exception) { |
| 50 | 68 | throw new DatabaseMigrationException("An error occurred while updating the {$wpdb->give_revenue} table", 0, |
| 51 | 69 | $exception); |
| 52 | 70 | } |