From 77e45e0d0e72510b182f386bcbdc208f77a05ab4 Mon Sep 17 00:00:00 2001 From: Paulo Iankoski Date: Thu, 16 Jan 2025 16:15:48 -0300 Subject: [PATCH] Fix: Prevent duplicated column error in Event Tickets migration (#7677) --- .../AddAmountColumnToEventTicketsTable.php | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/EventTickets/Migrations/AddAmountColumnToEventTicketsTable.php b/src/EventTickets/Migrations/AddAmountColumnToEventTicketsTable.php index b9542636a7..7bdbe33091 100644 --- a/src/EventTickets/Migrations/AddAmountColumnToEventTicketsTable.php +++ b/src/EventTickets/Migrations/AddAmountColumnToEventTicketsTable.php @@ -9,30 +9,45 @@ /** * @unreleased */ -class AddAmountColumnToEventTicketsTable extends Migration { +class AddAmountColumnToEventTicketsTable extends Migration +{ /** * @inheritdoc + * + * @unreleased */ - public static function id() { + public static function id() + { return 'give-events-add-amount-column-to-events-tickets-table'; } - public static function title() { + /** + * @unreleased + */ + public static function title() + { return 'Add "amount" column to give_event_tickets table'; } /** * @inheritdoc + * + * @unreleased */ - public static function timestamp() { - return strtotime( '2022-03-18 12:00:00' ); + public static function timestamp() + { + return strtotime('2022-03-18 12:00:00'); } /** * @inheritdoc + * + * @unreleased + * * @throws DatabaseMigrationException */ - public function run() { + public function run() + { global $wpdb; $eventTicketsTable = $wpdb->give_event_tickets; @@ -43,23 +58,29 @@ public function run() { } /** + * @unreleased + * * @throws DatabaseMigrationException */ - private function addAmountColumn($wpdb, $eventTicketsTable) { + private function addAmountColumn($wpdb, $eventTicketsTable) + { $sql = "ALTER TABLE $eventTicketsTable ADD COLUMN amount INT UNSIGNED NOT NULL AFTER donation_id"; try { - $wpdb->query($sql); + maybe_add_column($eventTicketsTable, 'amount', $sql); } catch (DatabaseQueryException $exception) { - throw new DatabaseMigrationException( "An error occurred while adding the amount column to the $eventTicketsTable table", 0, $exception ); + throw new DatabaseMigrationException("An error occurred while adding the amount column to the $eventTicketsTable table", 0, $exception); } } /** + * @unreleased + * * @throws DatabaseMigrationException */ - private function migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable) { + private function migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable) + { $sql = "UPDATE $eventTicketsTable eventTickets JOIN $eventTicketTypesTable evenTicketTypes ON eventTickets.ticket_type_id = evenTicketTypes.id @@ -68,7 +89,7 @@ private function migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketType try { $wpdb->query($sql); } catch (DatabaseQueryException $exception) { - throw new DatabaseMigrationException( "An error occurred while migrating data to the amount column in the $eventTicketsTable table", 0, $exception ); + throw new DatabaseMigrationException("An error occurred while migrating data to the amount column in the $eventTicketsTable table", 0, $exception); } } };