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 / SubscriptionsMigrator.php

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

133 lines 5.0 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 use FluentCart\App\Models\Subscription;
6 class SubscriptionsMigrator extends Migrator
7 {
8
9 public static string $tableName = "fct_subscriptions";
10
11
12 public static function getSqlSchema(): string
13 {
14 $indexPrefix = static::getDbPrefix() . 'fct_index_';
15
16 return "`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
17 `uuid` VARCHAR(100) NOT NULL,
18 `customer_id` BIGINT(20) UNSIGNED NOT NULL,
19 `parent_order_id` BIGINT(20) UNSIGNED NOT NULL,
20 `product_id` BIGINT(20) UNSIGNED NOT NULL,
21 `item_name` TEXT NOT NULL,
22 `quantity` INT NOT NULL DEFAULT '1',
23 `variation_id` BIGINT(20) UNSIGNED NOT NULL,
24 `billing_interval` VARCHAR(45) NULL,
25 `signup_fee` BIGINT UNSIGNED NOT NULL DEFAULT 0,
26 `initial_tax_total` BIGINT UNSIGNED NOT NULL DEFAULT 0,
27 `recurring_amount` BIGINT UNSIGNED NOT NULL DEFAULT 0,
28 `recurring_tax_total` BIGINT UNSIGNED NOT NULL DEFAULT 0,
29 `recurring_total` BIGINT UNSIGNED NOT NULL DEFAULT 0,
30 `bill_times` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
31 `bill_count` INT UNSIGNED NOT NULL DEFAULT 0,
32 `expire_at` DATETIME NULL,
33 `trial_ends_at` DATETIME NULL,
34 `canceled_at` DATETIME NULL,
35 `restored_at` DATETIME NULL,
36 `collection_method` ENUM('automatic', 'manual', 'system') NOT NULL DEFAULT 'automatic',
37 `next_billing_date` DATETIME NULL,
38 `trial_days` INT(10) UNSIGNED NOT NULL DEFAULT 0,
39 `vendor_customer_id` VARCHAR(45) NULL,
40 `vendor_plan_id` VARCHAR(45) NULL,
41 `vendor_subscription_id` VARCHAR(45) NULL,
42 `status` VARCHAR(45) NULL,
43 `original_plan` LONGTEXT NULL,
44 `vendor_response` LONGTEXT NULL,
45 `current_payment_method` VARCHAR(45) NULL,
46 `config` json DEFAULT NULL,
47 `created_at` DATETIME NULL,
48 `updated_at` DATETIME NULL,
49
50 INDEX `{$indexPrefix}_order_subscription_idx` (`parent_order_id` ASC),
51 INDEX `{$indexPrefix}vendor_subscription_id_idx` (`vendor_subscription_id` ASC),
52 INDEX `{$indexPrefix}_expiry_scan_idx` (`status`, `next_billing_date`, `id`),
53 INDEX `{$indexPrefix}collection_method_idx` (`collection_method`)";
54 }
55
56 public static function migrated()
57 {
58 static::addUuidColumn();
59 static::renameInitialAmountToSignupFee();
60 static::backfillEmptyUuids();
61 static::addVendorSubscriptionIdIndex();
62 static::addExpiryScanIndex();
63 static::addCollectionMethodIndex();
64 }
65
66 // Serves the store-managed cron guard: whereIn(collection_method,[manual,system])->exists().
67 // Online DDL, builds over existing rows; column is NOT NULL so no legacy backfill.
68 public static function addCollectionMethodIndex()
69 {
70 static::addIndexIfNotExists(
71 static::getDbPrefix() . 'fct_index_collection_method_idx',
72 'collection_method'
73 );
74 }
75
76 public static function addVendorSubscriptionIdIndex()
77 {
78 static::addIndexIfNotExists(
79 static::getDbPrefix() . 'fct_index_vendor_subscription_id_idx',
80 'vendor_subscription_id'
81 );
82 }
83
84 public static function addExpiryScanIndex()
85 {
86 static::addIndexIfNotExists(
87 static::getDbPrefix() . 'fct_index_expiry_scan_idx',
88 ['status', 'next_billing_date', 'id']
89 );
90 }
91
92 public static function addUuidColumn()
93 {
94 // "ALTER TABLE %i ADD COLUMN `uuid` VARCHAR(100) NOT NULL DEFAULT '' AFTER `id`"
95 static::addColumnIfNotExists('uuid', "VARCHAR(100) NOT NULL DEFAULT ''", 'id');
96 }
97
98 public static function renameInitialAmountToSignupFee()
99 {
100 // "ALTER TABLE %i CHANGE `initial_amount` `signup_fee` BIGINT UNSIGNED NOT NULL DEFAULT 0"
101 static::renameColumnIfExists('initial_amount', 'signup_fee', 'BIGINT UNSIGNED NOT NULL DEFAULT 0');
102 }
103
104 public static function backfillEmptyUuids()
105 {
106 $chunkSize = 500;
107
108 do {
109 $subscriptions = Subscription::select('id')
110 ->where(function ($query) {
111 $query->where('uuid', '')
112 ->orWhereNull('uuid');
113 })
114 ->limit($chunkSize)
115 ->get();
116
117 if ($subscriptions->isEmpty()) {
118 break;
119 }
120
121 $uuids = [];
122 foreach ($subscriptions as $subscription) {
123 $uuids[] = [
124 'id' => $subscription->id,
125 'uuid' => md5(time() . wp_generate_uuid4())
126 ];
127 }
128
129 (new Subscription())->batchUpdate($uuids);
130 } while ($subscriptions->count() >= $chunkSize);
131 }
132 }
133