| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Database\Migrations; |
| 4 |
|
| 5 |
class PersonsMigrator |
| 6 |
{ |
| 7 |
static $tableName = 'fs_persons'; |
| 8 |
|
| 9 |
public static function migrate() |
| 10 |
{ |
| 11 |
global $wpdb; |
| 12 |
$charsetCollate = $wpdb->get_charset_collate(); |
| 13 |
|
| 14 |
$table = $wpdb->prefix . static::$tableName; |
| 15 |
|
| 16 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { |
| 17 |
$sql = "CREATE TABLE $table ( |
| 18 |
`id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 19 |
`first_name` VARCHAR(192) NULL, |
| 20 |
`last_name` VARCHAR(192) NULL, |
| 21 |
`email` VARCHAR(192) NULL, |
| 22 |
`title` VARCHAR(192) NULL, |
| 23 |
`avatar` VARCHAR(192) NULL, |
| 24 |
`person_type` VARCHAR(192) DEFAULT 'customer', |
| 25 |
`status` VARCHAR(192) DEFAULT 'active', |
| 26 |
`ip_address` VARCHAR(20) NULL, |
| 27 |
`last_ip_address` VARCHAR(20) NULL, |
| 28 |
`address_line_1` VARCHAR(192) NULL, |
| 29 |
`address_line_2` VARCHAR(192) NULL, |
| 30 |
`city` VARCHAR(192) NULL, |
| 31 |
`zip` VARCHAR(192) NULL, |
| 32 |
`state` VARCHAR(192) NULL, |
| 33 |
`country` VARCHAR(192) NULL, |
| 34 |
`note` LONGTEXT NULL, |
| 35 |
`hash` VARCHAR(192) NULL, |
| 36 |
`user_id` BIGINT(20) UNSIGNED NULL, |
| 37 |
`description` MEDIUMTEXT NULL, |
| 38 |
`remote_uid` BIGINT(20) UNSIGNED NULL, |
| 39 |
`last_response_at` TIMESTAMP NULL, |
| 40 |
`created_at` TIMESTAMP NULL, |
| 41 |
`updated_at` TIMESTAMP NULL, |
| 42 |
INDEX `idx_email` (`email`), |
| 43 |
INDEX `idx_user_id` (`user_id`), |
| 44 |
INDEX `idx_ip_address` (`ip_address`) |
| 45 |
) $charsetCollate;"; |
| 46 |
$created = dbDelta($sql); |
| 47 |
return $created; |
| 48 |
} else { |
| 49 |
static::alterTable($table); |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
public static function alterTable($table) |
| 56 |
{ |
| 57 |
static::addMissingColumns($table); |
| 58 |
static::addMissingIndexes($table); |
| 59 |
} |
| 60 |
|
| 61 |
public static function addMissingColumns($table) |
| 62 |
{ |
| 63 |
global $wpdb; |
| 64 |
|
| 65 |
// Escape table name |
| 66 |
$table = esc_sql($table); |
| 67 |
|
| 68 |
// Get existing columns |
| 69 |
$existing_columns = $wpdb->get_col("DESC `$table`", 0); |
| 70 |
|
| 71 |
// @todo: We will remove this on final release |
| 72 |
// This is only for beta users |
| 73 |
if (!in_array('title', $existing_columns)) { |
| 74 |
$query = 'ALTER TABLE `' . $table . '` ADD `title` VARCHAR(192) NULL AFTER `email`'; |
| 75 |
$wpdb->query($query); |
| 76 |
} |
| 77 |
|
| 78 |
if (!in_array('description', $existing_columns)) { |
| 79 |
$query = 'ALTER TABLE `' . $table . '` ADD `description` MEDIUMTEXT NULL AFTER `user_id`'; |
| 80 |
$wpdb->query($query); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static function addMissingIndexes($table) |
| 85 |
{ |
| 86 |
global $wpdb; |
| 87 |
|
| 88 |
// Escape table name |
| 89 |
$table = esc_sql($table); |
| 90 |
|
| 91 |
// Get existing indexes |
| 92 |
$existing_indexes = $wpdb->get_results("SHOW INDEX FROM `$table`"); |
| 93 |
$existing_index_names = []; |
| 94 |
|
| 95 |
foreach ($existing_indexes as $index) { |
| 96 |
$existing_index_names[] = $index->Key_name; |
| 97 |
} |
| 98 |
|
| 99 |
// Desired indexes |
| 100 |
$indexes = [ |
| 101 |
'idx_email' => 'email', |
| 102 |
'idx_user_id' => 'user_id', |
| 103 |
'idx_ip_address' => 'ip_address', |
| 104 |
]; |
| 105 |
|
| 106 |
// Add missing indexes |
| 107 |
foreach ($indexes as $index_name => $column_name) { |
| 108 |
if (!in_array($index_name, $existing_index_names)) { |
| 109 |
$sql = "ALTER TABLE `$table` ADD INDEX `$index_name` (`$column_name`)"; |
| 110 |
$wpdb->query($sql); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|