| 1 |
<?php |
| 2 |
/** |
| 3 |
* Data Cleanup Migration for Easy Invoice |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration\Src; |
| 11 |
|
| 12 |
/** |
| 13 |
* Data Cleanup Migration Class |
| 14 |
* |
| 15 |
* Handles post-migration data management. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
class DataCleanupMigration extends AbstractMigration { |
| 20 |
|
| 21 |
/** |
| 22 |
* Migration description. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $description = 'Finalize migration (preserves old data)'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Check if cleanup is needed. |
| 31 |
* |
| 32 |
* @since 2.0.0 |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
public function is_needed(): bool { |
| 36 |
// Always return true as we need to finalize the migration |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Run the cleanup. |
| 42 |
* |
| 43 |
* @since 2.0.0 |
| 44 |
* @param array $options Cleanup options |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function migrate(array $options = []): array { |
| 48 |
$this->log('Finalizing migration (old data preserved)'); |
| 49 |
|
| 50 |
try { |
| 51 |
// Mark migration as completed |
| 52 |
update_option('easy_invoice_migration_completed', true); |
| 53 |
update_option('easy_invoice_migration_completed_time', current_time('mysql')); |
| 54 |
|
| 55 |
// Ensure version is bumped to target version when migration finalizes |
| 56 |
if (defined('EASY_INVOICE_VERSION')) { |
| 57 |
update_option('easy_invoice_version', EASY_INVOICE_VERSION); |
| 58 |
} |
| 59 |
|
| 60 |
// Store migration summary |
| 61 |
$summary = [ |
| 62 |
'completed_at' => current_time('mysql'), |
| 63 |
'old_data_preserved' => true, |
| 64 |
'version_migrated_from' => get_option('easy_invoice_version', '1.0.0'), |
| 65 |
'version_migrated_to' => defined('EASY_INVOICE_VERSION') ? EASY_INVOICE_VERSION : '2.0.0' |
| 66 |
]; |
| 67 |
update_option('easy_invoice_migration_summary', $summary); |
| 68 |
|
| 69 |
$this->log('Migration finalized successfully. Old data has been preserved.'); |
| 70 |
|
| 71 |
return [ |
| 72 |
'success' => true, |
| 73 |
'message' => 'Migration finalized successfully. Old data has been preserved.', |
| 74 |
'summary' => $summary |
| 75 |
]; |
| 76 |
|
| 77 |
} catch (\Exception $e) { |
| 78 |
$this->log('Failed to finalize migration: ' . $e->getMessage()); |
| 79 |
return [ |
| 80 |
'success' => false, |
| 81 |
'message' => 'Failed to finalize migration: ' . $e->getMessage() |
| 82 |
]; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|