| @@ -7,9 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SeQura\WC\Repositories\Migrations; |
| 10 | 10 | |
| 11 | -use SeQura\WC\Core\Extension\Infrastructure\Configuration\Configuration; | |
| 11 | +use SeQura\WC\Repositories\Interface_Cache_Repository; | |
| 12 | +use SeQura\WC\Repositories\Repository; | |
| 12 | 13 | |
| 13 | 14 | /** |
| 14 | 15 | * Database migration interface |
| 15 | 16 | */ |
| @@ -22,22 +23,23 @@ | ||
| 22 | 23 | */ |
| 23 | 24 | protected $db; |
| 24 | 25 | |
| 25 | 26 | /** |
| 26 | - * Configuration service. | |
| 27 | + * Cache repository. | |
| 27 | 28 | * |
| 28 | - * @var Configuration | |
| 29 | + * @var Interface_Cache_Repository | |
| 29 | 30 | */ |
| 30 | - protected $configuration; | |
| 31 | + protected $cache; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * Constructor |
| 34 | 35 | * |
| 35 | - * @param \wpdb $wpdb Database instance. | |
| 36 | + * @param \wpdb $wpdb Database instance. | |
| 37 | + * @param Interface_Cache_Repository $cache Cache repository. | |
| 36 | 38 | */ |
| 37 | - public function __construct( \wpdb $wpdb, Configuration $configuration ) { | |
| 38 | - $this->db = $wpdb; | |
| 39 | - $this->configuration = $configuration; | |
| 39 | + public function __construct( \wpdb $wpdb, Interface_Cache_Repository $cache ) { | |
| 40 | + $this->db = $wpdb; | |
| 41 | + $this->cache = $cache; | |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | /** |
| 43 | 45 | * Get the plugin version when the changes were made. |
| @@ -44,10 +46,42 @@ | ||
| 44 | 46 | */ |
| 45 | 47 | abstract public function get_version(): string; |
| 46 | 48 | |
| 47 | 49 | /** |
| 48 | - * Run the migration. | |
| 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. | |
| 49 | 55 | * |
| 50 | 56 | * @throws \Throwable |
| 51 | 57 | */ |
| 52 | - abstract public function run(): void; | |
| 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; | |
| 53 | 87 | } |