| @@ -5,8 +5,15 @@ | ||
| 5 | 5 | class OrderAddressesMigrator extends Migrator |
| 6 | 6 | { |
| 7 | 7 | public static string $tableName = 'fct_order_addresses'; |
| 8 | 8 | |
| 9 | + /** | |
| 10 | + * Named once and reused by getSqlSchema(), migrated() and hasOrderIdTypeIndex(), | |
| 11 | + * so the three can never drift into declaring, creating and checking different | |
| 12 | + * index names. | |
| 13 | + */ | |
| 14 | + const ORDER_ID_TYPE_INDEX = 'idx_order_addresses_order_id_type'; | |
| 15 | + | |
| 9 | 16 | public static function getSqlSchema(): string |
| 10 | 17 | { |
| 11 | 18 | return "`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 12 | 19 | `order_id` BIGINT UNSIGNED NOT NULL, |
| @@ -19,14 +26,36 @@ | ||
| 19 | 26 | `postcode` VARCHAR(50) NULL, |
| 20 | 27 | `country` VARCHAR(100) NULL, |
| 21 | 28 | `meta` JSON DEFAULT NULL, |
| 22 | 29 | `created_at` DATETIME NULL, |
| 23 | - `updated_at` DATETIME NULL"; | |
| 30 | + `updated_at` DATETIME NULL, | |
| 31 | + | |
| 32 | + INDEX `" . self::ORDER_ID_TYPE_INDEX . "` (`order_id` ASC, `type` ASC)"; | |
| 24 | 33 | } |
| 25 | 34 | |
| 26 | 35 | public static function migrated() |
| 27 | 36 | { |
| 28 | 37 | static::addMetaColumn(); |
| 38 | + // Same index as getSqlSchema(), by the SAME name on purpose: the schema | |
| 39 | + // above covers fresh installs (the table is created with it), this call | |
| 40 | + // self-heals tables created before the index was declared. A different | |
| 41 | + // name here would leave those installs carrying two identical indexes. | |
| 42 | + static::addIndexIfNotExists(self::ORDER_ID_TYPE_INDEX, ['order_id', 'type']); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Whether the composite index is actually present on the table. | |
| 47 | + * | |
| 48 | + * Public because the DataBackfills delivery path has to VERIFY its work rather | |
| 49 | + * than assume it: addIndexIfNotExists() returns void and its ALTER goes through | |
| 50 | + * $wpdb->query(), which returns false on failure instead of throwing, so a | |
| 51 | + * backfill that cannot see the outcome would retire its slug with no index | |
| 52 | + * created. Kept here rather than reimplemented in DataBackfills so this class | |
| 53 | + * stays the only place that knows the index name. | |
| 54 | + */ | |
| 55 | + public static function hasOrderIdTypeIndex(): bool | |
| 56 | + { | |
| 57 | + return static::hasIndex(self::ORDER_ID_TYPE_INDEX); | |
| 29 | 58 | } |
| 30 | 59 | |
| 31 | 60 | public static function addMetaColumn() |
| 32 | 61 | { |