| 1 |
<?php |
| 2 |
/** |
| 3 |
* Database migration interface |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Repositories/Migrations |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Repositories\Migrations; |
| 10 |
|
| 11 |
use SeQura\WC\Repositories\Interface_Cache_Repository; |
| 12 |
use SeQura\WC\Repositories\Repository; |
| 13 |
|
| 14 |
/** |
| 15 |
* Database migration interface |
| 16 |
*/ |
| 17 |
abstract class Migration { |
| 18 |
|
| 19 |
/** |
| 20 |
* Database session object. |
| 21 |
* |
| 22 |
* @var \wpdb |
| 23 |
*/ |
| 24 |
protected $db; |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache repository. |
| 28 |
* |
| 29 |
* @var Interface_Cache_Repository |
| 30 |
*/ |
| 31 |
protected $cache; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
* |
| 36 |
* @param \wpdb $wpdb Database instance. |
| 37 |
* @param Interface_Cache_Repository $cache Cache repository. |
| 38 |
*/ |
| 39 |
public function __construct( \wpdb $wpdb, Interface_Cache_Repository $cache ) { |
| 40 |
$this->db = $wpdb; |
| 41 |
$this->cache = $cache; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the plugin version when the changes were made. |
| 46 |
*/ |
| 47 |
abstract public function get_version(): string; |
| 48 |
|
| 49 |
/** |
| 50 |
* Run the migration with the repository cache temporarily disabled. |
| 51 |
* |
| 52 |
* Migrations mix raw SQL operations (which bypass the Repository cache) with |
| 53 |
* AdminAPI saves (which go through the cached Repository). Disabling the cache |
| 54 |
* prevents stale cached reads from interfering with the migration process. |
| 55 |
* |
| 56 |
* @throws \Throwable |
| 57 |
*/ |
| 58 |
final public function run(): void { |
| 59 |
\add_filter( 'sequra_cache_enabled', array( $this, 'sequra_cache_enabled_callback' ), 999 ); |
| 60 |
Repository::$cache_enabled = null; |
| 61 |
|
| 62 |
try { |
| 63 |
$this->execute(); |
| 64 |
} finally { |
| 65 |
\remove_filter( 'sequra_cache_enabled', array( $this, 'sequra_cache_enabled_callback' ), 999 ); |
| 66 |
Repository::$cache_enabled = null; |
| 67 |
// Flush caches so the plugin reads fresh data after the migration. |
| 68 |
$this->cache->flush(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Disable the cache |
| 74 |
* |
| 75 |
* @return bool |
| 76 |
*/ |
| 77 |
public function sequra_cache_enabled_callback(): bool { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Execute the migration logic. |
| 83 |
* |
| 84 |
* @throws \Throwable |
| 85 |
*/ |
| 86 |
abstract protected function execute(): void; |
| 87 |
} |
| 88 |
|