AddAmountColumnToEventTicketsTable.php
1 year ago
CreateEventTicketTypesTable.php
2 years ago
CreateEventTicketsTable.php
2 years ago
CreateEventsTable.php
2 years ago
AddAmountColumnToEventTicketsTable.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\EventTickets\Migrations; |
| 4 | |
| 5 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 6 | use Give\Framework\Migrations\Contracts\Migration; |
| 7 | use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 8 | |
| 9 | /** |
| 10 | * @since 3.20.0 |
| 11 | */ |
| 12 | class AddAmountColumnToEventTicketsTable extends Migration |
| 13 | { |
| 14 | /** |
| 15 | * @inheritdoc |
| 16 | * |
| 17 | * @since 3.20.0 |
| 18 | */ |
| 19 | public static function id() |
| 20 | { |
| 21 | return 'give-events-add-amount-column-to-events-tickets-table'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @since 3.20.0 |
| 26 | */ |
| 27 | public static function title() |
| 28 | { |
| 29 | return 'Add "amount" column to give_event_tickets table'; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @inheritdoc |
| 34 | * |
| 35 | * @since 3.20.0 |
| 36 | */ |
| 37 | public static function timestamp() |
| 38 | { |
| 39 | return strtotime('2022-03-18 12:00:00'); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @inheritdoc |
| 44 | * |
| 45 | * @since 3.20.0 |
| 46 | * |
| 47 | * @throws DatabaseMigrationException |
| 48 | */ |
| 49 | public function run() |
| 50 | { |
| 51 | global $wpdb; |
| 52 | |
| 53 | $eventTicketsTable = $wpdb->give_event_tickets; |
| 54 | $eventTicketTypesTable = $wpdb->give_event_ticket_types; |
| 55 | |
| 56 | $this->addAmountColumn($wpdb, $eventTicketsTable); |
| 57 | $this->migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 3.20.0 |
| 62 | * |
| 63 | * @throws DatabaseMigrationException |
| 64 | */ |
| 65 | private function addAmountColumn($wpdb, $eventTicketsTable) |
| 66 | { |
| 67 | $sql = "ALTER TABLE $eventTicketsTable |
| 68 | ADD COLUMN amount INT UNSIGNED NOT NULL AFTER donation_id"; |
| 69 | |
| 70 | try { |
| 71 | maybe_add_column($eventTicketsTable, 'amount', $sql); |
| 72 | } catch (DatabaseQueryException $exception) { |
| 73 | throw new DatabaseMigrationException("An error occurred while adding the amount column to the $eventTicketsTable table", 0, $exception); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 3.20.0 |
| 79 | * |
| 80 | * @throws DatabaseMigrationException |
| 81 | */ |
| 82 | private function migrateTicketPrices($wpdb, $eventTicketsTable, $eventTicketTypesTable) |
| 83 | { |
| 84 | $sql = "UPDATE $eventTicketsTable eventTickets |
| 85 | JOIN $eventTicketTypesTable evenTicketTypes |
| 86 | ON eventTickets.ticket_type_id = evenTicketTypes.id |
| 87 | SET eventTickets.amount = evenTicketTypes.price"; |
| 88 | |
| 89 | try { |
| 90 | $wpdb->query($sql); |
| 91 | } catch (DatabaseQueryException $exception) { |
| 92 | throw new DatabaseMigrationException("An error occurred while migrating data to the amount column in the $eventTicketsTable table", 0, $exception); |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 |