@@ -15,880 +15,880 @@ |
||
| 15 | 15 | abstract class EE_Data_Migration_Script_Base extends EE_Data_Migration_Class_Base |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
| 20 | - * instead of being used to merely setup (or verify) the database structure. |
|
| 21 | - * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
| 22 | - * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
| 23 | - * |
|
| 24 | - * @var boolean |
|
| 25 | - */ |
|
| 26 | - protected $_migrating = true; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
| 30 | - * |
|
| 31 | - * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
| 32 | - */ |
|
| 33 | - protected $_migration_stages = array(); |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
| 37 | - * |
|
| 38 | - * @var boolean |
|
| 39 | - */ |
|
| 40 | - protected $_schema_changes_before_migration_ran = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
| 44 | - * |
|
| 45 | - * @var boolean |
|
| 46 | - */ |
|
| 47 | - protected $_schema_changes_after_migration_ran = null; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * String which describes what's currently happening in this migration |
|
| 51 | - * |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - protected $_feedback_message; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
| 58 | - * correspond to earlier execution |
|
| 59 | - * |
|
| 60 | - * @var int |
|
| 61 | - */ |
|
| 62 | - protected $_priority = 5; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
| 66 | - * to NEW table Primary Keys. |
|
| 67 | - * Top-level array keys are OLD table names (minus the "wp_" part), |
|
| 68 | - * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
| 69 | - * 3rd-level array keys are the OLD table primary keys |
|
| 70 | - * and 3rd-level array values are the NEW table primary keys |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - protected $_mappings = array(); |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Returns whether or not this data migration script can operate on the given version of the database. |
|
| 79 | - * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
| 80 | - * it's passed a string like '3.1.38B', it should return true. |
|
| 81 | - * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
| 82 | - * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
| 83 | - * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
| 84 | - * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
| 85 | - * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
| 86 | - * to run this DMS). |
|
| 87 | - * If this DMS migrates data from a previous version of this EE4 addon, just |
|
| 88 | - * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
| 89 | - * If this DMS should never migrate data, because it's only used to define the initial |
|
| 90 | - * database state, just return FALSE (and core's activation process will take care |
|
| 91 | - * of calling its schema_changes_before_migration() and |
|
| 92 | - * schema_changes_after_migration() for you. ) |
|
| 93 | - * |
|
| 94 | - * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
| 95 | - * @return boolean |
|
| 96 | - */ |
|
| 97 | - abstract public function can_migrate_from_version($current_database_state_of); |
|
| 98 | - |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
| 102 | - * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
| 103 | - * during this migration, this would probably add a new column called something like |
|
| 104 | - * "encoded_password". |
|
| 105 | - * |
|
| 106 | - * @return boolean of success |
|
| 107 | - */ |
|
| 108 | - abstract public function schema_changes_before_migration(); |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
| 113 | - * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
| 114 | - * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
| 115 | - * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
| 116 | - * and possibly rename "encoded_password" to "password" |
|
| 117 | - * |
|
| 118 | - * @return boolean of success |
|
| 119 | - */ |
|
| 120 | - abstract public function schema_changes_after_migration(); |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * All children of this must call parent::__construct() |
|
| 125 | - * at the end of their constructor or suffer the consequences! |
|
| 126 | - * |
|
| 127 | - * @param TableManager $table_manager |
|
| 128 | - * @param TableAnalysis $table_analysis |
|
| 129 | - */ |
|
| 130 | - public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
| 131 | - { |
|
| 132 | - $this->_migration_stages = (array) apply_filters( |
|
| 133 | - 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
| 134 | - $this->_migration_stages |
|
| 135 | - ); |
|
| 136 | - foreach ($this->_migration_stages as $migration_stage) { |
|
| 137 | - if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
| 138 | - $migration_stage->_construct_finalize($this); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - parent::__construct($table_manager, $table_analysis); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Place to add hooks and filters for tweaking the migrations page, in order |
|
| 147 | - * to customize it |
|
| 148 | - */ |
|
| 149 | - public function migration_page_hooks() |
|
| 150 | - { |
|
| 151 | - // by default none are added because we normally like the default look of the migration page |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Sets the mapping from old table primary keys to new table primary keys. |
|
| 157 | - * This mapping is automatically persisted as a property on the migration |
|
| 158 | - * |
|
| 159 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 160 | - * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
| 161 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 162 | - * @param int|string $new_pk eg posts.ID |
|
| 163 | - * @return void |
|
| 164 | - */ |
|
| 165 | - public function set_mapping($old_table, $old_pk, $new_table, $new_pk) |
|
| 166 | - { |
|
| 167 | - // make sure it has the needed keys |
|
| 168 | - if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 169 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 170 | - } |
|
| 171 | - $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Gets the new primary key, if provided with the OLD table and the primary key |
|
| 177 | - * of an item in the old table, and the new table |
|
| 178 | - * |
|
| 179 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 180 | - * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
| 181 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 182 | - * @return mixed the primary key on the new table |
|
| 183 | - */ |
|
| 184 | - public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
| 185 | - { |
|
| 186 | - if (! isset($this->_mappings[ $old_table ]) || |
|
| 187 | - ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 188 | - // try fetching the option |
|
| 189 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 190 | - } |
|
| 191 | - return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
| 192 | - ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Gets the old primary key, if provided with the OLD table, |
|
| 198 | - * and the new table and the primary key of an item in the new table |
|
| 199 | - * |
|
| 200 | - * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 201 | - * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 202 | - * @param mixed $new_pk |
|
| 203 | - * @return mixed |
|
| 204 | - */ |
|
| 205 | - public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
| 206 | - { |
|
| 207 | - if (! isset($this->_mappings[ $old_table ]) || |
|
| 208 | - ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 209 | - // try fetching the option |
|
| 210 | - $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 211 | - } |
|
| 212 | - if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 213 | - $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
| 214 | - if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
| 215 | - return $new_pk_to_old_pk[ $new_pk ]; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - return null; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Gets the mapping array option specified by the table names |
|
| 224 | - * |
|
| 225 | - * @param string $old_table_name |
|
| 226 | - * @param string $new_table_name |
|
| 227 | - * @return array |
|
| 228 | - */ |
|
| 229 | - protected function _get_mapping_option($old_table_name, $new_table_name) |
|
| 230 | - { |
|
| 231 | - $option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
| 232 | - return $option; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Updates the mapping option specified by the table names with the array provided |
|
| 238 | - * |
|
| 239 | - * @param string $old_table_name |
|
| 240 | - * @param string $new_table_name |
|
| 241 | - * @param array $mapping_array |
|
| 242 | - * @return boolean success of updating option |
|
| 243 | - */ |
|
| 244 | - protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
| 245 | - { |
|
| 246 | - $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
| 247 | - return $success; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
| 253 | - * |
|
| 254 | - * @param string $old_table_name |
|
| 255 | - * @param string $new_table_name |
|
| 256 | - * @return string |
|
| 257 | - */ |
|
| 258 | - protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
| 259 | - { |
|
| 260 | - global $wpdb; |
|
| 261 | - $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
| 262 | - $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
| 263 | - $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
| 264 | - return substr( |
|
| 265 | - EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp, |
|
| 266 | - 0, |
|
| 267 | - 64 |
|
| 268 | - ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Counts all the records that will be migrated during this data migration. |
|
| 274 | - * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
| 275 | - * this would be a count of all users who have passwords. If we were going to also split |
|
| 276 | - * attendee records into transactions, registrations, and attendee records, this would include |
|
| 277 | - * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
| 278 | - * If you can't determine how many records there are to migrate, just provide a guess: this |
|
| 279 | - * number will only be used in calculating the percent complete. If you estimate there to be |
|
| 280 | - * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
| 281 | - * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
| 282 | - * |
|
| 283 | - * @return int |
|
| 284 | - */ |
|
| 285 | - protected function _count_records_to_migrate() |
|
| 286 | - { |
|
| 287 | - $count = 0; |
|
| 288 | - foreach ($this->stages() as $stage) { |
|
| 289 | - $count += $stage->count_records_to_migrate(); |
|
| 290 | - } |
|
| 291 | - return $count; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Returns the number of records updated so far. Usually this is easiest to do |
|
| 297 | - * by just setting a transient and updating it after each migration_step |
|
| 298 | - * |
|
| 299 | - * @return int |
|
| 300 | - */ |
|
| 301 | - public function count_records_migrated() |
|
| 302 | - { |
|
| 303 | - $count = 0; |
|
| 304 | - foreach ($this->stages() as $stage) { |
|
| 305 | - $count += $stage->count_records_migrated(); |
|
| 306 | - } |
|
| 307 | - $this->_records_migrated = $count; |
|
| 308 | - return $count; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param int $num_records_to_migrate_limit |
|
| 314 | - * @return int |
|
| 315 | - * @throws EE_Error |
|
| 316 | - * @throws Exception |
|
| 317 | - */ |
|
| 318 | - public function migration_step($num_records_to_migrate_limit) |
|
| 319 | - { |
|
| 320 | - // reset the feedback message |
|
| 321 | - $this->_feedback_message = ''; |
|
| 322 | - // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
| 323 | - $this->_maybe_do_schema_changes(true); |
|
| 324 | - |
|
| 325 | - $num_records_actually_migrated = 0; |
|
| 326 | - $records_migrated_per_stage = array(); |
|
| 327 | - // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
| 328 | - $stage = null; |
|
| 329 | - // get the next stage that isn't complete |
|
| 330 | - foreach ($this->stages() as $stage) { |
|
| 331 | - if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
| 332 | - try { |
|
| 333 | - $records_migrated_during_stage = $stage->migration_step( |
|
| 334 | - $num_records_to_migrate_limit - $num_records_actually_migrated |
|
| 335 | - ); |
|
| 336 | - $num_records_actually_migrated += $records_migrated_during_stage; |
|
| 337 | - $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
| 338 | - } catch (Exception $e) { |
|
| 339 | - // yes if we catch an exception here, we consider that migration stage borked. |
|
| 340 | - $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 341 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 342 | - $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
| 343 | - throw $e; |
|
| 344 | - } |
|
| 345 | - // check that the migration stage didn't mark itself as having a fatal error |
|
| 346 | - if ($stage->is_broken()) { |
|
| 347 | - $this->set_broken(); |
|
| 348 | - throw new EE_Error($stage->get_last_error()); |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
| 352 | - // or if we had a fatal error |
|
| 353 | - // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
| 354 | - if ($num_records_actually_migrated >= $num_records_to_migrate_limit |
|
| 355 | - || $stage->is_broken() |
|
| 356 | - || $stage->has_more_to_do() |
|
| 357 | - ) { |
|
| 358 | - break; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - // check if we're all done this data migration... |
|
| 362 | - // which is indicated by being done early AND the last stage claims to be done |
|
| 363 | - if ($stage == null) { |
|
| 364 | - // this migration script apparently has NO stages... which is super weird, but whatever |
|
| 365 | - $this->set_completed(); |
|
| 366 | - $this->_maybe_do_schema_changes(false); |
|
| 367 | - } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
| 368 | - // apparently we're done, because we couldn't migrate the number we intended to |
|
| 369 | - $this->set_completed(); |
|
| 370 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
| 371 | - // do schema changes for after the migration now |
|
| 372 | - // first double-check we haven't already done this |
|
| 373 | - $this->_maybe_do_schema_changes(false); |
|
| 374 | - } else { |
|
| 375 | - // update feedback message, keeping in mind that we show them with the most recent at the top |
|
| 376 | - $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
| 377 | - } |
|
| 378 | - return $num_records_actually_migrated; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Updates the feedback message according to what was done during this migration stage. |
|
| 384 | - * |
|
| 385 | - * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
| 386 | - * migrated from that stage |
|
| 387 | - * @return void |
|
| 388 | - */ |
|
| 389 | - private function _update_feedback_message($records_migrated_per_stage) |
|
| 390 | - { |
|
| 391 | - $feedback_message_array = array(); |
|
| 392 | - foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
| 393 | - $feedback_message_array[] = sprintf( |
|
| 394 | - __("Migrated %d records successfully during %s", "event_espresso"), |
|
| 395 | - $num_records_migrated, |
|
| 396 | - $migration_stage_name |
|
| 397 | - ); |
|
| 398 | - } |
|
| 399 | - $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
| 405 | - * (if $before==false). Buffers their outputs and stores them on the class. |
|
| 406 | - * |
|
| 407 | - * @param boolean $before |
|
| 408 | - * @throws Exception |
|
| 409 | - * @return void |
|
| 410 | - */ |
|
| 411 | - private function _maybe_do_schema_changes($before = true) |
|
| 412 | - { |
|
| 413 | - // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
| 414 | - $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
| 415 | - if (! $this->{$property_name}) { |
|
| 416 | - try { |
|
| 417 | - ob_start(); |
|
| 418 | - if ($before) { |
|
| 419 | - $this->schema_changes_before_migration(); |
|
| 420 | - } else { |
|
| 421 | - $this->schema_changes_after_migration(); |
|
| 422 | - } |
|
| 423 | - $output = ob_get_contents(); |
|
| 424 | - ob_end_clean(); |
|
| 425 | - } catch (Exception $e) { |
|
| 426 | - $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 427 | - throw $e; |
|
| 428 | - } |
|
| 429 | - // record that we've done these schema changes |
|
| 430 | - $this->{$property_name} = true; |
|
| 431 | - // if there were any warnings etc, record them as non-fatal errors |
|
| 432 | - if ($output) { |
|
| 433 | - // there were some warnings |
|
| 434 | - $this->_errors[] = $output; |
|
| 435 | - } |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
| 442 | - * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
| 443 | - * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
| 444 | - * or |
|
| 445 | - * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
| 446 | - * not |
|
| 447 | - * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
| 448 | - * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
| 449 | - * should be nuked. |
|
| 450 | - * |
|
| 451 | - * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
| 452 | - * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
| 453 | - * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
| 454 | - * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
| 455 | - * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
| 456 | - * |
|
| 457 | - * @param string $table_name |
|
| 458 | - * @param string $table_definition_sql |
|
| 459 | - * @param string $engine_string |
|
| 460 | - */ |
|
| 461 | - protected function _table_is_new_in_this_version( |
|
| 462 | - $table_name, |
|
| 463 | - $table_definition_sql, |
|
| 464 | - $engine_string = 'ENGINE=InnoDB ' |
|
| 465 | - ) { |
|
| 466 | - $this->_create_table_and_catch_errors( |
|
| 467 | - $table_name, |
|
| 468 | - $table_definition_sql, |
|
| 469 | - $engine_string, |
|
| 470 | - $this->_pre_existing_table_should_be_dropped(true) |
|
| 471 | - ); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
| 476 | - * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
| 477 | - * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
| 478 | - * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
| 479 | - * _table_should_exist_previously. |
|
| 480 | - * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
| 481 | - * table shouldn't exist). |
|
| 482 | - * |
|
| 483 | - * @param string $table_name |
|
| 484 | - * @param string $table_definition_sql |
|
| 485 | - * @param string $engine_string |
|
| 486 | - */ |
|
| 487 | - protected function _table_is_changed_in_this_version( |
|
| 488 | - $table_name, |
|
| 489 | - $table_definition_sql, |
|
| 490 | - $engine_string = 'ENGINE=MyISAM' |
|
| 491 | - ) { |
|
| 492 | - $this->_create_table_and_catch_errors( |
|
| 493 | - $table_name, |
|
| 494 | - $table_definition_sql, |
|
| 495 | - $engine_string, |
|
| 496 | - $this->_pre_existing_table_should_be_dropped(false) |
|
| 497 | - ); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * _old_table_exists |
|
| 503 | - * returns TRUE if the requested table exists in the current database |
|
| 504 | - * |
|
| 505 | - * @param string $table_name |
|
| 506 | - * @return boolean |
|
| 507 | - */ |
|
| 508 | - protected function _old_table_exists($table_name) |
|
| 509 | - { |
|
| 510 | - return $this->_get_table_analysis()->tableExists($table_name); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * _delete_table_if_empty |
|
| 516 | - * returns TRUE if the requested table was empty and successfully empty |
|
| 517 | - * |
|
| 518 | - * @param string $table_name |
|
| 519 | - * @return boolean |
|
| 520 | - */ |
|
| 521 | - protected function _delete_table_if_empty($table_name) |
|
| 522 | - { |
|
| 523 | - return EEH_Activation::delete_db_table_if_empty($table_name); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
| 529 | - * as these are significantly more efficient or explicit. |
|
| 530 | - * Please see description of _table_is_new_in_this_version. This function will only set |
|
| 531 | - * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
| 532 | - * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
| 533 | - * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
| 534 | - * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
| 535 | - * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
| 536 | - * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
| 537 | - * because apparently it hasn't changed since the previous one, right? |
|
| 538 | - * |
|
| 539 | - * @param string $table_name |
|
| 540 | - * @param string $table_definition_sql |
|
| 541 | - * @param string $engine_string |
|
| 542 | - */ |
|
| 543 | - protected function _table_should_exist_previously( |
|
| 544 | - $table_name, |
|
| 545 | - $table_definition_sql, |
|
| 546 | - $engine_string = 'ENGINE=MyISAM' |
|
| 547 | - ) { |
|
| 548 | - $this->_create_table_and_catch_errors( |
|
| 549 | - $table_name, |
|
| 550 | - $table_definition_sql, |
|
| 551 | - $engine_string, |
|
| 552 | - $this->_pre_existing_table_should_be_dropped(false) |
|
| 553 | - ); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
| 558 | - * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
| 559 | - * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
| 560 | - * but important for multisite where migrations can take a very long time otherwise). |
|
| 561 | - * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
| 562 | - * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
| 563 | - * shouldn't exist). |
|
| 564 | - * |
|
| 565 | - * @param string $table_name |
|
| 566 | - * @param string $table_definition_sql |
|
| 567 | - * @param string $engine_string |
|
| 568 | - */ |
|
| 569 | - protected function _table_has_not_changed_since_previous( |
|
| 570 | - $table_name, |
|
| 571 | - $table_definition_sql, |
|
| 572 | - $engine_string = 'ENGINE=MyISAM' |
|
| 573 | - ) { |
|
| 574 | - if ($this->_currently_migrating()) { |
|
| 575 | - // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
| 576 | - return; |
|
| 577 | - } |
|
| 578 | - $this->_create_table_and_catch_errors( |
|
| 579 | - $table_name, |
|
| 580 | - $table_definition_sql, |
|
| 581 | - $engine_string, |
|
| 582 | - $this->_pre_existing_table_should_be_dropped(false) |
|
| 583 | - ); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Returns whether or not this migration script is being used as part of an actual migration |
|
| 588 | - * |
|
| 589 | - * @return boolean |
|
| 590 | - */ |
|
| 591 | - protected function _currently_migrating() |
|
| 592 | - { |
|
| 593 | - // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM) |
|
| 594 | - return $this->_migrating && |
|
| 595 | - $this->can_migrate_from_version( |
|
| 596 | - EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
| 597 | - ) && |
|
| 598 | - EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - /** |
|
| 602 | - * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
| 603 | - * and the plugin's request type. |
|
| 604 | - * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
| 605 | - * shouldn't exist no matter what). |
|
| 606 | - * |
|
| 607 | - * @param boolean $table_is_new |
|
| 608 | - * @return boolean |
|
| 609 | - */ |
|
| 610 | - protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
| 611 | - { |
|
| 612 | - if ($table_is_new) { |
|
| 613 | - if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
| 614 | - || $this->_currently_migrating() |
|
| 615 | - ) { |
|
| 616 | - return true; |
|
| 617 | - } else { |
|
| 618 | - return false; |
|
| 619 | - } |
|
| 620 | - } else { |
|
| 621 | - if (in_array( |
|
| 622 | - $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
| 623 | - array(EE_System::req_type_new_activation) |
|
| 624 | - )) { |
|
| 625 | - return true; |
|
| 626 | - } else { |
|
| 627 | - return false; |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
| 634 | - * |
|
| 635 | - * @param string $table_name |
|
| 636 | - * @param string $table_definition_sql |
|
| 637 | - * @param string $engine_string |
|
| 638 | - * @param boolean $drop_pre_existing_tables |
|
| 639 | - */ |
|
| 640 | - private function _create_table_and_catch_errors( |
|
| 641 | - $table_name, |
|
| 642 | - $table_definition_sql, |
|
| 643 | - $engine_string = 'ENGINE=MyISAM', |
|
| 644 | - $drop_pre_existing_tables = false |
|
| 645 | - ) { |
|
| 646 | - try { |
|
| 647 | - EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
| 648 | - } catch (EE_Error $e) { |
|
| 649 | - $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
| 650 | - $this->add_error($message); |
|
| 651 | - $this->_feedback_message .= $message; |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
| 658 | - * |
|
| 659 | - * @return int one of EE_System::_req_type_* constants |
|
| 660 | - * @throws EE_Error |
|
| 661 | - */ |
|
| 662 | - private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
| 663 | - { |
|
| 664 | - if ($this->slug() == 'Core') { |
|
| 665 | - return EE_System::instance()->detect_req_type(); |
|
| 666 | - } else {// it must be for an addon |
|
| 667 | - $addon_name = $this->slug(); |
|
| 668 | - if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
| 669 | - return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
| 670 | - } else { |
|
| 671 | - throw new EE_Error( |
|
| 672 | - sprintf( |
|
| 673 | - __( |
|
| 674 | - "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s", |
|
| 675 | - "event_espresso" |
|
| 676 | - ), |
|
| 677 | - $this->slug(), |
|
| 678 | - $addon_name, |
|
| 679 | - implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
| 680 | - ) |
|
| 681 | - ); |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * returns an array of strings describing errors by all the script's stages |
|
| 689 | - * |
|
| 690 | - * @return array |
|
| 691 | - */ |
|
| 692 | - public function get_errors() |
|
| 693 | - { |
|
| 694 | - $all_errors = $this->_errors; |
|
| 695 | - if (! is_array($all_errors)) { |
|
| 696 | - $all_errors = array(); |
|
| 697 | - } |
|
| 698 | - foreach ($this->stages() as $stage) { |
|
| 699 | - $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
| 700 | - } |
|
| 701 | - return $all_errors; |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Indicates whether or not this migration script should continue |
|
| 707 | - * |
|
| 708 | - * @return boolean |
|
| 709 | - */ |
|
| 710 | - public function can_continue() |
|
| 711 | - { |
|
| 712 | - return in_array( |
|
| 713 | - $this->get_status(), |
|
| 714 | - EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
| 715 | - ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - |
|
| 719 | - /** |
|
| 720 | - * Gets all the data migration stages associated with this script. Note: |
|
| 721 | - * addons can filter this list to add their own stages, and because the list is |
|
| 722 | - * numerically-indexed, they can insert their stage wherever they like and it will |
|
| 723 | - * get ordered by the indexes |
|
| 724 | - * |
|
| 725 | - * @return EE_Data_Migration_Script_Stage[] |
|
| 726 | - */ |
|
| 727 | - protected function stages() |
|
| 728 | - { |
|
| 729 | - $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
| 730 | - ksort($stages); |
|
| 731 | - return $stages; |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * Gets a string which should describe what's going on currently with this migration, which |
|
| 737 | - * can be displayed to the user |
|
| 738 | - * |
|
| 739 | - * @return string |
|
| 740 | - */ |
|
| 741 | - public function get_feedback_message() |
|
| 742 | - { |
|
| 743 | - return $this->_feedback_message; |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
| 749 | - * properties to the DB. However, we don't want to use __sleep() because its quite |
|
| 750 | - * possible that this class is defined when it goes to sleep, but NOT available when it |
|
| 751 | - * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
| 752 | - */ |
|
| 753 | - public function properties_as_array() |
|
| 754 | - { |
|
| 755 | - $properties = parent::properties_as_array(); |
|
| 756 | - $properties['_migration_stages'] = array(); |
|
| 757 | - foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
| 758 | - $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
| 759 | - ); |
|
| 760 | - } |
|
| 761 | - unset($properties['_mappings']); |
|
| 762 | - |
|
| 763 | - foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
| 764 | - foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
| 765 | - $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - return $properties; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
| 774 | - * to have been made from the properties_as_array() function. |
|
| 775 | - * |
|
| 776 | - * @param array $array_of_properties like what's produced from properties_as_array() method |
|
| 777 | - * @return void |
|
| 778 | - */ |
|
| 779 | - public function instantiate_from_array_of_properties($array_of_properties) |
|
| 780 | - { |
|
| 781 | - $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
| 782 | - unset($array_of_properties['_migration_stages']); |
|
| 783 | - unset($array_of_properties['class']); |
|
| 784 | - foreach ($array_of_properties as $property_name => $property_value) { |
|
| 785 | - $this->{$property_name} = $property_value; |
|
| 786 | - } |
|
| 787 | - // _migration_stages are already instantiated, but have only default data |
|
| 788 | - foreach ($this->_migration_stages as $stage) { |
|
| 789 | - $stage_data = $this->_find_migration_stage_data_with_classname( |
|
| 790 | - get_class($stage), |
|
| 791 | - $stages_properties_arrays |
|
| 792 | - ); |
|
| 793 | - // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
| 794 | - // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
| 795 | - if ($stage_data) { |
|
| 796 | - $stage->instantiate_from_array_of_properties($stage_data); |
|
| 797 | - } |
|
| 798 | - } |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - |
|
| 802 | - /** |
|
| 803 | - * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
| 804 | - * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
| 805 | - * for the given classname |
|
| 806 | - * |
|
| 807 | - * @param string $classname |
|
| 808 | - * @param array $migration_stage_data_arrays |
|
| 809 | - * @return null |
|
| 810 | - */ |
|
| 811 | - private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
| 812 | - { |
|
| 813 | - foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
| 814 | - if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
| 815 | - return $migration_stage_data_array; |
|
| 816 | - } |
|
| 817 | - } |
|
| 818 | - return null; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * Returns the version that this script migrates to, based on the script's name. |
|
| 824 | - * Cannot be overwritten because lots of code needs to know which version a script |
|
| 825 | - * migrates to knowing only its name. |
|
| 826 | - * |
|
| 827 | - * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
| 828 | - * that will be updated to. Eg array('Core','4.1.0') |
|
| 829 | - */ |
|
| 830 | - final public function migrates_to_version() |
|
| 831 | - { |
|
| 832 | - return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
| 838 | - * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
| 839 | - * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
| 840 | - * Or 'Core' for core (non-addon). |
|
| 841 | - * |
|
| 842 | - * @return string |
|
| 843 | - */ |
|
| 844 | - public function slug() |
|
| 845 | - { |
|
| 846 | - $migrates_to_version_info = $this->migrates_to_version(); |
|
| 847 | - // the slug is the first part of the array |
|
| 848 | - return $migrates_to_version_info['slug']; |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - |
|
| 852 | - /** |
|
| 853 | - * Returns the script's priority relative to DMSs from other addons. However, when |
|
| 854 | - * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
| 855 | - * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
| 856 | - * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
| 857 | - * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
| 858 | - * the database up so it can run), then you can set "A" to priority 3 or something. |
|
| 859 | - * |
|
| 860 | - * @return int |
|
| 861 | - */ |
|
| 862 | - public function priority() |
|
| 863 | - { |
|
| 864 | - return $this->_priority; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - |
|
| 868 | - /** |
|
| 869 | - * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
| 870 | - * just being used to setup (or verify) the current database structure matches |
|
| 871 | - * what the latest DMS indicates it should be |
|
| 872 | - * |
|
| 873 | - * @param boolean $migrating |
|
| 874 | - * @return void |
|
| 875 | - */ |
|
| 876 | - public function set_migrating($migrating = true) |
|
| 877 | - { |
|
| 878 | - $this->_migrating = $migrating; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * Marks that we think this migration class can continue to migrate |
|
| 883 | - */ |
|
| 884 | - public function reattempt() |
|
| 885 | - { |
|
| 886 | - parent::reattempt(); |
|
| 887 | - // also, we want to reattempt any stages that were marked as borked |
|
| 888 | - foreach ($this->stages() as $stage) { |
|
| 889 | - if ($stage->is_broken()) { |
|
| 890 | - $stage->reattempt(); |
|
| 891 | - } |
|
| 892 | - } |
|
| 893 | - } |
|
| 18 | + /** |
|
| 19 | + * Set by client code to indicate this DMS is being ran as part of a proper migration, |
|
| 20 | + * instead of being used to merely setup (or verify) the database structure. |
|
| 21 | + * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration |
|
| 22 | + * should call EE_Data_Migration_Script_Base::set_migrating( FALSE ) |
|
| 23 | + * |
|
| 24 | + * @var boolean |
|
| 25 | + */ |
|
| 26 | + protected $_migrating = true; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object |
|
| 30 | + * |
|
| 31 | + * @var EE_Data_Migration_Script_Stage[] $migration_functions |
|
| 32 | + */ |
|
| 33 | + protected $_migration_stages = array(); |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Indicates we've already ran the schema changes that needed to happen BEFORE the data migration |
|
| 37 | + * |
|
| 38 | + * @var boolean |
|
| 39 | + */ |
|
| 40 | + protected $_schema_changes_before_migration_ran = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Indicates we've already ran the schema changes that needed to happen AFTER the data migration |
|
| 44 | + * |
|
| 45 | + * @var boolean |
|
| 46 | + */ |
|
| 47 | + protected $_schema_changes_after_migration_ran = null; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * String which describes what's currently happening in this migration |
|
| 51 | + * |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + protected $_feedback_message; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers |
|
| 58 | + * correspond to earlier execution |
|
| 59 | + * |
|
| 60 | + * @var int |
|
| 61 | + */ |
|
| 62 | + protected $_priority = 5; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Multi-dimensional array that defines the mapping from OLD table Primary Keys |
|
| 66 | + * to NEW table Primary Keys. |
|
| 67 | + * Top-level array keys are OLD table names (minus the "wp_" part), |
|
| 68 | + * 2nd-level array keys are NEW table names (again, minus the "wp_" part), |
|
| 69 | + * 3rd-level array keys are the OLD table primary keys |
|
| 70 | + * and 3rd-level array values are the NEW table primary keys |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + protected $_mappings = array(); |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Returns whether or not this data migration script can operate on the given version of the database. |
|
| 79 | + * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and |
|
| 80 | + * it's passed a string like '3.1.38B', it should return true. |
|
| 81 | + * If this DMS is to migrate data from an EE3 addon, you will probably want to use |
|
| 82 | + * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and |
|
| 83 | + * EE_Data_Migration_Manager::get_migration_ran() to check that core was already |
|
| 84 | + * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated |
|
| 85 | + * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want |
|
| 86 | + * to run this DMS). |
|
| 87 | + * If this DMS migrates data from a previous version of this EE4 addon, just |
|
| 88 | + * comparing $current_database_state_of[ $this->slug() ] will probably suffice. |
|
| 89 | + * If this DMS should never migrate data, because it's only used to define the initial |
|
| 90 | + * database state, just return FALSE (and core's activation process will take care |
|
| 91 | + * of calling its schema_changes_before_migration() and |
|
| 92 | + * schema_changes_after_migration() for you. ) |
|
| 93 | + * |
|
| 94 | + * @param array $current_database_state_of keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc) |
|
| 95 | + * @return boolean |
|
| 96 | + */ |
|
| 97 | + abstract public function can_migrate_from_version($current_database_state_of); |
|
| 98 | + |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Performs database schema changes that need to occur BEFORE the data is migrated. |
|
| 102 | + * Eg, if we were going to change user passwords from plaintext to encoded versions |
|
| 103 | + * during this migration, this would probably add a new column called something like |
|
| 104 | + * "encoded_password". |
|
| 105 | + * |
|
| 106 | + * @return boolean of success |
|
| 107 | + */ |
|
| 108 | + abstract public function schema_changes_before_migration(); |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Performs the database schema changes that need to occur AFTER the data has been migrated. |
|
| 113 | + * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords |
|
| 114 | + * from plaintext to encoded versions, and we had added a column called "encoded_password", |
|
| 115 | + * this function would probably remove the old column "password" (which still holds the plaintext password) |
|
| 116 | + * and possibly rename "encoded_password" to "password" |
|
| 117 | + * |
|
| 118 | + * @return boolean of success |
|
| 119 | + */ |
|
| 120 | + abstract public function schema_changes_after_migration(); |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * All children of this must call parent::__construct() |
|
| 125 | + * at the end of their constructor or suffer the consequences! |
|
| 126 | + * |
|
| 127 | + * @param TableManager $table_manager |
|
| 128 | + * @param TableAnalysis $table_analysis |
|
| 129 | + */ |
|
| 130 | + public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null) |
|
| 131 | + { |
|
| 132 | + $this->_migration_stages = (array) apply_filters( |
|
| 133 | + 'FHEE__' . get_class($this) . '__construct__migration_stages', |
|
| 134 | + $this->_migration_stages |
|
| 135 | + ); |
|
| 136 | + foreach ($this->_migration_stages as $migration_stage) { |
|
| 137 | + if ($migration_stage instanceof EE_Data_Migration_Script_Stage) { |
|
| 138 | + $migration_stage->_construct_finalize($this); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + parent::__construct($table_manager, $table_analysis); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Place to add hooks and filters for tweaking the migrations page, in order |
|
| 147 | + * to customize it |
|
| 148 | + */ |
|
| 149 | + public function migration_page_hooks() |
|
| 150 | + { |
|
| 151 | + // by default none are added because we normally like the default look of the migration page |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Sets the mapping from old table primary keys to new table primary keys. |
|
| 157 | + * This mapping is automatically persisted as a property on the migration |
|
| 158 | + * |
|
| 159 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 160 | + * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
| 161 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 162 | + * @param int|string $new_pk eg posts.ID |
|
| 163 | + * @return void |
|
| 164 | + */ |
|
| 165 | + public function set_mapping($old_table, $old_pk, $new_table, $new_pk) |
|
| 166 | + { |
|
| 167 | + // make sure it has the needed keys |
|
| 168 | + if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 169 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 170 | + } |
|
| 171 | + $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Gets the new primary key, if provided with the OLD table and the primary key |
|
| 177 | + * of an item in the old table, and the new table |
|
| 178 | + * |
|
| 179 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 180 | + * @param int|string $old_pk old primary key. Eg events_detail.id's value |
|
| 181 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 182 | + * @return mixed the primary key on the new table |
|
| 183 | + */ |
|
| 184 | + public function get_mapping_new_pk($old_table, $old_pk, $new_table) |
|
| 185 | + { |
|
| 186 | + if (! isset($this->_mappings[ $old_table ]) || |
|
| 187 | + ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 188 | + // try fetching the option |
|
| 189 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 190 | + } |
|
| 191 | + return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ]) |
|
| 192 | + ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Gets the old primary key, if provided with the OLD table, |
|
| 198 | + * and the new table and the primary key of an item in the new table |
|
| 199 | + * |
|
| 200 | + * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail |
|
| 201 | + * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts |
|
| 202 | + * @param mixed $new_pk |
|
| 203 | + * @return mixed |
|
| 204 | + */ |
|
| 205 | + public function get_mapping_old_pk($old_table, $new_table, $new_pk) |
|
| 206 | + { |
|
| 207 | + if (! isset($this->_mappings[ $old_table ]) || |
|
| 208 | + ! isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 209 | + // try fetching the option |
|
| 210 | + $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table); |
|
| 211 | + } |
|
| 212 | + if (isset($this->_mappings[ $old_table ][ $new_table ])) { |
|
| 213 | + $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]); |
|
| 214 | + if (isset($new_pk_to_old_pk[ $new_pk ])) { |
|
| 215 | + return $new_pk_to_old_pk[ $new_pk ]; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + return null; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Gets the mapping array option specified by the table names |
|
| 224 | + * |
|
| 225 | + * @param string $old_table_name |
|
| 226 | + * @param string $new_table_name |
|
| 227 | + * @return array |
|
| 228 | + */ |
|
| 229 | + protected function _get_mapping_option($old_table_name, $new_table_name) |
|
| 230 | + { |
|
| 231 | + $option = get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array()); |
|
| 232 | + return $option; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Updates the mapping option specified by the table names with the array provided |
|
| 238 | + * |
|
| 239 | + * @param string $old_table_name |
|
| 240 | + * @param string $new_table_name |
|
| 241 | + * @param array $mapping_array |
|
| 242 | + * @return boolean success of updating option |
|
| 243 | + */ |
|
| 244 | + protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array) |
|
| 245 | + { |
|
| 246 | + $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false); |
|
| 247 | + return $success; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Gets the option name for this script to map from $old_table_name to $new_table_name |
|
| 253 | + * |
|
| 254 | + * @param string $old_table_name |
|
| 255 | + * @param string $new_table_name |
|
| 256 | + * @return string |
|
| 257 | + */ |
|
| 258 | + protected function _get_mapping_option_name($old_table_name, $new_table_name) |
|
| 259 | + { |
|
| 260 | + global $wpdb; |
|
| 261 | + $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name); |
|
| 262 | + $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name); |
|
| 263 | + $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
| 264 | + return substr( |
|
| 265 | + EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp, |
|
| 266 | + 0, |
|
| 267 | + 64 |
|
| 268 | + ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Counts all the records that will be migrated during this data migration. |
|
| 274 | + * For example, if we were changing old user passwords from plaintext to encoded versions, |
|
| 275 | + * this would be a count of all users who have passwords. If we were going to also split |
|
| 276 | + * attendee records into transactions, registrations, and attendee records, this would include |
|
| 277 | + * the count of all attendees currently in existence in the DB (ie, users + attendees). |
|
| 278 | + * If you can't determine how many records there are to migrate, just provide a guess: this |
|
| 279 | + * number will only be used in calculating the percent complete. If you estimate there to be |
|
| 280 | + * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at |
|
| 281 | + * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete. |
|
| 282 | + * |
|
| 283 | + * @return int |
|
| 284 | + */ |
|
| 285 | + protected function _count_records_to_migrate() |
|
| 286 | + { |
|
| 287 | + $count = 0; |
|
| 288 | + foreach ($this->stages() as $stage) { |
|
| 289 | + $count += $stage->count_records_to_migrate(); |
|
| 290 | + } |
|
| 291 | + return $count; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Returns the number of records updated so far. Usually this is easiest to do |
|
| 297 | + * by just setting a transient and updating it after each migration_step |
|
| 298 | + * |
|
| 299 | + * @return int |
|
| 300 | + */ |
|
| 301 | + public function count_records_migrated() |
|
| 302 | + { |
|
| 303 | + $count = 0; |
|
| 304 | + foreach ($this->stages() as $stage) { |
|
| 305 | + $count += $stage->count_records_migrated(); |
|
| 306 | + } |
|
| 307 | + $this->_records_migrated = $count; |
|
| 308 | + return $count; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param int $num_records_to_migrate_limit |
|
| 314 | + * @return int |
|
| 315 | + * @throws EE_Error |
|
| 316 | + * @throws Exception |
|
| 317 | + */ |
|
| 318 | + public function migration_step($num_records_to_migrate_limit) |
|
| 319 | + { |
|
| 320 | + // reset the feedback message |
|
| 321 | + $this->_feedback_message = ''; |
|
| 322 | + // if we haven't yet done the 1st schema changes, do them now. buffer any output |
|
| 323 | + $this->_maybe_do_schema_changes(true); |
|
| 324 | + |
|
| 325 | + $num_records_actually_migrated = 0; |
|
| 326 | + $records_migrated_per_stage = array(); |
|
| 327 | + // setup the 'stage' variable, which should hold the last run stage of the migration (or none at all if nothing runs) |
|
| 328 | + $stage = null; |
|
| 329 | + // get the next stage that isn't complete |
|
| 330 | + foreach ($this->stages() as $stage) { |
|
| 331 | + if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) { |
|
| 332 | + try { |
|
| 333 | + $records_migrated_during_stage = $stage->migration_step( |
|
| 334 | + $num_records_to_migrate_limit - $num_records_actually_migrated |
|
| 335 | + ); |
|
| 336 | + $num_records_actually_migrated += $records_migrated_during_stage; |
|
| 337 | + $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage; |
|
| 338 | + } catch (Exception $e) { |
|
| 339 | + // yes if we catch an exception here, we consider that migration stage borked. |
|
| 340 | + $stage->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 341 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 342 | + $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString()); |
|
| 343 | + throw $e; |
|
| 344 | + } |
|
| 345 | + // check that the migration stage didn't mark itself as having a fatal error |
|
| 346 | + if ($stage->is_broken()) { |
|
| 347 | + $this->set_broken(); |
|
| 348 | + throw new EE_Error($stage->get_last_error()); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + // once we've migrated all the number we intended to (possibly from different stages), stop migrating |
|
| 352 | + // or if we had a fatal error |
|
| 353 | + // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step |
|
| 354 | + if ($num_records_actually_migrated >= $num_records_to_migrate_limit |
|
| 355 | + || $stage->is_broken() |
|
| 356 | + || $stage->has_more_to_do() |
|
| 357 | + ) { |
|
| 358 | + break; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + // check if we're all done this data migration... |
|
| 362 | + // which is indicated by being done early AND the last stage claims to be done |
|
| 363 | + if ($stage == null) { |
|
| 364 | + // this migration script apparently has NO stages... which is super weird, but whatever |
|
| 365 | + $this->set_completed(); |
|
| 366 | + $this->_maybe_do_schema_changes(false); |
|
| 367 | + } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) { |
|
| 368 | + // apparently we're done, because we couldn't migrate the number we intended to |
|
| 369 | + $this->set_completed(); |
|
| 370 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
| 371 | + // do schema changes for after the migration now |
|
| 372 | + // first double-check we haven't already done this |
|
| 373 | + $this->_maybe_do_schema_changes(false); |
|
| 374 | + } else { |
|
| 375 | + // update feedback message, keeping in mind that we show them with the most recent at the top |
|
| 376 | + $this->_update_feedback_message(array_reverse($records_migrated_per_stage)); |
|
| 377 | + } |
|
| 378 | + return $num_records_actually_migrated; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Updates the feedback message according to what was done during this migration stage. |
|
| 384 | + * |
|
| 385 | + * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records |
|
| 386 | + * migrated from that stage |
|
| 387 | + * @return void |
|
| 388 | + */ |
|
| 389 | + private function _update_feedback_message($records_migrated_per_stage) |
|
| 390 | + { |
|
| 391 | + $feedback_message_array = array(); |
|
| 392 | + foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) { |
|
| 393 | + $feedback_message_array[] = sprintf( |
|
| 394 | + __("Migrated %d records successfully during %s", "event_espresso"), |
|
| 395 | + $num_records_migrated, |
|
| 396 | + $migration_stage_name |
|
| 397 | + ); |
|
| 398 | + } |
|
| 399 | + $this->_feedback_message .= implode("<br>", $feedback_message_array); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration |
|
| 405 | + * (if $before==false). Buffers their outputs and stores them on the class. |
|
| 406 | + * |
|
| 407 | + * @param boolean $before |
|
| 408 | + * @throws Exception |
|
| 409 | + * @return void |
|
| 410 | + */ |
|
| 411 | + private function _maybe_do_schema_changes($before = true) |
|
| 412 | + { |
|
| 413 | + // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran |
|
| 414 | + $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran'; |
|
| 415 | + if (! $this->{$property_name}) { |
|
| 416 | + try { |
|
| 417 | + ob_start(); |
|
| 418 | + if ($before) { |
|
| 419 | + $this->schema_changes_before_migration(); |
|
| 420 | + } else { |
|
| 421 | + $this->schema_changes_after_migration(); |
|
| 422 | + } |
|
| 423 | + $output = ob_get_contents(); |
|
| 424 | + ob_end_clean(); |
|
| 425 | + } catch (Exception $e) { |
|
| 426 | + $this->set_status(EE_Data_Migration_Manager::status_fatal_error); |
|
| 427 | + throw $e; |
|
| 428 | + } |
|
| 429 | + // record that we've done these schema changes |
|
| 430 | + $this->{$property_name} = true; |
|
| 431 | + // if there were any warnings etc, record them as non-fatal errors |
|
| 432 | + if ($output) { |
|
| 433 | + // there were some warnings |
|
| 434 | + $this->_errors[] = $output; |
|
| 435 | + } |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Wrapper for EEH_Activation::create_table. However, takes into account the request type when |
|
| 442 | + * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead |
|
| 443 | + * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to |
|
| 444 | + * or |
|
| 445 | + * activated currently. If this is a brand new activation or a migration, and we're indicating this table should |
|
| 446 | + * not |
|
| 447 | + * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this |
|
| 448 | + * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table |
|
| 449 | + * should be nuked. |
|
| 450 | + * |
|
| 451 | + * Just for a bit of context, the migration script's db_schema_changes_* methods |
|
| 452 | + * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the |
|
| 453 | + * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was |
|
| 454 | + * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should |
|
| 455 | + * be dropped), and during a migration when we're moving the DB to the state of the migration script |
|
| 456 | + * |
|
| 457 | + * @param string $table_name |
|
| 458 | + * @param string $table_definition_sql |
|
| 459 | + * @param string $engine_string |
|
| 460 | + */ |
|
| 461 | + protected function _table_is_new_in_this_version( |
|
| 462 | + $table_name, |
|
| 463 | + $table_definition_sql, |
|
| 464 | + $engine_string = 'ENGINE=InnoDB ' |
|
| 465 | + ) { |
|
| 466 | + $this->_create_table_and_catch_errors( |
|
| 467 | + $table_name, |
|
| 468 | + $table_definition_sql, |
|
| 469 | + $engine_string, |
|
| 470 | + $this->_pre_existing_table_should_be_dropped(true) |
|
| 471 | + ); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table |
|
| 476 | + * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not |
|
| 477 | + * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is |
|
| 478 | + * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as |
|
| 479 | + * _table_should_exist_previously. |
|
| 480 | + * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the |
|
| 481 | + * table shouldn't exist). |
|
| 482 | + * |
|
| 483 | + * @param string $table_name |
|
| 484 | + * @param string $table_definition_sql |
|
| 485 | + * @param string $engine_string |
|
| 486 | + */ |
|
| 487 | + protected function _table_is_changed_in_this_version( |
|
| 488 | + $table_name, |
|
| 489 | + $table_definition_sql, |
|
| 490 | + $engine_string = 'ENGINE=MyISAM' |
|
| 491 | + ) { |
|
| 492 | + $this->_create_table_and_catch_errors( |
|
| 493 | + $table_name, |
|
| 494 | + $table_definition_sql, |
|
| 495 | + $engine_string, |
|
| 496 | + $this->_pre_existing_table_should_be_dropped(false) |
|
| 497 | + ); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * _old_table_exists |
|
| 503 | + * returns TRUE if the requested table exists in the current database |
|
| 504 | + * |
|
| 505 | + * @param string $table_name |
|
| 506 | + * @return boolean |
|
| 507 | + */ |
|
| 508 | + protected function _old_table_exists($table_name) |
|
| 509 | + { |
|
| 510 | + return $this->_get_table_analysis()->tableExists($table_name); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * _delete_table_if_empty |
|
| 516 | + * returns TRUE if the requested table was empty and successfully empty |
|
| 517 | + * |
|
| 518 | + * @param string $table_name |
|
| 519 | + * @return boolean |
|
| 520 | + */ |
|
| 521 | + protected function _delete_table_if_empty($table_name) |
|
| 522 | + { |
|
| 523 | + return EEH_Activation::delete_db_table_if_empty($table_name); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version |
|
| 529 | + * as these are significantly more efficient or explicit. |
|
| 530 | + * Please see description of _table_is_new_in_this_version. This function will only set |
|
| 531 | + * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand |
|
| 532 | + * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin" |
|
| 533 | + * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually |
|
| 534 | + * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table |
|
| 535 | + * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then |
|
| 536 | + * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations |
|
| 537 | + * because apparently it hasn't changed since the previous one, right? |
|
| 538 | + * |
|
| 539 | + * @param string $table_name |
|
| 540 | + * @param string $table_definition_sql |
|
| 541 | + * @param string $engine_string |
|
| 542 | + */ |
|
| 543 | + protected function _table_should_exist_previously( |
|
| 544 | + $table_name, |
|
| 545 | + $table_definition_sql, |
|
| 546 | + $engine_string = 'ENGINE=MyISAM' |
|
| 547 | + ) { |
|
| 548 | + $this->_create_table_and_catch_errors( |
|
| 549 | + $table_name, |
|
| 550 | + $table_definition_sql, |
|
| 551 | + $engine_string, |
|
| 552 | + $this->_pre_existing_table_should_be_dropped(false) |
|
| 553 | + ); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing |
|
| 558 | + * a migration, we skip checking this table's structure in the database and just assume it's correct. |
|
| 559 | + * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs, |
|
| 560 | + * but important for multisite where migrations can take a very long time otherwise). |
|
| 561 | + * If the table is known to have changed since previous version, use _table_is_changed_in_this_version(). |
|
| 562 | + * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table |
|
| 563 | + * shouldn't exist). |
|
| 564 | + * |
|
| 565 | + * @param string $table_name |
|
| 566 | + * @param string $table_definition_sql |
|
| 567 | + * @param string $engine_string |
|
| 568 | + */ |
|
| 569 | + protected function _table_has_not_changed_since_previous( |
|
| 570 | + $table_name, |
|
| 571 | + $table_definition_sql, |
|
| 572 | + $engine_string = 'ENGINE=MyISAM' |
|
| 573 | + ) { |
|
| 574 | + if ($this->_currently_migrating()) { |
|
| 575 | + // if we're doing a migration, and this table apparently already exists, then we don't need do anything right? |
|
| 576 | + return; |
|
| 577 | + } |
|
| 578 | + $this->_create_table_and_catch_errors( |
|
| 579 | + $table_name, |
|
| 580 | + $table_definition_sql, |
|
| 581 | + $engine_string, |
|
| 582 | + $this->_pre_existing_table_should_be_dropped(false) |
|
| 583 | + ); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Returns whether or not this migration script is being used as part of an actual migration |
|
| 588 | + * |
|
| 589 | + * @return boolean |
|
| 590 | + */ |
|
| 591 | + protected function _currently_migrating() |
|
| 592 | + { |
|
| 593 | + // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM) |
|
| 594 | + return $this->_migrating && |
|
| 595 | + $this->can_migrate_from_version( |
|
| 596 | + EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set() |
|
| 597 | + ) && |
|
| 598 | + EE_Maintenance_Mode::instance()->real_level() == EE_Maintenance_Mode::level_2_complete_maintenance; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + /** |
|
| 602 | + * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new, |
|
| 603 | + * and the plugin's request type. |
|
| 604 | + * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table |
|
| 605 | + * shouldn't exist no matter what). |
|
| 606 | + * |
|
| 607 | + * @param boolean $table_is_new |
|
| 608 | + * @return boolean |
|
| 609 | + */ |
|
| 610 | + protected function _pre_existing_table_should_be_dropped($table_is_new) |
|
| 611 | + { |
|
| 612 | + if ($table_is_new) { |
|
| 613 | + if ($this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation |
|
| 614 | + || $this->_currently_migrating() |
|
| 615 | + ) { |
|
| 616 | + return true; |
|
| 617 | + } else { |
|
| 618 | + return false; |
|
| 619 | + } |
|
| 620 | + } else { |
|
| 621 | + if (in_array( |
|
| 622 | + $this->_get_req_type_for_plugin_corresponding_to_this_dms(), |
|
| 623 | + array(EE_System::req_type_new_activation) |
|
| 624 | + )) { |
|
| 625 | + return true; |
|
| 626 | + } else { |
|
| 627 | + return false; |
|
| 628 | + } |
|
| 629 | + } |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS |
|
| 634 | + * |
|
| 635 | + * @param string $table_name |
|
| 636 | + * @param string $table_definition_sql |
|
| 637 | + * @param string $engine_string |
|
| 638 | + * @param boolean $drop_pre_existing_tables |
|
| 639 | + */ |
|
| 640 | + private function _create_table_and_catch_errors( |
|
| 641 | + $table_name, |
|
| 642 | + $table_definition_sql, |
|
| 643 | + $engine_string = 'ENGINE=MyISAM', |
|
| 644 | + $drop_pre_existing_tables = false |
|
| 645 | + ) { |
|
| 646 | + try { |
|
| 647 | + EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables); |
|
| 648 | + } catch (EE_Error $e) { |
|
| 649 | + $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString(); |
|
| 650 | + $this->add_error($message); |
|
| 651 | + $this->_feedback_message .= $message; |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Gets the request type for the plugin (core or addon) that corresponds to this DMS |
|
| 658 | + * |
|
| 659 | + * @return int one of EE_System::_req_type_* constants |
|
| 660 | + * @throws EE_Error |
|
| 661 | + */ |
|
| 662 | + private function _get_req_type_for_plugin_corresponding_to_this_dms() |
|
| 663 | + { |
|
| 664 | + if ($this->slug() == 'Core') { |
|
| 665 | + return EE_System::instance()->detect_req_type(); |
|
| 666 | + } else {// it must be for an addon |
|
| 667 | + $addon_name = $this->slug(); |
|
| 668 | + if (EE_Registry::instance()->get_addon_by_name($addon_name)) { |
|
| 669 | + return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type(); |
|
| 670 | + } else { |
|
| 671 | + throw new EE_Error( |
|
| 672 | + sprintf( |
|
| 673 | + __( |
|
| 674 | + "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s", |
|
| 675 | + "event_espresso" |
|
| 676 | + ), |
|
| 677 | + $this->slug(), |
|
| 678 | + $addon_name, |
|
| 679 | + implode(",", array_keys(EE_Registry::instance()->get_addons_by_name())) |
|
| 680 | + ) |
|
| 681 | + ); |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * returns an array of strings describing errors by all the script's stages |
|
| 689 | + * |
|
| 690 | + * @return array |
|
| 691 | + */ |
|
| 692 | + public function get_errors() |
|
| 693 | + { |
|
| 694 | + $all_errors = $this->_errors; |
|
| 695 | + if (! is_array($all_errors)) { |
|
| 696 | + $all_errors = array(); |
|
| 697 | + } |
|
| 698 | + foreach ($this->stages() as $stage) { |
|
| 699 | + $all_errors = array_merge($stage->get_errors(), $all_errors); |
|
| 700 | + } |
|
| 701 | + return $all_errors; |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Indicates whether or not this migration script should continue |
|
| 707 | + * |
|
| 708 | + * @return boolean |
|
| 709 | + */ |
|
| 710 | + public function can_continue() |
|
| 711 | + { |
|
| 712 | + return in_array( |
|
| 713 | + $this->get_status(), |
|
| 714 | + EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script |
|
| 715 | + ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + |
|
| 719 | + /** |
|
| 720 | + * Gets all the data migration stages associated with this script. Note: |
|
| 721 | + * addons can filter this list to add their own stages, and because the list is |
|
| 722 | + * numerically-indexed, they can insert their stage wherever they like and it will |
|
| 723 | + * get ordered by the indexes |
|
| 724 | + * |
|
| 725 | + * @return EE_Data_Migration_Script_Stage[] |
|
| 726 | + */ |
|
| 727 | + protected function stages() |
|
| 728 | + { |
|
| 729 | + $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages); |
|
| 730 | + ksort($stages); |
|
| 731 | + return $stages; |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * Gets a string which should describe what's going on currently with this migration, which |
|
| 737 | + * can be displayed to the user |
|
| 738 | + * |
|
| 739 | + * @return string |
|
| 740 | + */ |
|
| 741 | + public function get_feedback_message() |
|
| 742 | + { |
|
| 743 | + return $this->_feedback_message; |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class' |
|
| 749 | + * properties to the DB. However, we don't want to use __sleep() because its quite |
|
| 750 | + * possible that this class is defined when it goes to sleep, but NOT available when it |
|
| 751 | + * awakes (eg, this class is part of an addon that is deactivated at some point). |
|
| 752 | + */ |
|
| 753 | + public function properties_as_array() |
|
| 754 | + { |
|
| 755 | + $properties = parent::properties_as_array(); |
|
| 756 | + $properties['_migration_stages'] = array(); |
|
| 757 | + foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) { |
|
| 758 | + $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array( |
|
| 759 | + ); |
|
| 760 | + } |
|
| 761 | + unset($properties['_mappings']); |
|
| 762 | + |
|
| 763 | + foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) { |
|
| 764 | + foreach ($mapping_to_new_table as $new_table_name => $mapping) { |
|
| 765 | + $this->_set_mapping_option($old_table_name, $new_table_name, $mapping); |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + return $properties; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * Sets all of the properties of this script stage to match what's in the array, which is assumed |
|
| 774 | + * to have been made from the properties_as_array() function. |
|
| 775 | + * |
|
| 776 | + * @param array $array_of_properties like what's produced from properties_as_array() method |
|
| 777 | + * @return void |
|
| 778 | + */ |
|
| 779 | + public function instantiate_from_array_of_properties($array_of_properties) |
|
| 780 | + { |
|
| 781 | + $stages_properties_arrays = $array_of_properties['_migration_stages']; |
|
| 782 | + unset($array_of_properties['_migration_stages']); |
|
| 783 | + unset($array_of_properties['class']); |
|
| 784 | + foreach ($array_of_properties as $property_name => $property_value) { |
|
| 785 | + $this->{$property_name} = $property_value; |
|
| 786 | + } |
|
| 787 | + // _migration_stages are already instantiated, but have only default data |
|
| 788 | + foreach ($this->_migration_stages as $stage) { |
|
| 789 | + $stage_data = $this->_find_migration_stage_data_with_classname( |
|
| 790 | + get_class($stage), |
|
| 791 | + $stages_properties_arrays |
|
| 792 | + ); |
|
| 793 | + // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by |
|
| 794 | + // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet) |
|
| 795 | + if ($stage_data) { |
|
| 796 | + $stage->instantiate_from_array_of_properties($stage_data); |
|
| 797 | + } |
|
| 798 | + } |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + |
|
| 802 | + /** |
|
| 803 | + * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which |
|
| 804 | + * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes) |
|
| 805 | + * for the given classname |
|
| 806 | + * |
|
| 807 | + * @param string $classname |
|
| 808 | + * @param array $migration_stage_data_arrays |
|
| 809 | + * @return null |
|
| 810 | + */ |
|
| 811 | + private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays) |
|
| 812 | + { |
|
| 813 | + foreach ($migration_stage_data_arrays as $migration_stage_data_array) { |
|
| 814 | + if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) { |
|
| 815 | + return $migration_stage_data_array; |
|
| 816 | + } |
|
| 817 | + } |
|
| 818 | + return null; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * Returns the version that this script migrates to, based on the script's name. |
|
| 824 | + * Cannot be overwritten because lots of code needs to know which version a script |
|
| 825 | + * migrates to knowing only its name. |
|
| 826 | + * |
|
| 827 | + * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin |
|
| 828 | + * that will be updated to. Eg array('Core','4.1.0') |
|
| 829 | + */ |
|
| 830 | + final public function migrates_to_version() |
|
| 831 | + { |
|
| 832 | + return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this)); |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * Gets this addon's slug as it would appear in the current_db_state wp option, |
|
| 838 | + * and if this migration script is for an addon, it SHOULD match the addon's slug |
|
| 839 | + * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon. |
|
| 840 | + * Or 'Core' for core (non-addon). |
|
| 841 | + * |
|
| 842 | + * @return string |
|
| 843 | + */ |
|
| 844 | + public function slug() |
|
| 845 | + { |
|
| 846 | + $migrates_to_version_info = $this->migrates_to_version(); |
|
| 847 | + // the slug is the first part of the array |
|
| 848 | + return $migrates_to_version_info['slug']; |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + |
|
| 852 | + /** |
|
| 853 | + * Returns the script's priority relative to DMSs from other addons. However, when |
|
| 854 | + * two DMSs from the same addon/core apply, this is ignored (and instead the version that |
|
| 855 | + * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs |
|
| 856 | + * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons, |
|
| 857 | + * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set |
|
| 858 | + * the database up so it can run), then you can set "A" to priority 3 or something. |
|
| 859 | + * |
|
| 860 | + * @return int |
|
| 861 | + */ |
|
| 862 | + public function priority() |
|
| 863 | + { |
|
| 864 | + return $this->_priority; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + |
|
| 868 | + /** |
|
| 869 | + * Sets whether or not this DMS is being ran as part of a migration, instead of |
|
| 870 | + * just being used to setup (or verify) the current database structure matches |
|
| 871 | + * what the latest DMS indicates it should be |
|
| 872 | + * |
|
| 873 | + * @param boolean $migrating |
|
| 874 | + * @return void |
|
| 875 | + */ |
|
| 876 | + public function set_migrating($migrating = true) |
|
| 877 | + { |
|
| 878 | + $this->_migrating = $migrating; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * Marks that we think this migration class can continue to migrate |
|
| 883 | + */ |
|
| 884 | + public function reattempt() |
|
| 885 | + { |
|
| 886 | + parent::reattempt(); |
|
| 887 | + // also, we want to reattempt any stages that were marked as borked |
|
| 888 | + foreach ($this->stages() as $stage) { |
|
| 889 | + if ($stage->is_broken()) { |
|
| 890 | + $stage->reattempt(); |
|
| 891 | + } |
|
| 892 | + } |
|
| 893 | + } |
|
| 894 | 894 | } |