PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
1.6.5 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 All 48 releases
fluent-cart / database / Migrations / OrderTaxRateMigrator.php

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

70 lines 2.4 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 OrderTaxRateMigrator extends Migrator
6 {
7
8 public static string $tableName = 'fct_order_tax_rate';
9
10 public static function getSqlSchema(): string
11 {
12 return "`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
13 `order_id` BIGINT(20) UNSIGNED NOT NULL,
14 `tax_rate_id` BIGINT(20) UNSIGNED NOT NULL,
15 `shipping_tax` BIGINT NULL,
16 `order_tax` BIGINT NULL,
17 `total_tax` BIGINT NULL,
18 `meta` json DEFAULT NULL,
19 `filed_at` DATETIME NULL,
20 `created_at` DATETIME NULL,
21 `updated_at` DATETIME NULL";
22 }
23
24 public static function migrated()
25 {
26 static::addMetaColumn();
27 static::addFiledAtColumn();
28 static::deduplicateOrderTaxRates();
29 static::addOrderTaxRateUniqueIndex();
30 }
31
32 public static function deduplicateOrderTaxRates()
33 {
34 // Keep only the latest row per (order_id, tax_rate_id) before adding the unique
35 // index — duplicate keys from the old write path would cause ALTER TABLE to fail.
36 //
37 // "Latest" = highest id. Duplicates existed because the original prepareOtherData()
38 // path did INSERT without a uniqueness guard; the last write was always authoritative
39 // (it overwrote the in-memory tax_data), so keeping the highest id is correct.
40 // Any rows deleted here were already superseded by a later write in the same order.
41 global $wpdb;
42 $table = $wpdb->prefix . static::$tableName;
43 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
44 $wpdb->query("
45 DELETE t1 FROM `{$table}` t1
46 INNER JOIN `{$table}` t2
47 ON t1.order_id = t2.order_id
48 AND t1.tax_rate_id = t2.tax_rate_id
49 AND t1.id < t2.id
50 ");
51 }
52
53 public static function addOrderTaxRateUniqueIndex()
54 {
55 static::addIndexIfNotExists('uniq_order_tax_rate', ['order_id', 'tax_rate_id'], true);
56 }
57
58 public static function addMetaColumn()
59 {
60 // "ALTER TABLE %i ADD COLUMN `meta` JSON"
61 static::addColumnIfNotExists('meta', 'JSON');
62 }
63
64 public static function addFiledAtColumn()
65 {
66 // "ALTER TABLE %i ADD COLUMN `filed_at` DATETIME NULL AFTER `meta`"
67 static::addColumnIfNotExists('filed_at', 'DATETIME NULL', 'meta');
68 }
69 }
70