| 1 |
<?php |
| 2 |
// phpcs:disable |
| 3 |
namespace FluentCommunity\Database\Migrations; |
| 4 |
|
| 5 |
class FeedReactionsMigrator |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @param bool $isForced |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
public static function migrate($isForced = false) |
| 12 |
{ |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$charsetCollate = $wpdb->get_charset_collate(); |
| 16 |
$table = $wpdb->prefix . 'fcom_post_reactions'; |
| 17 |
|
| 18 |
if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table || $isForced) { |
| 19 |
$sql = "CREATE TABLE $table ( |
| 20 |
`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 21 |
`user_id` BIGINT UNSIGNED NULL, |
| 22 |
`object_id` BIGINT UNSIGNED NULL, |
| 23 |
`parent_id` BIGINT UNSIGNED NULL, |
| 24 |
`object_type` VARCHAR(100) default 'feed', |
| 25 |
`type` VARCHAR(100) NULL DEFAULT 'like', |
| 26 |
`ip_address` VARCHAR(100) NULL, |
| 27 |
`created_at` TIMESTAMP NULL, |
| 28 |
`updated_at` TIMESTAMP NULL, |
| 29 |
INDEX object_user_object_type_type (object_id, user_id, object_type, type), |
| 30 |
INDEX object_type_parent_id_user_id (object_type, parent_id, user_id) |
| 31 |
) $charsetCollate;"; |
| 32 |
dbDelta($sql); |
| 33 |
} else { |
| 34 |
$indexes = $wpdb->get_results("SHOW INDEX FROM $table"); |
| 35 |
|
| 36 |
$hasObjectUserObjectTypeType = false; |
| 37 |
$hasObjectTypeParentIdUserId = false; |
| 38 |
|
| 39 |
foreach ($indexes as $index) { |
| 40 |
if ($index->Key_name === 'object_user_object_type_type') { |
| 41 |
$hasObjectUserObjectTypeType = true; |
| 42 |
} |
| 43 |
if ($index->Key_name === 'object_type_parent_id_user_id') { |
| 44 |
$hasObjectTypeParentIdUserId = true; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if (!$hasObjectUserObjectTypeType) { |
| 49 |
$wpdb->query("ALTER TABLE $table ADD INDEX object_user_object_type_type (object_id, user_id, object_type, type)"); |
| 50 |
} |
| 51 |
|
| 52 |
if (!$hasObjectTypeParentIdUserId) { |
| 53 |
$wpdb->query("ALTER TABLE $table ADD INDEX object_type_parent_id_user_id (object_type, parent_id, user_id)"); |
| 54 |
} |
| 55 |
|
| 56 |
$removeIndexes = ['object_id', 'object_type', 'type']; |
| 57 |
|
| 58 |
foreach ($indexes as $index) { |
| 59 |
if (in_array($index->Key_name, $removeIndexes)) { |
| 60 |
$wpdb->query("ALTER TABLE $table DROP INDEX $index->Key_name"); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|