| @@ -21,9 +21,9 @@ | ||
| 21 | 21 | |
| 22 | 22 | $table = $wpdb->prefix . 'fluentform_transactions'; |
| 23 | 23 | |
| 24 | 24 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Migration file, direct query needed |
| 25 | - if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { | |
| 25 | + if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) != $table) { | |
| 26 | 26 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Migration file, schema change is the purpose |
| 27 | 27 | $sql = "CREATE TABLE $table ( |
| 28 | 28 | id int(11) NOT NULL AUTO_INCREMENT, |
| 29 | 29 | transaction_hash varchar(255) NULL, |
| @@ -46,19 +46,51 @@ | ||
| 46 | 46 | payment_mode varchar(255), |
| 47 | 47 | payment_note longtext, |
| 48 | 48 | created_at timestamp NULL, |
| 49 | 49 | updated_at timestamp NULL, |
| 50 | - PRIMARY KEY (id) | |
| 50 | + PRIMARY KEY (id), | |
| 51 | + KEY ff_txn_created_at (created_at), | |
| 52 | + KEY ff_txn_form_created (form_id, created_at) | |
| 51 | 53 | ) $charsetCollate;"; |
| 52 | 54 | |
| 53 | - require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); | |
| 55 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
| 54 | 56 | |
| 55 | 57 | dbDelta($sql); |
| 56 | 58 | } else { |
| 57 | 59 | self::maybeAlterColumns(); |
| 58 | 60 | } |
| 61 | + | |
| 62 | + self::maybeAddIndexes(); | |
| 59 | 63 | } |
| 60 | 64 | |
| 65 | + public static function maybeAddIndexes() | |
| 66 | + { | |
| 67 | + global $wpdb; | |
| 68 | + $table = $wpdb->prefix . 'fluentform_transactions'; | |
| 69 | + | |
| 70 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, reading index metadata, %1s is for identifier | |
| 71 | + $indexes = $wpdb->get_results($wpdb->prepare('SHOW INDEX FROM %1s', $table)); | |
| 72 | + $existing = []; | |
| 73 | + if (is_array($indexes)) { | |
| 74 | + foreach ($indexes as $index) { | |
| 75 | + $existing[$index->Key_name] = true; | |
| 76 | + } | |
| 77 | + } | |
| 78 | + | |
| 79 | + // Reports and the MCP payment/trend tools filter transactions by created_at | |
| 80 | + // (cross-form) and by (form_id, created_at) (form-scoped); neither had an | |
| 81 | + // index, so every summary scanned the whole table. Additive + idempotent. | |
| 82 | + if (!isset($existing['ff_txn_created_at'])) { | |
| 83 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier | |
| 84 | + $wpdb->query($wpdb->prepare('ALTER TABLE %1s ADD INDEX ff_txn_created_at (created_at)', $table)); | |
| 85 | + } | |
| 86 | + | |
| 87 | + if (!isset($existing['ff_txn_form_created'])) { | |
| 88 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier | |
| 89 | + $wpdb->query($wpdb->prepare('ALTER TABLE %1s ADD INDEX ff_txn_form_created (form_id, created_at)', $table)); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 61 | 93 | public static function maybeAlterColumns() |
| 62 | 94 | { |
| 63 | 95 | global $wpdb; |
| 64 | 96 | $table = $wpdb->prefix . 'fluentform_transactions'; |
| @@ -64,9 +96,9 @@ | ||
| 64 | 96 | $table = $wpdb->prefix . 'fluentform_transactions'; |
| 65 | 97 | |
| 66 | 98 | // find the data types of each column |
| 67 | 99 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, checking table structure, %1s is for identifier |
| 68 | - $results = $wpdb->get_results($wpdb->prepare("DESCRIBE %1s", $table)); | |
| 100 | + $results = $wpdb->get_results($wpdb->prepare('DESCRIBE %1s', $table)); | |
| 69 | 101 | $items = []; |
| 70 | 102 | foreach ($results as $result) { |
| 71 | 103 | $items[$result->Field] = $result->Type; |
| 72 | 104 | } |
| @@ -73,9 +105,8 @@ | ||
| 73 | 105 | |
| 74 | 106 | $paymentTotalMigrated = strpos($items['payment_total'], 'bigint') !== false; |
| 75 | 107 | if (!$paymentTotalMigrated) { |
| 76 | 108 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier |
| 77 | - $wpdb->query($wpdb->prepare("ALTER TABLE %1s MODIFY payment_total BIGINT UNSIGNED DEFAULT 1", $table)); | |
| 109 | + $wpdb->query($wpdb->prepare('ALTER TABLE %1s MODIFY payment_total BIGINT UNSIGNED DEFAULT 1', $table)); | |
| 78 | 110 | } |
| 79 | - | |
| 80 | 111 | } |
| 81 | 112 | } |