PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.3
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 / MailBoxMigrator.php

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

83 lines 2.4 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 class MailBoxMigrator
6 {
7 static $tableName = 'fs_mail_boxes';
8
9 public static function migrate()
10 {
11 global $wpdb;
12
13 $charsetCollate = $wpdb->get_charset_collate();
14
15 $table = $wpdb->prefix . static::$tableName;
16
17 if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) {
18 $sql = "CREATE TABLE $table (
19 `id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
20 `name` VARCHAR(192) NOT NULL,
21 `slug` VARCHAR(192) NOT NULL,
22 `box_type` VARCHAR(50) default 'web',
23 `email` VARCHAR(192) NOT NULL,
24 `mapped_email` VARCHAR(192) NULL,
25 `email_footer` LONGTEXT NULL,
26 `settings` LONGTEXT NULL,
27 `avatar` VARCHAR(192) NULL,
28 `created_by` BIGINT(20) UNSIGNED NULL,
29 `is_default` ENUM('yes', 'no') DEFAULT 'no',
30 `created_at` TIMESTAMP NULL,
31 `updated_at` TIMESTAMP NULL,
32 INDEX `idx_slug` (`slug`),
33 INDEX `idx_email` (`email`),
34 INDEX `idx_created_by` (`created_by`),
35 INDEX `idx_box_type` (`box_type`)
36 ) $charsetCollate;";
37 $created = dbDelta($sql);
38 return $created;
39 } else {
40 static::alterTable($table);
41 }
42
43 return false;
44 }
45
46 public static function alterTable($table)
47 {
48 static::addMissingIndexes($table);
49 }
50
51 public static function addMissingIndexes($table)
52 {
53 global $wpdb;
54
55 // Escape table name
56 $table = esc_sql($table);
57
58 // Get existing indexes
59 $existing_indexes = $wpdb->get_results("SHOW INDEX FROM `$table`");
60 $existing_index_names = [];
61
62 foreach ($existing_indexes as $index) {
63 $existing_index_names[] = $index->Key_name;
64 }
65
66 // Desired indexes
67 $indexes = [
68 'idx_slug' => 'slug',
69 'idx_email' => 'email',
70 'idx_created_by' => 'created_by',
71 'idx_box_type' => 'box_type',
72 ];
73
74 // Add missing indexes
75 foreach ($indexes as $index_name => $column_name) {
76 if (!in_array($index_name, $existing_index_names)) {
77 $sql = "ALTER TABLE `$table` ADD INDEX `$index_name` (`$column_name`)";
78 $wpdb->query($sql);
79 }
80 }
81 }
82 }
83