Tables
1 year ago
GeneralMigration.php
1 year ago
MigrationsServiceProvider.php
1 year ago
ProductPageMigrationService.php
1 year ago
RewriteRulesMigrationService.php
1 year ago
Table.php
3 years ago
UpdateMigrationServiceProvider.php
2 years ago
UserMetaMigrationsService.php
3 years ago
VersionMigration.php
1 year ago
WebhookMigrationsService.php
1 year ago
VersionMigration.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Database; |
| 4 | |
| 5 | /** |
| 6 | * A migration that will run each time the version of the plugin changes. |
| 7 | */ |
| 8 | abstract class VersionMigration { |
| 9 | /** |
| 10 | * The key for the migration. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $migration_key = 'surecart_migration_version'; |
| 15 | |
| 16 | /** |
| 17 | * Run on init. |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | public function bootstrap() { |
| 22 | add_action( 'admin_init', [ $this, 'maybeRun' ] ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Get the current version of the plugin. |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public function currentVersion() { |
| 31 | return \SureCart::plugin()->version(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Maybe let's run the migration. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function maybeRun() { |
| 40 | if ( ! $this->shouldMigrate() ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // run the migration. |
| 45 | $this->run(); |
| 46 | |
| 47 | // update the migration complete on admin_init complete, after all migrations have run. |
| 48 | add_action( 'admin_init', [ $this, 'complete' ], 999999 ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Should we run this migration? |
| 53 | * |
| 54 | * @return boolean |
| 55 | */ |
| 56 | public function shouldMigrate(): bool { |
| 57 | // check if we already have done this migration. |
| 58 | return version_compare( $this->currentVersion(), $this->getLastMigrationVersion(), '!=' ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Run the migration |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | protected function run() { |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Store the current plugin version when complete. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function complete() { |
| 75 | update_option( $this->migration_key, $this->currentVersion(), false ); // don't autoload since we are only doing this on the admin. |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the last version there was a migration. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function getLastMigrationVersion() { |
| 84 | return get_option( $this->migration_key, '0.0.0' ); |
| 85 | } |
| 86 | } |
| 87 |