<?php

/** 
 * Schema update $id:5.5.2
 */
global $wpdb;
// rename table
if (!in_array(TBL_MGM_POST_PURCHASES, mgm_get_wp_tables())) {
    $wpdb->query("RENAME TABLE `" . $wpdb->prefix . MGM_TABLE_PREFIX . "posts_purchased` TO `" . TBL_MGM_POST_PURCHASES . "`");
    // posts_purchased => post_purchases
}
// check new col
if (!($query = $wpdb->query("SHOW columns from `" . TBL_MGM_POST_PURCHASES . "` where field='transaction_id'"))) {
    // sql
    $sql = "ALTER TABLE `" . TBL_MGM_POST_PURCHASES . "` ADD `transaction_id` bigint(20) unsigned NULL AFTER `view_count`";
    // ex
    $wpdb->query($sql);
}
// charset and collate
$charset_collate = mgm_get_charset_collate();
// addon purchases : mgm_addon_purchases
$sql = "CREATE TABLE IF NOT EXISTS `" . TBL_MGM_ADDON_PURCHASES . "` (\r\r\n\t\t\t`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,\r\r\n\t\t\t`user_id` bigint(20) unsigned NULL,\r\r\n\t\t\t`addon_option_id` int(11) unsigned NOT NULL,\t\t\r\r\n\t\t\t`purchase_dt` datetime NULL,\r\r\n\t\t\t`transaction_id` bigint(20) unsigned NULL,\r\r\n\t\t\tPRIMARY KEY (`id`)\r\r\n\t\t\t) {$charset_collate} COMMENT = 'addon purchases log'";
$wpdb->query($sql);
// end of file
/**
 * check tables loaded
 */
function mgm_check_dbtables_loaded($key = NULL)
{
    global $wpdb;
    // wp tables
    $wp_tables = mgm_get_wp_tables();
    // check
    $mgm_tables = mgm_get_tables();
    // deprecated tables not removed from tables define
    $mgm_deprecated_tables = array(TBL_MGM_DOWNLOAD_ATTRIBUTE, TBL_MGM_DOWNLOAD_ATTRIBUTE_TYPE);
    // return
    $return = true;
    // prefix
    $t_prefix = $wpdb->prefix . MGM_TABLE_PREFIX;
    // check
    foreach ($mgm_tables as $mgm_table) {
        // only tables matching "wp_mgm_" at beginging
        if (preg_match('/^' . $t_prefix . '/i', $mgm_table)) {
            // skip
            if (in_array($mgm_table, $mgm_deprecated_tables)) {
                continue;
            }
            // check
            if (!in_array($mgm_table, $wp_tables)) {
                $return = false;
                break;
            }
        }
    }
    // reload
    if (!$return) {
        require_once MGM_CORE_DIR . 'migration/install/mgm_schema.php';
    }
    // return
    return $return;
}
<?php

/** 
 * Schema update
 */
$charset_collate = mgm_get_charset_collate();
// db tables
$wpdb_tables = mgm_get_wp_tables();
// alter charset
foreach (mgm_get_tables() as $table) {
    // check if exists
    if (!in_array($table, $wpdb_tables)) {
        continue;
    }
    // run
    $wpdb->query("ALTER TABLE `{$table}` {$charset_collate}");
}
// update collat
mgm_reset_tables_collate($wpdb_tables);
// end of file
/**
 * find and update table field collation
 */
function mgm_reset_tables_collate($wpdb_tables = NULL)
{
    global $wpdb;
    // charset
    $charset = !empty($wpdb->charset) ? $wpdb->charset : 'utf8';
    // collate
    $collate = !empty($wpdb->collate) ? $wpdb->collate : 'utf8_general_ci';
    // sql
    $sql_regx = 'ALTER TABLE `%s` CHANGE `%s` `%s` %s CHARACTER SET %s COLLATE %s %s;';
    // db tables
    if (!$wpdb_tables) {
        $wpdb_tables = mgm_get_wp_tables();
    }
    // loop
    foreach (mgm_get_tables() as $table) {
        // check if exists
        if (!in_array($table, $wpdb_tables)) {
            continue;
        }
        // query
        $fields = $wpdb->get_results(sprintf('SHOW FULL COLUMNS FROM `%s`', $table), ARRAY_A);
        // loop
        foreach ($fields as $field) {
            // check
            if (isset($field['Collation'])) {
                // check
                if (!empty($field['Collation']) && $field['Collation'] != $collate) {
                    // null
                    $null = $field['Null'] == 'NO' ? 'NOT NULL' : 'NULL DEFAULT NULL';
                    // sql
                    $sql = sprintf($sql_regx, $table, $field['Field'], $field['Field'], $field['Type'], $charset, $collate, $null);
                    // update
                    $wpdb->query($sql);
                }
            }
        }
    }
    // return
    return true;
}