PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / database / Migrations / OrderAddressesMigrator.php

OrderAddressesMigrator.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at database/Migrations/OrderAddressesMigrator.php

66 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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