PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.2
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.2
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / database / Migrations / AIActivityLogsMigrator.php

AIActivityLogsMigrator.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.2, at database/Migrations/AIActivityLogsMigrator.php

79 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Database\Migrations;
4
5 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
6 class AIActivityLogsMigrator
7 {
8 static $tableName = 'fs_ai_activity_logs';
9
10 public static function migrate()
11 {
12 global $wpdb;
13
14 $charsetCollate = $wpdb->get_charset_collate();
15
16 $table = $wpdb->prefix . static::$tableName;
17
18 if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
19 $sql = "CREATE TABLE $table (
20 `id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
21 `agent_id` BIGINT(20) NULL,
22 `ticket_id` BIGINT(20) NULL,
23 `model_name` VARCHAR(50) NULL,
24 `tokens` MEDIUMTEXT NULL,
25 `prompt` LONGTEXT NULL,
26 `created_at` TIMESTAMP NULL,
27 `updated_at` TIMESTAMP NULL,
28 INDEX `idx_agent_id` (`agent_id`),
29 INDEX `idx_ticket_id` (`ticket_id`),
30 INDEX `idx_model_name` (`model_name`),
31 INDEX `idx_created_at` (`created_at`)
32 ) $charsetCollate;";
33 $created = dbDelta($sql);
34 return $created;
35 } else {
36 static::alterTable($table);
37 }
38
39 return false;
40 }
41
42 public static function alterTable($table)
43 {
44 static::addMissingIndexes($table);
45 }
46
47 public static function addMissingIndexes($table)
48 {
49 global $wpdb;
50
51 // Escape table name
52 $table = esc_sql($table);
53
54 // Get existing indexes
55 $existing_indexes = $wpdb->get_results("SHOW INDEX FROM `$table`");
56 $existing_index_names = [];
57
58 foreach ($existing_indexes as $index) {
59 $existing_index_names[] = $index->Key_name;
60 }
61
62 // Desired indexes
63 $indexes = [
64 'idx_agent_id' => 'agent_id',
65 'idx_ticket_id' => 'ticket_id',
66 'idx_model_name' => 'model_name',
67 'idx_created_at' => 'created_at',
68 ];
69
70 // Add missing indexes
71 foreach ($indexes as $index_name => $column_name) {
72 if (!in_array($index_name, $existing_index_names)) {
73 $sql = "ALTER TABLE `$table` ADD INDEX `$index_name` (`$column_name`)";
74 $wpdb->query($sql);
75 }
76 }
77 }
78 }
79