Creates_Reports.php
1 year ago
Migration.php
1 year ago
Migration_10.php
2 years ago
Migration_11.php
2 years ago
Migration_12.php
1 year ago
Migration_13.php
2 years ago
Migration_14.php
2 years ago
Migration_15.php
2 years ago
Migration_16.php
2 years ago
Migration_17.php
1 year ago
Migration_18.php
2 years ago
Migration_19.php
2 years ago
Migration_1_0.php
2 years ago
Migration_1_6.php
2 years ago
Migration_1_8.php
2 years ago
Migration_1_9.php
2 years ago
Migration_2.php
2 years ago
Migration_20.php
2 years ago
Migration_21.php
2 years ago
Migration_22.php
2 years ago
Migration_23.php
1 year ago
Migration_24.php
2 years ago
Migration_25.php
2 years ago
Migration_26.php
2 years ago
Migration_27.php
2 years ago
Migration_28.php
2 years ago
Migration_29.php
2 years ago
Migration_3.php
2 years ago
Migration_30.php
2 years ago
Migration_31.php
2 years ago
Migration_32.php
2 years ago
Migration_33.php
2 years ago
Migration_34.php
1 year ago
Migration_35.php
1 year ago
Migration_36.php
1 year ago
Migration_37.php
1 year ago
Migration_38.php
1 year ago
Migration_39.php
1 year ago
Migration_4.php
2 years ago
Migration_40.php
1 year ago
Migration_41.php
1 year ago
Migration_42.php
1 year ago
Migration_43.php
1 year ago
Migration_44.php
9 months ago
Migration_45.php
8 months ago
Migration_46.php
8 months ago
Migration_47.php
8 months ago
Migration_48.php
8 months ago
Migration_49.php
8 months ago
Migration_5.php
2 years ago
Migration_50.php
9 months ago
Migration_51.php
8 months ago
Migration_52.php
8 months ago
Migration_6.php
2 years ago
Migration_7.php
1 year ago
Migration_8.php
2 years ago
Migration_9.php
2 years ago
Migration_Job.php
1 year ago
Migrations.php
8 months ago
Step_Migration.php
5 months ago
Step_Migration.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Migrations; |
| 4 | |
| 5 | use IAWP\Database; |
| 6 | use IAWP\Query; |
| 7 | use IAWP\Tables; |
| 8 | use IAWPSCOPED\Illuminate\Support\Str; |
| 9 | /** @internal */ |
| 10 | abstract class Step_Migration |
| 11 | { |
| 12 | protected $tables = Tables::class; |
| 13 | protected abstract function database_version() : int; |
| 14 | protected abstract function queries() : array; |
| 15 | public function migrate() : bool |
| 16 | { |
| 17 | $current_db_version = \get_option('iawp_db_version', '0'); |
| 18 | if (\version_compare($current_db_version, \strval($this->database_version()), '>=')) { |
| 19 | return \true; |
| 20 | } |
| 21 | \update_option('iawp_migration_started_at', \time(), \true); |
| 22 | try { |
| 23 | $completed = $this->run_queries(); |
| 24 | } catch (\Throwable $error) { |
| 25 | $completed = \false; |
| 26 | \update_option('iawp_migration_error_original_error_message', 'Unable to generate migration queries: ' . $error->getMessage(), \true); |
| 27 | \update_option('iawp_migration_error', 'Unable to generate migration queries.', \true); |
| 28 | \update_option('iawp_migration_error_query', $error->getMessage(), \true); |
| 29 | } |
| 30 | if ($completed) { |
| 31 | \update_option('iawp_db_version', $this->database_version(), \true); |
| 32 | \delete_option('iawp_migration_auto_fixed'); |
| 33 | } |
| 34 | return $completed; |
| 35 | } |
| 36 | public function character_set() : string |
| 37 | { |
| 38 | return Database::character_set(); |
| 39 | } |
| 40 | public function collation() : string |
| 41 | { |
| 42 | return Database::collation(); |
| 43 | } |
| 44 | protected function drop_table_if_exists(string $table_name) : string |
| 45 | { |
| 46 | return "\n DROP TABLE IF EXISTS {$table_name};\n "; |
| 47 | } |
| 48 | protected function get_collation_statement(?string $from, ?string $to) : string |
| 49 | { |
| 50 | if (!$from || !$to) { |
| 51 | return ''; |
| 52 | } |
| 53 | if ($from === $to) { |
| 54 | return ''; |
| 55 | } |
| 56 | $from_character_set = $this->extract_character_set($from); |
| 57 | $to_character_set = $this->extract_character_set($to); |
| 58 | if (!$from_character_set || !$to_character_set || $from_character_set !== $to_character_set) { |
| 59 | return ''; |
| 60 | } |
| 61 | return " COLLATE {$from} "; |
| 62 | } |
| 63 | private function extract_character_set(string $collation) : ?string |
| 64 | { |
| 65 | if (!Str::of($collation)->test('/\\A[a-zA-Z0-9]+_/')) { |
| 66 | return null; |
| 67 | } |
| 68 | return Str::before($collation, '_'); |
| 69 | } |
| 70 | private function run_queries() : bool |
| 71 | { |
| 72 | global $wpdb; |
| 73 | $queries = $this->queries(); |
| 74 | foreach ($queries as $index => $query) { |
| 75 | // Skip the step if there is no query to run |
| 76 | if (\is_null($query)) { |
| 77 | \update_option('iawp_last_finished_migration_step', $index + 1, \true); |
| 78 | continue; |
| 79 | } |
| 80 | try { |
| 81 | $initial_response = $wpdb->query($query); |
| 82 | } catch (\Throwable $error) { |
| 83 | $max_connections_error = 'SQLSTATE[HY000] [1203]'; |
| 84 | if (Str::startsWith($error->getMessage(), $max_connections_error)) { |
| 85 | $initial_response = \false; |
| 86 | } else { |
| 87 | throw $error; |
| 88 | } |
| 89 | } |
| 90 | if ($initial_response === \false) { |
| 91 | \sleep(1); |
| 92 | \update_option('iawp_migration_error_original_error_message', \trim($wpdb->last_error), \true); |
| 93 | $is_connected = $wpdb->check_connection(\false); |
| 94 | if (!$is_connected) { |
| 95 | // There is no database connection at this point, so options cannot be updated |
| 96 | return \false; |
| 97 | } |
| 98 | $retry_response = $wpdb->query($query); |
| 99 | if ($retry_response === \false) { |
| 100 | // You cannot take these variable values and inline them below. The calls to |
| 101 | // update_option use $wpdb, so last_error and last_query will be altered |
| 102 | $last_error = \trim($wpdb->last_error); |
| 103 | $last_query = \trim($wpdb->last_query); |
| 104 | \update_option('iawp_migration_error', $last_error, \true); |
| 105 | \update_option('iawp_migration_error_query', $last_query, \true); |
| 106 | return \false; |
| 107 | } |
| 108 | } |
| 109 | \update_option('iawp_last_finished_migration_step', $index + 1, \true); |
| 110 | } |
| 111 | return \true; |
| 112 | } |
| 113 | } |
| 114 |