Example #1
0
/**
* espresso_check_data_tables
* 
* ensures that the database has been updated to the current version
* and also ensures that all necessary data migration scripts have been applied
* in order to bring the content of the database up to snuff as well
* 
* @since 3.1.28
* @return void
*/
function espresso_check_data_tables()
{
    // check if db has been updated, cuz autoupdates don't trigger database install script
    $espresso_db_update = get_option('espresso_db_update');
    if (!is_array($espresso_db_update)) {
        $espresso_db_update = array($espresso_db_update);
        update_option('espresso_db_update', $espresso_db_update);
    }
    // if current EE version is NOT in list of db updates, then update the db
    if (!in_array(EVENT_ESPRESSO_VERSION, $espresso_db_update)) {
        events_data_tables_install();
    }
    // grab list of any existing data migrations from db
    if (!($existing_data_migrations = get_option('espresso_data_migrations'))) {
        // or initialize as an empty array
        $existing_data_migrations = array();
        // and set WP option
        add_option('espresso_data_migrations', array(), '', 'no');
    }
    // check separately if data migration from pre 3.1.28 versions to 3.1.28 has already been performed
    // because this data migration didn't use the new system for tracking migrations
    if ($attendee_cost_table_fix_3_1_28 = get_option('espresso_data_migrated')) {
        // if this option already exists, let's add it to the new array for tracking all migrations
        $existing_data_migrations[$attendee_cost_table_fix_3_1_28][] = 'espresso_copy_data_from_attendee_cost_table';
        // the delete the old tracking method
        delete_option('espresso_data_migrated');
    }
    // array of all previous data migrations to date
    // using the name of the callback function for the value
    $espresso_data_migrations = array('espresso_copy_data_from_attendee_cost_table', 'espresso_ensure_is_primary_is_set');
    // temp array to track scripts we need to run
    $scripts_to_run = array();
    // if we don't need them, don't load them
    $load_data_migration_scripts = FALSE;
    if (!empty($existing_data_migrations)) {
        // loop through all previous migrations
        foreach ($existing_data_migrations as $ver => $migrations) {
            $migrations = is_array($migrations) ? $migrations : array($migrations);
            foreach ($migrations as $migration_func) {
                // make sure they have been executed
                if (!in_array($migration_func, $espresso_data_migrations)) {
                    // ok NOW load the scripts
                    $load_data_migration_scripts = TRUE;
                    $scripts_to_run[$migration_func] = $migration_func;
                }
            }
        }
    } else {
        $load_data_migration_scripts = TRUE;
        $scripts_to_run = $espresso_data_migrations;
    }
    if ($load_data_migration_scripts) {
        require_once 'includes/functions/data_migration_scripts.php';
        // run the appropriate migration script
        foreach ($scripts_to_run as $migration_func) {
            call_user_func($migration_func);
        }
    }
}
/**
* espresso_check_data_tables
* 
* ensures that the database has been updated to the current version
* and also ensures that all necessary data migration scripts have been applied
* in order to bring the content of the database up to snuff as well
* 
* @since 3.1.28
* @return void
*/
function espresso_check_data_tables()
{
    // check if db has been updated, cuz autoupdates don't trigger database install script
    $espresso_db_update = get_option('espresso_db_update');
    // chech that option is an array
    if (!is_array($espresso_db_update)) {
        // if option is FALSE, then it never existed
        if ($espresso_db_update === FALSE) {
            // create coption with autoload OFF
            add_option('espresso_db_update', array(), '', 'no');
        } else {
            // option is NOT FALSE but also is NOT an array, so make it an array and save it
            $espresso_db_update = array($espresso_db_update);
            update_option('espresso_db_update', $espresso_db_update);
        }
    }
    // if current EE version is NOT in list of db updates, then update the db
    if (!in_array(EVENT_ESPRESSO_VERSION, $espresso_db_update)) {
        events_data_tables_install();
    }
    // grab list of any existing data migrations from db
    if (!($existing_data_migrations = get_option('espresso_data_migrations'))) {
        // or initialize as an empty array
        $existing_data_migrations = array();
        // and set WP option
        add_option('espresso_data_migrations', array(), '', 'no');
    }
    // assume everything is good
    $existing_migrations_error = FALSE;
    // check separately if data migration from pre 3.1.28 versions to 3.1.28 has already been performed
    // because this data migration didn't use the new system for tracking migrations
    if ($attendee_cost_table_fix_3_1_28 = get_option('espresso_data_migrated')) {
        // if this option already exists, let's add it to the new array for tracking all migrations
        $existing_data_migrations[$attendee_cost_table_fix_3_1_28][] = 'espresso_copy_data_from_attendee_cost_table';
        // the delete the old tracking method
        delete_option('espresso_data_migrated');
    }
    // array of all previous data migrations to date
    // using the name of the callback function for the value
    $espresso_data_migrations = array('espresso_copy_data_from_attendee_cost_table', 'espresso_ensure_is_primary_is_set');
    // temp array to track scripts we need to run
    $scripts_to_run = array();
    // for tracking script errors
    $previous_script = '';
    // if we don't need them, don't load them
    $load_data_migration_scripts = FALSE;
    // have we already performed some data migrations ?
    if (!empty($existing_data_migrations)) {
        // loop through all previous migrations
        foreach ($existing_data_migrations as $ver => $migrations) {
            // ensure that migrations is an array, then loop thru it
            $migrations = is_array($migrations) ? $migrations : array($migrations);
            foreach ($migrations as $key => $value) {
                // check format of $key
                if (is_numeric($key)) {
                    // $existing_migrations array might be corrupted
                    $existing_migrations_error = TRUE;
                    unset($existing_data_migrations[$ver][$key]);
                    $existing_data_migrations[$ver][$value] = array();
                    // track script
                    if ($value != $previous_script) {
                        $previous_script = $value;
                        // numeric key means that it is NOT the callback function
                        // therefore the callback function is the value
                        $migration_func = $value;
                        $errors_array = array();
                    }
                } else {
                    // callback function is the key
                    $migration_func = $key;
                    $errors_array = $value;
                }
                // make sure they have been executed
                if (!in_array($migration_func, $espresso_data_migrations)) {
                    // ok NOW load the scripts
                    $load_data_migration_scripts = TRUE;
                    $scripts_to_run[$migration_func] = $migration_func;
                }
            }
        }
    } else {
        $load_data_migration_scripts = TRUE;
        $scripts_to_run = $espresso_data_migrations;
    }
    // Houston... we might have a problem ?!?!
    if ($existing_migrations_error) {
        delete_option('espresso_data_migrated');
        add_option('espresso_data_migrations', $existing_data_migrations, '', 'no');
    }
    if ($load_data_migration_scripts && !empty($scripts_to_run)) {
        require_once 'includes/functions/data_migration_scripts.php';
        // run the appropriate migration script
        foreach ($scripts_to_run as $migration_func) {
            if (function_exists($migration_func)) {
                call_user_func($migration_func);
            }
        }
    }
}