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
2 years 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_4.php
2 years ago
Migration_5.php
2 years 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
2 years ago
Migrations.php
1 year ago
Step_Migration.php
1 year ago
Migrations.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Migrations; |
| 4 | |
| 5 | use IAWP\Database; |
| 6 | use IAWP\Query; |
| 7 | use IAWP\Utils\Dir; |
| 8 | use IAWP\Utils\Server; |
| 9 | /** @internal */ |
| 10 | class Migrations |
| 11 | { |
| 12 | /** |
| 13 | * @return void |
| 14 | */ |
| 15 | public static function create_or_migrate() : void |
| 16 | { |
| 17 | if (self::should_migrate()) { |
| 18 | \update_option('iawp_is_migrating', '1', \true); |
| 19 | Server::increase_max_execution_time(); |
| 20 | new \IAWP\Migrations\Migration_1_0(); |
| 21 | new \IAWP\Migrations\Migration_1_6(); |
| 22 | new \IAWP\Migrations\Migration_1_8(); |
| 23 | new \IAWP\Migrations\Migration_1_9(); |
| 24 | new \IAWP\Migrations\Migration_2(); |
| 25 | new \IAWP\Migrations\Migration_3(); |
| 26 | new \IAWP\Migrations\Migration_4(); |
| 27 | new \IAWP\Migrations\Migration_5(); |
| 28 | new \IAWP\Migrations\Migration_6(); |
| 29 | new \IAWP\Migrations\Migration_7(); |
| 30 | new \IAWP\Migrations\Migration_8(); |
| 31 | new \IAWP\Migrations\Migration_9(); |
| 32 | new \IAWP\Migrations\Migration_10(); |
| 33 | new \IAWP\Migrations\Migration_11(); |
| 34 | new \IAWP\Migrations\Migration_12(); |
| 35 | new \IAWP\Migrations\Migration_13(); |
| 36 | new \IAWP\Migrations\Migration_14(); |
| 37 | new \IAWP\Migrations\Migration_15(); |
| 38 | new \IAWP\Migrations\Migration_16(); |
| 39 | new \IAWP\Migrations\Migration_17(); |
| 40 | new \IAWP\Migrations\Migration_18(); |
| 41 | new \IAWP\Migrations\Migration_19(); |
| 42 | new \IAWP\Migrations\Migration_20(); |
| 43 | new \IAWP\Migrations\Migration_21(); |
| 44 | $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()]); |
| 45 | if ($completed === \true) { |
| 46 | \update_option('iawp_is_migrating', '0', \true); |
| 47 | \delete_option('iawp_last_finished_migration_step'); |
| 48 | \delete_option('iawp_migration_error'); |
| 49 | \delete_option('iawp_migration_error_query'); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | /** |
| 54 | * is_migrating is serving multiple purposes. It's also being used to stop ajax requests and dashboard |
| 55 | * widgets from running when the database version is newer than one that comes with the installed version |
| 56 | * of independent analytics. The probably should be a method called something `database_ready` that serves |
| 57 | * this purpose more explicitly. |
| 58 | * |
| 59 | * @return bool |
| 60 | */ |
| 61 | public static function is_migrating() : bool |
| 62 | { |
| 63 | $db_version = \get_option('iawp_db_version', '0'); |
| 64 | $is_migrating = \get_option('iawp_is_migrating') === '1'; |
| 65 | $is_current = \version_compare($db_version, '36', '='); |
| 66 | $is_outdated = !$is_current; |
| 67 | return $is_outdated || $is_migrating; |
| 68 | } |
| 69 | public static function is_database_ahead_of_plugin() : bool |
| 70 | { |
| 71 | $db_version = \get_option('iawp_db_version', '0'); |
| 72 | return \version_compare($db_version, '36', '>'); |
| 73 | } |
| 74 | public static function is_actually_migrating() : bool |
| 75 | { |
| 76 | return \get_option('iawp_is_migrating') === '1'; |
| 77 | } |
| 78 | /** |
| 79 | * @return bool |
| 80 | */ |
| 81 | public static function should_migrate() : bool |
| 82 | { |
| 83 | $db_version = \get_option('iawp_db_version', '0'); |
| 84 | $is_migrating = \get_option('iawp_is_migrating') === '1'; |
| 85 | $is_current = \version_compare($db_version, '36', '='); |
| 86 | $is_outdated = !$is_current; |
| 87 | return $is_outdated && !$is_migrating; |
| 88 | } |
| 89 | public static function handle_migration_18_error() : void |
| 90 | { |
| 91 | $directory = \trailingslashit(\wp_upload_dir()['basedir']) . 'iawp/'; |
| 92 | $db_version = \get_option('iawp_db_version', '0'); |
| 93 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 94 | if ($db_version === '17' && $is_migrating && \is_dir($directory)) { |
| 95 | \update_option('iawp_db_version', '18', \true); |
| 96 | \update_option('iawp_is_migrating', '0', \true); |
| 97 | \delete_option('iawp_last_finished_migration_step'); |
| 98 | \delete_option('iawp_migration_error'); |
| 99 | \delete_option('iawp_migration_error_query'); |
| 100 | try { |
| 101 | $directory = \trailingslashit(\wp_upload_dir()['basedir']) . 'iawp/'; |
| 102 | Dir::delete($directory); |
| 103 | } catch (\Throwable $e) { |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | public static function handle_migration_22_error() : void |
| 108 | { |
| 109 | $db_version = \get_option('iawp_db_version', '0'); |
| 110 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 111 | $last_finished_step = \get_option('iawp_last_finished_migration_step', '0'); |
| 112 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 113 | $referrers_table = Query::get_table_name(Query::REFERRERS); |
| 114 | $has_index = Database::has_index($referrers_table, 'referrers_domain_index'); |
| 115 | if ($db_version === '21' && $is_migrating && $last_finished_step === '0' && $has_error && !$has_index) { |
| 116 | \update_option('iawp_is_migrating', '0', \true); |
| 117 | \delete_option('iawp_last_finished_migration_step'); |
| 118 | \delete_option('iawp_migration_error'); |
| 119 | \delete_option('iawp_migration_error_query'); |
| 120 | } |
| 121 | } |
| 122 | public static function handle_migration_29_error() : void |
| 123 | { |
| 124 | global $wpdb; |
| 125 | $db_version = \get_option('iawp_db_version', '0'); |
| 126 | $is_migrating = \get_option('iawp_is_migrating', '0') === '1'; |
| 127 | $last_finished_step = \get_option('iawp_last_finished_migration_step', '0'); |
| 128 | $has_error = \get_option('iawp_migration_error_query', null) !== null && \get_option('iawp_migration_error', null) !== null; |
| 129 | if ($db_version === '28' && $is_migrating && $last_finished_step === '5' && $has_error) { |
| 130 | $sessions_table = Query::get_table_name(Query::SESSIONS); |
| 131 | $wpdb->query("ALTER TABLE {$sessions_table} DROP COLUMN visitor_id"); |
| 132 | $wpdb->query("ALTER TABLE {$sessions_table} CHANGE COLUMN old_visitor_id visitor_id varchar(32)"); |
| 133 | \delete_option('iawp_last_finished_migration_step'); |
| 134 | \delete_option('iawp_migration_error'); |
| 135 | \delete_option('iawp_migration_error_query'); |
| 136 | \update_option('iawp_is_migrating', '0', \true); |
| 137 | } |
| 138 | } |
| 139 | /** |
| 140 | * @param Step_Migration[] $migrations |
| 141 | * |
| 142 | * @return bool |
| 143 | */ |
| 144 | private static function run_step_migrations(array $migrations) : bool |
| 145 | { |
| 146 | foreach ($migrations as $migration) { |
| 147 | $completed = $migration->migrate(); |
| 148 | if (!$completed) { |
| 149 | return \false; |
| 150 | } |
| 151 | } |
| 152 | return \true; |
| 153 | } |
| 154 | } |
| 155 |