prefix . 'ab_wd_units'; } public static function claimsTable(): string { global $wpdb; return $wpdb->prefix . 'ab_wd_claims'; } public static function eventsTable(): string { global $wpdb; return $wpdb->prefix . 'ab_wd_events'; } public static function idempotencyTable(): string { global $wpdb; return $wpdb->prefix . 'ab_wd_idempotency'; } public static function maybeUpgrade(): void { if (get_option(self::DB_VERSION_OPTION) !== self::DB_VERSION) { self::install(); } } public static function install(): void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $units = self::unitsTable(); $claims = self::claimsTable(); $events = self::eventsTable(); $idempotency = self::idempotencyTable(); // dbDelta is picky: one column per line, two spaces after PRIMARY KEY. // One row per unit of an order line; open_claim_id is a single column, // so a unit can be in at most one open claim (rule 1), and the // versioned update makes the second of two racing claims fail. dbDelta("CREATE TABLE {$units} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, order_id bigint(20) unsigned NOT NULL, order_item_id bigint(20) unsigned NOT NULL, unit_index int(10) unsigned NOT NULL, product_id bigint(20) unsigned NOT NULL DEFAULT 0, contract_date date NOT NULL, delivered_on date NULL, delivery_source varchar(20) NOT NULL, guarantee_months tinyint(3) unsigned NOT NULL DEFAULT 24, consumer varchar(20) NOT NULL, open_claim_id bigint(20) unsigned NULL, repairs text NOT NULL, repair_count int(10) unsigned NOT NULL DEFAULT 0, extended tinyint(1) NOT NULL DEFAULT 0, extended_by_claim_id bigint(20) unsigned NULL, terminated_by_claim_id bigint(20) unsigned NULL, operator_end_on date NULL, version int(10) unsigned NOT NULL DEFAULT 1, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY item_unit (order_item_id,unit_index), KEY order_id (order_id), KEY open_claim_id (open_claim_id) ) {$charset} ENGINE=InnoDB;"); dbDelta("CREATE TABLE {$claims} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, order_id bigint(20) unsigned NOT NULL, order_item_id bigint(20) unsigned NOT NULL, unit_ids text NOT NULL, reported_on date NOT NULL, manifested_on date NULL, accepted_facts text NULL, facts_revision int(10) unsigned NOT NULL DEFAULT 0, defect text NOT NULL, informativa_required tinyint(1) NOT NULL DEFAULT 0, state varchar(20) NOT NULL, review_reasons text NOT NULL, informed_on date NULL, informed_via varchar(20) NULL, informativa_version varchar(64) NULL, choice varchar(20) NULL, refurbished_requested tinyint(1) NOT NULL DEFAULT 0, remedy_refused tinyint(1) NOT NULL DEFAULT 0, goods_received_on date NULL, loaner_out tinyint(1) NOT NULL DEFAULT 0, outcome varchar(32) NULL, closed_on date NULL, refund_ids text NOT NULL, version int(10) unsigned NOT NULL DEFAULT 1, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY state_reported (state,reported_on), KEY order_id (order_id), KEY order_item_id (order_item_id) ) {$charset} ENGINE=InnoDB;"); dbDelta("CREATE TABLE {$events} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, subject varchar(10) NOT NULL, subject_id bigint(20) unsigned NOT NULL, event_type varchar(40) NOT NULL, actor_id bigint(20) unsigned NULL, reason varchar(255) NOT NULL DEFAULT '', before_json longtext NULL, after_json longtext NULL, external_ref varchar(191) NULL, idempotency_key varchar(191) NOT NULL, occurred_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY idempotency_key (idempotency_key), KEY subject (subject,subject_id,id) ) {$charset} ENGINE=InnoDB;"); // Rule 8: one row per Idempotency-Key; the primary key makes the // second of two concurrent requests with the same key fail and roll back. dbDelta("CREATE TABLE {$idempotency} ( idempotency_key varchar(191) NOT NULL, request_hash char(64) NOT NULL, subject varchar(10) NOT NULL, subject_id bigint(20) unsigned NOT NULL, created_at datetime NOT NULL, PRIMARY KEY (idempotency_key) ) {$charset} ENGINE=InnoDB;"); update_option(self::DB_VERSION_OPTION, self::DB_VERSION, true); // Autoloaded: read on every request. } public static function drop(): void { global $wpdb; // Uninstall with "delete data" chosen: drop the plugin's own tables. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange foreach ([self::eventsTable(), self::idempotencyTable(), self::claimsTable(), self::unitsTable()] as $table) { $wpdb->query($wpdb->prepare('DROP TABLE IF EXISTS %i', $table)); } // phpcs:enable delete_option(self::DB_VERSION_OPTION); } }