Migration.php
3 years ago
Migration_1_0.php
3 years ago
Migration_1_6.php
3 years ago
Migration_1_8.php
3 years ago
Migration_1_9.php
3 years ago
Migration_2.php
3 years ago
Migration_3.php
3 years ago
Migration_4.php
3 years ago
Migration_5.php
3 years ago
Migration_6.php
3 years ago
Migration_7.php
3 years ago
Migration_8.php
3 years ago
Migration_Job.php
3 years ago
Migration.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Migrations; |
| 4 | |
| 5 | abstract class Migration |
| 6 | { |
| 7 | /** |
| 8 | * Classes that extend must provide their database version |
| 9 | * |
| 10 | * @var string |
| 11 | */ |
| 12 | protected $database_version = '0'; |
| 13 | |
| 14 | final public function __construct() |
| 15 | { |
| 16 | $this->migrate(); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Trigger the migration |
| 21 | * |
| 22 | * @return void |
| 23 | */ |
| 24 | final protected function migrate(): void |
| 25 | { |
| 26 | $db_version = get_option('iawp_db_version', '0'); |
| 27 | |
| 28 | if (version_compare($db_version, $this->database_version, '<')) { |
| 29 | // Trigger the classes' migration function |
| 30 | $this->handle(); |
| 31 | update_option('iawp_db_version', $this->database_version); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Classes that extend must define a handle method where migration work is done |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | abstract protected function handle(): void; |
| 41 | |
| 42 | /** |
| 43 | * @return void |
| 44 | */ |
| 45 | public static function create_or_migrate(): void |
| 46 | { |
| 47 | if (self::should_migrate()) { |
| 48 | update_option('iawp_is_migrating', '1'); |
| 49 | |
| 50 | new Migration_1_0(); |
| 51 | new Migration_1_6(); |
| 52 | new Migration_1_8(); |
| 53 | new Migration_1_9(); |
| 54 | new Migration_2(); |
| 55 | new Migration_3(); |
| 56 | new Migration_4(); |
| 57 | new Migration_5(); |
| 58 | new Migration_6(); |
| 59 | new Migration_7(); |
| 60 | new Migration_8(); |
| 61 | |
| 62 | delete_option('iawp_is_migrating'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function is_migrating(): bool |
| 70 | { |
| 71 | $db_version = get_option('iawp_db_version', '0'); |
| 72 | $is_migrating = get_option('iawp_is_migrating') === '1'; |
| 73 | $is_current = version_compare($db_version, '8', '='); |
| 74 | $is_outdated = !$is_current; |
| 75 | |
| 76 | return $is_outdated || $is_migrating; |
| 77 | } |
| 78 | |
| 79 | public static function is_actually_migrating(): bool |
| 80 | { |
| 81 | return get_option('iawp_is_migrating') === '1'; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return bool |
| 86 | */ |
| 87 | public static function should_migrate(): bool |
| 88 | { |
| 89 | $db_version = get_option('iawp_db_version', '0'); |
| 90 | $is_migrating = get_option('iawp_is_migrating') === '1'; |
| 91 | $is_current = version_compare($db_version, '8', '='); |
| 92 | $is_outdated = !$is_current; |
| 93 | |
| 94 | return $is_outdated && !$is_migrating; |
| 95 | } |
| 96 | } |
| 97 |