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