| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Migrations; |
| 4 |
|
| 5 |
class OrderAddressesMigrator extends Migrator |
| 6 |
{ |
| 7 |
public static string $tableName = 'fct_order_addresses'; |
| 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 |
|
| 16 |
public static function getSqlSchema(): string |
| 17 |
{ |
| 18 |
return "`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 19 |
`order_id` BIGINT UNSIGNED NOT NULL, |
| 20 |
`type` VARCHAR(20) NOT NULL DEFAULT 'billing', |
| 21 |
`name` VARCHAR(192) NULL, |
| 22 |
`address_1` VARCHAR(192) NULL, |
| 23 |
`address_2` VARCHAR(192) NULL, |
| 24 |
`city` VARCHAR(192) NULL, |
| 25 |
`state` VARCHAR(192) NULL, |
| 26 |
`postcode` VARCHAR(50) NULL, |
| 27 |
`country` VARCHAR(100) NULL, |
| 28 |
`meta` JSON DEFAULT NULL, |
| 29 |
`created_at` DATETIME NULL, |
| 30 |
`updated_at` DATETIME NULL, |
| 31 |
|
| 32 |
INDEX `" . self::ORDER_ID_TYPE_INDEX . "` (`order_id` ASC, `type` ASC)"; |
| 33 |
} |
| 34 |
|
| 35 |
public static function migrated() |
| 36 |
{ |
| 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); |
| 58 |
} |
| 59 |
|
| 60 |
public static function addMetaColumn() |
| 61 |
{ |
| 62 |
// "ALTER TABLE %i ADD COLUMN `meta` JSON DEFAULT NULL AFTER `country`" |
| 63 |
static::addColumnIfNotExists('meta', 'JSON DEFAULT NULL', 'country'); |
| 64 |
} |
| 65 |
} |
| 66 |
|