| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\InstallActions\Migrations; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 |
use IvyForms\Services\InstallActions\DB\Notification\NotificationsTable; |
| 12 |
use IvyForms\Services\Translations\BackendStrings; |
| 13 |
|
| 14 |
class AddReplyToToNotifications |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Run the migration |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
* @throws InvalidArgumentException |
| 21 |
*/ |
| 22 |
public static function run(): bool |
| 23 |
{ |
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
$tableName = NotificationsTable::getTableName(); |
| 27 |
|
| 28 |
// Check if replyTo column already exists |
| 29 |
$columnExists = $wpdb->get_results( |
| 30 |
"SHOW COLUMNS FROM `{$tableName}` LIKE 'replyTo'" |
| 31 |
); |
| 32 |
|
| 33 |
if (!empty($columnExists)) { |
| 34 |
return true; // Already migrated |
| 35 |
} |
| 36 |
|
| 37 |
// Add replyTo column |
| 38 |
$alterQuery = "ALTER TABLE `{$tableName}` ADD COLUMN `replyTo` VARCHAR(255) NULL AFTER `sender`"; |
| 39 |
|
| 40 |
$result = $wpdb->query($alterQuery); |
| 41 |
|
| 42 |
if ($result === false) { |
| 43 |
error_log(sprintf( |
| 44 |
BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 45 |
'replyTo', |
| 46 |
$tableName |
| 47 |
)); |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
public static function rollback(): bool |
| 55 |
{ |
| 56 |
global $wpdb; |
| 57 |
|
| 58 |
$tableName = NotificationsTable::getTableName(); |
| 59 |
|
| 60 |
$alterQuery = "ALTER TABLE `{$tableName}` DROP COLUMN `replyTo`"; |
| 61 |
|
| 62 |
$result = $wpdb->query($alterQuery); |
| 63 |
|
| 64 |
return $result !== false; |
| 65 |
} |
| 66 |
} |
| 67 |
|