protected function _migrate_old_row($old_row) { $term_id = EE_Data_Migration_Manager::instance()->get_mapping_new_pk('Core_4_1_0', $this->_old_table, $old_row['id'], $this->_new_table); if (!$term_id) { $this->add_error(sprintf(__("Could not migrate metadata for old category '%s' with id '%d' because there is no EE4 category (term) for it.", "event_espresso"), $old_row['category_name'], $old_row['id'])); return false; } $metadata = maybe_unserialize($old_row['category_meta']); if (!is_array($metadata)) { return; } if (isset($metadata['use_pickers'])) { $this->_add_extra_meta_to_term($term_id, 'use_color_picker', $metadata['use_pickers'] == 'Y'); } if (isset($metadata['event_background'])) { $this->_add_extra_meta_to_term($term_id, 'background_color', $metadata['event_background']); } if (isset($metadata['event_text_color'])) { $this->_add_extra_meta_to_term($term_id, 'text_color', $metadata['event_text_color']); } }
/** * Does the traditional work of setting up the plugin's database and adding default data. * If migration script/process did not exist, this is what would happen on every activation/reactivation/upgrade. * NOTE: if we're in maintenance mode (which would be the case if we detect there are data * migration scripts that need to be run and a version change happens), enqueues core for database initialization, * so that it will be done when migrations are finished * * @param boolean $initialize_addons_too if true, we double-check addons' database tables etc too; * @param boolean $verify_schema if true will re-check the database tables have the correct schema. * This is a resource-intensive job * so we prefer to only do it when necessary * @return void */ public function initialize_db_if_no_migrations_required($initialize_addons_too = FALSE, $verify_schema = true) { $request_type = $this->detect_req_type(); //only initialize system if we're not in maintenance mode. if (EE_Maintenance_Mode::instance()->level() != EE_Maintenance_Mode::level_2_complete_maintenance) { update_option('ee_flush_rewrite_rules', TRUE); if ($verify_schema) { EEH_Activation::initialize_db_and_folders(); } EEH_Activation::initialize_db_content(); EEH_Activation::system_initialization(); if ($initialize_addons_too) { $this->initialize_addons(); } } else { EE_Data_Migration_Manager::instance()->enqueue_db_initialization_for('Core'); } if ($request_type == EE_System::req_type_new_activation || $request_type == EE_System::req_type_reactivation || $request_type == EE_System::req_type_upgrade) { add_action('AHEE__EE_System__load_CPTs_and_session__start', array($this, 'redirect_to_about_ee'), 9); } }
/** * create_database_tables * * @access public * @static * @throws EE_Error * @return boolean success (whether database is setup properly or not) */ public static function create_database_tables() { EE_Registry::instance()->load_core('Data_Migration_Manager'); //find the migration script that sets the database to be compatible with the code $dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms(); if ($dms_name) { $current_data_migration_script = EE_Registry::instance()->load_dms($dms_name); $current_data_migration_script->set_migrating(false); $current_data_migration_script->schema_changes_before_migration(); $current_data_migration_script->schema_changes_after_migration(); if ($current_data_migration_script->get_errors()) { if (WP_DEBUG) { foreach ($current_data_migration_script->get_errors() as $error) { EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__); } } else { EE_Error::add_error(__('There were errors creating the Event Espresso database tables and Event Espresso has been deactivated. To view the errors, please enable WP_DEBUG in your wp-config.php file.', 'event_espresso')); } return false; } EE_Data_Migration_Manager::instance()->update_current_database_state_to(); } else { EE_Error::add_error(__('Could not determine most up-to-date data migration script from which to pull database schema structure. So database is probably not setup properly', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); return false; } return true; }
/** * loads data_migration_scripts * * @access public * @param string $class_name - class name for the DMS ie: EE_DMS_Core_4_2_0 * @param mixed $arguments * @return EE_Data_Migration_Script_Base */ public function load_dms($class_name, $arguments = array()) { // retrieve instantiated class return $this->_load(EE_Data_Migration_Manager::instance()->get_data_migration_script_folders(), 'EE_DMS_', $class_name, 'dms', $arguments, FALSE, FALSE, FALSE); }
public function _confirm_migration_crash_report_sent() { try { $most_recent_migration = EE_Data_Migration_Manager::instance()->get_last_ran_script(true); } catch (EE_Error $e) { EE_Data_Migration_Manager::instance()->add_error_to_migrations_ran($e->getMessage()); //now, just so we can display the page correctly, make a error migration script stage object //and also put the error on it. It only persists for the duration of this request $most_recent_migration = new EE_DMS_Unknown_1_0_0(); $most_recent_migration->add_error($e->getMessage()); } $success = $this->_req_data['success'] == '1' ? true : false; $this->_template_args['success'] = $success; $this->_template_args['most_recent_migration'] = $most_recent_migration; $this->_template_args['reset_db_action_url'] = EE_Admin_Page::add_query_args_and_nonce(array('action' => 'reset_db'), EE_MAINTENANCE_ADMIN_URL); $this->_template_args['reset_db_page_url'] = EE_Admin_Page::add_query_args_and_nonce(array('action' => 'data_reset'), EE_MAINTENANCE_ADMIN_URL); $this->_template_args['reattempt_action_url'] = EE_Admin_Page::add_query_args_and_nonce(array('action' => 'reattempt_migration'), EE_MAINTENANCE_ADMIN_URL); $this->_template_path = EE_MAINTENANCE_TEMPLATE_PATH . 'ee_confirm_migration_crash_report_sent.template.php'; $this->_template_args['admin_page_content'] = EEH_Template::display_template($this->_template_path, $this->_template_args, TRUE); $this->display_admin_page_with_sidebar(); }
/** * Determines if we need to put EE in maintenance mode because the database needs updating * @return boolean true if DB is old and maintenance mode was triggered; false otherwise */ public function set_maintenance_mode_if_db_old() { EE_Registry::instance()->load_core('Data_Migration_Manager'); if (EE_Data_Migration_Manager::instance()->check_for_applicable_data_migration_scripts()) { update_option(self::option_name_maintenance_mode, self::level_2_complete_maintenance); return true; } elseif ($this->level() == self::level_2_complete_maintenance) { //we also want to handle the opposite: if the site is mm2, but there aren't any migrations to run //then we shouldn't be in mm2. (Maybe an addon got deactivated?) update_option(self::option_name_maintenance_mode, self::level_0_not_in_maintenance); return false; } else { return false; } }
/** * Returns the version that this script migrates to, based on the script's name. * Cannot be overwritten because lots of code needs to know which version a script * migrates to knowing only its name. * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin * that will be updated to. Eg array('Core','4.1.0') */ public final function migrates_to_version() { return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); }
/** * Used to setup this addon's database tables, but not necessarily any default * data in them. The default is to actually use the most up-to-date data migration script * for this addon, and just use its schema_changes_before_migration() and schema_changes_after_migration() * methods to setup the db. */ public function initialize_db() { //find the migration script that sets the database to be compatible with the code $current_dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms($this->name()); if ($current_dms_name) { $current_data_migration_script = EE_Registry::instance()->load_dms($current_dms_name); $current_data_migration_script->set_migrating(FALSE); $current_data_migration_script->schema_changes_before_migration(); $current_data_migration_script->schema_changes_after_migration(); if ($current_data_migration_script->get_errors()) { foreach ($current_data_migration_script->get_errors() as $error) { EE_Error::add_error($error, __FILE__, __FUNCTION__, __LINE__); } } } //if not DMS was found that should be ok. This addon just doesn't require any database changes EE_Data_Migration_Manager::instance()->update_current_database_state_to(array('slug' => $this->name(), 'version' => $this->version())); }
/** * Gets an array where keys are ee versions, and their values are arrays indicating all the different times that version was installed * @return EE_Data_Migration_Script_Base[] */ function get_ee_migration_history() { $options = EE_Data_Migration_Manager::instance()->get_all_migration_script_options(); $presentable_migration_scripts = array(); foreach ($options as $option_array) { $presentable_migration_scripts[str_replace(EE_Data_Migration_Manager::data_migration_script_option_prefix, "", $option_array['option_name'])] = maybe_unserialize($option_array['option_value']); } return $presentable_migration_scripts; // return get_option(EE_Data_Migration_Manager::data_migrations_option_name);//EE_Data_Migration_Manager::instance()->get_data_migrations_ran(); }
/** * Checks that even though the addon was upgraded, because it happened during * maintenance mode, we couldn't do any of its setup logic. (SO it should be run * later, when the site is taken out of MM by the migration manager) * @group 6910 */ public function test_detect_actiavtions_or_upgrade__upgrade_during_maintenance_mode() { global $wp_actions; //pretend an older version of this addon was activated a while ago $addon_activation_history = array('0.9.0.dev.000' => array(date('Y-m-d H:i:s', current_time('timestamp') - DAY_IN_SECONDS * 10))); update_option($this->_addon->get_activation_history_option_name(), $addon_activation_history); //and it also shouldn't be in the current db state $current_db_state = get_option(EE_Data_Migration_Manager::current_database_state); //just for assurance, make sure New Addon is the only existing addon unset($current_db_state[$this->_addon_name]); update_option(EE_Data_Migration_Manager::current_database_state, $current_db_state); $times_reactivation_hook_fired_before = isset($wp_actions["AHEE__{$this->_addon_classname}__upgrade"]) ? $wp_actions["AHEE__{$this->_addon_classname}__upgrade"] : 0; //lastly, and imporatntly SET MAINTENANCE MODE LEVEL 2 $this->_add_mock_dms(); //now check for activations/upgrades in addons EE_System::reset(); $this->assertEquals(EE_Maintenance_Mode::level_2_complete_maintenance, EE_Maintenance_Mode::instance()->level()); $this->assertEquals(EE_System::req_type_upgrade, $this->_addon->detect_req_type()); $this->assertEquals($times_reactivation_hook_fired_before + 1, isset($wp_actions["AHEE__{$this->_addon_classname}__upgrade"]) ? $wp_actions["AHEE__{$this->_addon_classname}__upgrade"] : 0); $this->assertArrayContains('New_Addon', EE_Data_Migration_Manager::instance()->get_db_initialization_queue()); $addon_activation_history = $this->_addon->get_activation_history(); $this->assertArrayHasKey('0.9.0.dev.000', $addon_activation_history); $this->assertArrayHasKey('1.0.0.dev.000', $addon_activation_history); $this->assertTableDoesNotExist('esp_new_addon_thing'); //ok, now let's pretend the site was teaken out of MM because migrations were finished $this->_remove_mock_dms(); EE_Maintenance_Mode::reset()->set_maintenance_level(EE_Maintenance_Mode::level_0_not_in_maintenance); EE_Data_Migration_Manager::instance()->initialize_db_for_enqueued_ee_plugins(); //now we also want to check that the addon will have created the necessary table //that it needed upon new activation $this->assertEquals(array(), EE_Data_Migration_Manager::instance()->check_for_applicable_data_migration_scripts()); $this->assertEquals(EE_Maintenance_Mode::level_0_not_in_maintenance, EE_Maintenance_Mode::instance()->real_level()); $this->assertTableExists('esp_new_addon_thing'); //check for activations/upgrades again. It should be a normal request EE_System::reset(); $this->assertEquals(EE_System::req_type_normal, $this->_addon->detect_req_type()); $this->assertEquals($times_reactivation_hook_fired_before + 1, $wp_actions["AHEE__{$this->_addon_classname}__upgrade"]); $this->assertWPOptionDoesNotExist($this->_addon->get_activation_indicator_option_name()); }
/** * @group 8328 */ public function test_database_needs_updating_to__current_version_is_lower() { EE_Data_Migration_Manager::instance()->update_current_database_state_to(array('slug' => 'Core', 'version' => '4.9.0')); $this->assertFalse(EE_Data_Migration_Manager::instance()->database_needs_updating_to(array('slug' => 'Core', 'version' => '4.4.0.p'))); }
/** * create_database_tables * * @access public * @static * @return void */ public static function create_database_tables() { //find the migration script that sets the database to be compatible with the code $dms_name = EE_Data_Migration_Manager::instance()->get_most_up_to_date_dms(); if ($dms_name) { $current_data_migration_script = EE_Registry::instance()->load_dms($dms_name); $current_data_migration_script->schema_changes_before_migration(); $current_data_migration_script->schema_changes_after_migration(); EE_Data_Migration_Manager::instance()->update_current_database_state_to(); } else { throw new EE_Error(sprintf(__('Could not determine most up-to-date data migration script from which to pull database schema structure. So database is probably not setup properly', 'event_espresso'))); } }