| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Database\Migrations; |
| 4 |
|
| 5 |
class ConversationsMigrator |
| 6 |
{ |
| 7 |
static $tableName = 'fs_conversations'; |
| 8 |
|
| 9 |
public static function migrate() |
| 10 |
{ |
| 11 |
global $wpdb; |
| 12 |
|
| 13 |
$charsetCollate = $wpdb->get_charset_collate(); |
| 14 |
$table = $wpdb->prefix . static::$tableName; |
| 15 |
|
| 16 |
if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { |
| 17 |
$sql = "CREATE TABLE $table ( |
| 18 |
`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 19 |
`serial` INT(11) UNSIGNED DEFAULT 1, |
| 20 |
`ticket_id` BIGINT(20) UNSIGNED NOT NULL, |
| 21 |
`person_id` BIGINT(20) UNSIGNED NOT NULL, |
| 22 |
`conversation_type` VARCHAR(100) DEFAULT 'response', |
| 23 |
`content` LONGTEXT NULL, |
| 24 |
`source` VARCHAR(100) DEFAULT 'web', |
| 25 |
`content_hash` VARCHAR(192) NULL, |
| 26 |
`message_id` VARCHAR(192) NULL, |
| 27 |
`is_important` ENUM('yes', 'no') DEFAULT 'no', |
| 28 |
`created_at` TIMESTAMP NULL, |
| 29 |
`updated_at` TIMESTAMP NULL, |
| 30 |
INDEX `idx_ticket_id` (`ticket_id`), |
| 31 |
INDEX `idx_person_id` (`person_id`), |
| 32 |
INDEX `idx_created_at` (`created_at`), |
| 33 |
INDEX `idx_person_id_created_at` (`person_id`, `created_at`) |
| 34 |
) $charsetCollate;"; |
| 35 |
$created = dbDelta($sql); |
| 36 |
return $created; |
| 37 |
} else { |
| 38 |
static::alterTable($table); |
| 39 |
} |
| 40 |
|
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
public static function alterTable($table) |
| 45 |
{ |
| 46 |
static::addMissingIndexes($table); |
| 47 |
} |
| 48 |
|
| 49 |
public static function addMissingIndexes($table) |
| 50 |
{ |
| 51 |
global $wpdb; |
| 52 |
|
| 53 |
// $table is always $wpdb->prefix . 'fs_conversations' — not user input. |
| 54 |
// esc_sql() is the correct escaping for SQL identifiers; $wpdb->prepare() |
| 55 |
// cannot quote identifiers in WP < 6.2 (no %i placeholder available). |
| 56 |
$table = esc_sql($table); |
| 57 |
|
| 58 |
// Get existing indexes |
| 59 |
$existing_indexes = $wpdb->get_results("SHOW INDEX FROM `{$table}`"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 60 |
$existing_index_names = []; |
| 61 |
|
| 62 |
foreach ($existing_indexes as $index) { |
| 63 |
$existing_index_names[] = $index->Key_name; |
| 64 |
} |
| 65 |
|
| 66 |
// Desired indexes — keys and values are all hardcoded string literals. |
| 67 |
// idx_person_id_created_at speeds up Advanced Reports queries that filter |
| 68 |
// fs_conversations by an agent's person_id AND a date range together — |
| 69 |
// without it MySQL falls back to idx_person_id alone and scans an agent's |
| 70 |
// entire conversation history before filtering by date, which gets very |
| 71 |
// slow on large installs (proven ~100x slower on a 300k-ticket dataset). |
| 72 |
$indexes = [ |
| 73 |
'idx_ticket_id' => '`ticket_id`', |
| 74 |
'idx_person_id' => '`person_id`', |
| 75 |
'idx_created_at' => '`created_at`', |
| 76 |
'idx_person_id_created_at' => '`person_id`, `created_at`', |
| 77 |
]; |
| 78 |
|
| 79 |
// Add missing indexes. $table is esc_sql()'d above; $index_name and |
| 80 |
// $columns are hardcoded array literals — no user input reaches this query. |
| 81 |
foreach ($indexes as $index_name => $columns) { |
| 82 |
if (!in_array($index_name, $existing_index_names)) { |
| 83 |
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- all identifiers are either esc_sql()'d or hardcoded literals. |
| 84 |
$wpdb->query("ALTER TABLE `{$table}` ADD INDEX `{$index_name}` ({$columns})"); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|