| 1 |
<?php |
| 2 |
// phpcs:disable |
| 3 |
namespace FluentCommunity\Database\Migrations; |
| 4 |
|
| 5 |
class FeedCommentsMigrator |
| 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_comments'; |
| 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 |
`post_id` BIGINT UNSIGNED NULL, |
| 23 |
`parent_id` BIGINT UNSIGNED NULL, |
| 24 |
`reactions_count` BIGINT UNSIGNED DEFAULT 0, |
| 25 |
`message` LONGTEXT NULL, |
| 26 |
`message_rendered` LONGTEXT NULL, |
| 27 |
`meta` LONGTEXT NULL, |
| 28 |
`type` VARCHAR(100) NULL DEFAULT 'comment', |
| 29 |
`content_type` VARCHAR(100) NULL DEFAULT 'text', |
| 30 |
`status` VARCHAR(100) NULL DEFAULT 'published', |
| 31 |
`is_sticky` TINYINT(1) NULL DEFAULT 0, |
| 32 |
`created_at` TIMESTAMP NULL, |
| 33 |
`updated_at` TIMESTAMP NULL, |
| 34 |
INDEX `post_id` (`post_id`), |
| 35 |
INDEX `status` (`status`), |
| 36 |
INDEX `type` (`type`), |
| 37 |
INDEX `user_id` (`user_id`) |
| 38 |
) $charsetCollate;"; |
| 39 |
dbDelta($sql); |
| 40 |
} else { |
| 41 |
// check if meta is exist or not |
| 42 |
$isMigrated = $wpdb->get_col($wpdb->prepare("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND COLUMN_NAME='meta' AND TABLE_NAME=%s", $table)); |
| 43 |
if(!$isMigrated) { |
| 44 |
$wpdb->query("ALTER TABLE {$table} ADD COLUMN `meta` LONGTEXT NULL AFTER `message_rendered`"); |
| 45 |
} |
| 46 |
|
| 47 |
// check if the user_id index exists or not |
| 48 |
$hasUserIdIndex = $wpdb->get_col($wpdb->prepare("SELECT INDEX_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=%s AND INDEX_NAME='user_id'", $table)); |
| 49 |
if (!$hasUserIdIndex) { |
| 50 |
$wpdb->query("ALTER TABLE {$table} ADD INDEX `user_id` (`user_id`)"); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|