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
Migrations.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Migrations; |
| 4 | |
| 5 | use IAWP\Database; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Query; |
| 8 | use IAWP\Tables; |
| 9 | use IAWP\Utils\Dir; |
| 10 | use IAWP\Utils\Server; |
| 11 | use IAWPSCOPED\Illuminate\Support\Str; |
| 12 | /** @internal */ |
| 13 | class Migrations |
| 14 | { |
| 15 | /** |
| 16 | * @return void |
| 17 | */ |
| 18 | public static function create_or_migrate() : void |
| 19 | { |
| 20 | if (self::should_migrate()) { |
| 21 | Server::increase_max_execution_time(); |
| 22 | \update_option('iawp_is_migrating', '1', \true); |
| 23 | new \IAWP\Migrations\Migration_1_0(); |
| 24 | new \IAWP\Migrations\Migration_1_6(); |
| 25 | new \IAWP\Migrations\Migration_1_8(); |
| 26 | new \IAWP\Migrations\Migration_1_9(); |
| 27 | new \IAWP\Migrations\Migration_2(); |
| 28 | new \IAWP\Migrations\Migration_3(); |
| 29 | new \IAWP\Migrations\Migration_4(); |
| 30 | new \IAWP\Migrations\Migration_5(); |
| 31 | new \IAWP\Migrations\Migration_6(); |
| 32 | new \IAWP\Migrations\Migration_7(); |
| 33 | new \IAWP\Migrations\Migration_8(); |
| 34 | new \IAWP\Migrations\Migration_9(); |
| 35 | new \IAWP\Migrations\Migration_10(); |
| 36 | new \IAWP\Migrations\Migration_11(); |
| 37 | new \IAWP\Migrations\Migration_12(); |
| 38 | new \IAWP\Migrations\Migration_13(); |
| 39 | new \IAWP\Migrations\Migration_14(); |
| 40 | new \IAWP\Migrations\Migration_15(); |
| 41 | new \IAWP\Migrations\Migration_16(); |
| 42 | new \IAWP\Migrations\Migration_17(); |
| 43 | new \IAWP\Migrations\Migration_18(); |
| 44 | new \IAWP\Migrations\Migration_19(); |
| 45 | new \IAWP\Migrations\Migration_20(); |
| 46 | new \IAWP\Migrations\Migration_21(); |
| 47 | $completed = self::run_step_migrations([new \IAWP\Migrations\Migration_22(), new \IAWP\Migrations\Migration_23(), new \IAWP\Migrations\Migration_24(), new \IAWP\Migrations\Migration_25(), new \IAWP\Migrations\Migration_26(), new \IAWP\Migrations\Migration_27(), new \IAWP\Migrations\Migration_28(), new \IAWP\Migrations\Migration_29(), new \IAWP\Migrations\Migration_30(), new \IAWP\Migrations\Migration_31(), new \IAWP\Migrations\Migration_32(), new \IAWP\Migrations\Migration_33(), new \IAWP\Migrations\Migration_34(), new \IAWP\Migrations\Migration_35(), new \IAWP\Migrations\Migration_36(), new \IAWP\Migrations\Migration_37(), new \IAWP\Migrations\Migration_38(), new \IAWP\Migrations\Migration_39(), new \IAWP\Migrations\Migration_40(), new \IAWP\Migrations\Migration_41(), new \IAWP\Migrations\Migration_42(), new \IAWP\Migrations\Migration_43(), new \IAWP\Migrations\Migration_44(), new \IAWP\Migrations\Migration_45(), new \IAWP\Migrations\Migration_46(), new \IAWP\Migrations\Migration_47(), new \IAWP\Migrations\Migration_48(), new \IAWP\Migrations\Migration_49(), new \IAWP\Migrations\Migration_50(), new \IAWP\Migrations\Migration_51(), new \IAWP\Migrations\Migration_52()]); |
| 48 | if ($completed === \true) { |
| 49 | \update_option('iawp_is_migrating', '0', \true); |
| 50 | \delete_option('iawp_migration_started_at'); |
| 51 | \delete_option('iawp_last_finished_migration_step'); |
| 52 | \delete_option('iawp_migration_error'); |
| 53 | \delete_option('iawp_migration_error_query'); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | /** |
| 58 | * is_migrating is serving multiple purposes. It's also being used to stop ajax requests and dashboard |
| 59 | * widgets from running when the database version is newer than one that comes with the installed version |
| 60 | * of independent analytics. The probably should be a method called something `database_ready` that serves |
| 61 | * this purpose more explicitly. |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | public static function is_migrating() : bool |
| 66 | { |
| 67 | $db_version = \get_option('iawp_db_version', '0'); |
| 68 | $is_migrating = \get_option('iawp_is_migrating') === '1'; |
| 69 | $is_current = \version_compare($db_version, '52', '='); |
| 70 | $is_outdated = !$is_current; |
| 71 | return $is_outdated || $is_migrating; |
| 72 | } |
| 73 | public static function is_database_ahead_of_plugin() : bool |
| 74 | { |
| 75 | $db_version = \get_option('iawp_db_version', '0'); |
| 76 | return \version_compare($db_version, '52', '>'); |
| 77 | } |
| 78 | public static function is_actually_migrating() : bool |
| 79 | { |
| 80 | return \get_option('iawp_is_migrating') === '1'; |
| 81 | } |
| 82 | /** |
| 83 | * @return bool |
| 84 | */ |
| 85 | public static function should_migrate() : bool |
| 86 | { |
| 87 | $db_version = \get_option('iawp_db_version', '0'); |
| 88 | $is_migrating = \get_option('iawp_is_migrating') === '1'; |
| 89 | $is_current = \version_compare($db_version, '52', '='); |
| 90 | $is_outdated = !$is_current; |
| 91 | return $is_outdated && !$is_migrating; |
| 92 | } |
| 93 | public static function handle_migration_18_error() : void |
| 94 | { |
| 95 | $directory = \trailingslashit(\wp_upload_dir()['basedir']) . 'iawp/'; |
| 96 | $db_version = \get_option('iawp_db_version', '0'); |
| 97 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 98 | if ($db_version === '17' && $is_migrating && \is_dir($directory)) { |
| 99 | \update_option('iawp_db_version', '18', \true); |
| 100 | \update_option('iawp_is_migrating', '0', \true); |
| 101 | \delete_option('iawp_migration_started_at'); |
| 102 | \delete_option('iawp_last_finished_migration_step'); |
| 103 | \delete_option('iawp_migration_error'); |
| 104 | \delete_option('iawp_migration_error_query'); |
| 105 | try { |
| 106 | $directory = \trailingslashit(\wp_upload_dir()['basedir']) . 'iawp/'; |
| 107 | Dir::delete($directory); |
| 108 | } catch (\Throwable $e) { |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | public static function handle_migration_22_error() : void |
| 113 | { |
| 114 | $db_version = \get_option('iawp_db_version', '0'); |
| 115 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 116 | $last_finished_step = \get_option('iawp_last_finished_migration_step', '0'); |
| 117 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 118 | $referrers_table = Query::get_table_name(Query::REFERRERS); |
| 119 | $has_index = Database::has_index($referrers_table, 'referrers_domain_index'); |
| 120 | if ($db_version === '21' && $is_migrating && $last_finished_step === '0' && $has_error && !$has_index) { |
| 121 | \update_option('iawp_is_migrating', '0', \true); |
| 122 | \delete_option('iawp_migration_started_at'); |
| 123 | \delete_option('iawp_last_finished_migration_step'); |
| 124 | \delete_option('iawp_migration_error'); |
| 125 | \delete_option('iawp_migration_error_query'); |
| 126 | } |
| 127 | } |
| 128 | public static function handle_migration_29_error() : void |
| 129 | { |
| 130 | global $wpdb; |
| 131 | $db_version = \get_option('iawp_db_version', '0'); |
| 132 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 133 | $last_finished_step = \get_option('iawp_last_finished_migration_step', '0'); |
| 134 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 135 | if ($db_version === '28' && $is_migrating && $last_finished_step === '5' && $has_error) { |
| 136 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 137 | $wpdb->query("ALTER TABLE {$sessions_table} DROP COLUMN visitor_id"); |
| 138 | $wpdb->query("ALTER TABLE {$sessions_table} CHANGE COLUMN old_visitor_id visitor_id varchar(32)"); |
| 139 | \delete_option('iawp_last_finished_migration_step'); |
| 140 | \delete_option('iawp_migration_error'); |
| 141 | \delete_option('iawp_migration_error_query'); |
| 142 | \update_option('iawp_is_migrating', '0', \true); |
| 143 | \delete_option('iawp_migration_started_at'); |
| 144 | } |
| 145 | } |
| 146 | public static function handle_migration_45_collation_error() : void |
| 147 | { |
| 148 | $db_version = \get_option('iawp_db_version', '0'); |
| 149 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 150 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 151 | if ($db_version !== '44' || !$is_migrating || !$has_error) { |
| 152 | return; |
| 153 | } |
| 154 | $failed_query = \get_option('iawp_migration_error_query', ''); |
| 155 | if (!\is_string($failed_query)) { |
| 156 | return; |
| 157 | } |
| 158 | $failed_query = \strtolower(\trim($failed_query)); |
| 159 | if (!Str::startsWith($failed_query, 'update')) { |
| 160 | return; |
| 161 | } |
| 162 | try { |
| 163 | $updated_referrer = Illuminate_Builder::new()->from(Tables::referrers())->whereNotNull('referrer_type_id')->first(); |
| 164 | if ($updated_referrer !== null) { |
| 165 | return; |
| 166 | } |
| 167 | } catch (\Throwable $e) { |
| 168 | return; |
| 169 | } |
| 170 | try { |
| 171 | Illuminate_Builder::new()->select('type')->from(Tables::referrers())->value('type'); |
| 172 | } catch (\Throwable $e) { |
| 173 | return; |
| 174 | } |
| 175 | try { |
| 176 | $referrers_table = Tables::referrers(); |
| 177 | Illuminate_Builder::get_connection()->statement("ALTER TABLE {$referrers_table} DROP COLUMN referrer_type_id"); |
| 178 | } catch (\Throwable $e) { |
| 179 | return; |
| 180 | } |
| 181 | \delete_option('iawp_last_finished_migration_step'); |
| 182 | \delete_option('iawp_migration_error'); |
| 183 | \delete_option('iawp_migration_error_original_error_message'); |
| 184 | \delete_option('iawp_migration_error_query'); |
| 185 | \delete_option('iawp_migration_started_at'); |
| 186 | \update_option('iawp_is_migrating', '0', \true); |
| 187 | } |
| 188 | public static function handle_migration_46_error() : void |
| 189 | { |
| 190 | $db_version = \get_option('iawp_db_version', '0'); |
| 191 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 192 | $last_finished_step = \get_option('iawp_last_finished_migration_step', '0'); |
| 193 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 194 | if ($db_version === '45' && $is_migrating && $last_finished_step === '7' && $has_error) { |
| 195 | \delete_option('iawp_last_finished_migration_step'); |
| 196 | \delete_option('iawp_migration_error'); |
| 197 | \delete_option('iawp_migration_error_original_error_message'); |
| 198 | \delete_option('iawp_migration_error_query'); |
| 199 | \delete_option('iawp_migration_started_at'); |
| 200 | \update_option('iawp_is_migrating', '0', \true); |
| 201 | } |
| 202 | } |
| 203 | /** |
| 204 | * @param Step_Migration[] $migrations |
| 205 | * |
| 206 | * @return bool |
| 207 | */ |
| 208 | private static function run_step_migrations(array $migrations) : bool |
| 209 | { |
| 210 | foreach ($migrations as $migration) { |
| 211 | $completed = $migration->migrate(); |
| 212 | if (!$completed) { |
| 213 | return \false; |
| 214 | } |
| 215 | } |
| 216 | return \true; |
| 217 | } |
| 218 | } |
| 219 |