AddPastDonationsToRevenueTable.php
5 years ago
CreateRevenueTable.php
5 years ago
RemoveRevenueForeignKeys.php
5 years ago
RemoveRevenueForeignKeys.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Revenue\Migrations; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\Migrations\Contracts\Migration; |
| 7 | |
| 8 | /** |
| 9 | * Class RemoveRevenueForeignKeys |
| 10 | * |
| 11 | * @since 2.9.6 |
| 12 | * @package Give\Revenue\Migrations |
| 13 | */ |
| 14 | class RemoveRevenueForeignKeys extends Migration { |
| 15 | /** |
| 16 | * @inheritDoc |
| 17 | */ |
| 18 | public static function id() { |
| 19 | return 'remove_revenue_foreign_keys'; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @inheritDoc |
| 24 | */ |
| 25 | public function run() { |
| 26 | global $wpdb; |
| 27 | |
| 28 | $this->dropColumnForeignKeyConstraint( $wpdb->give_revenue, 'form_id' ); |
| 29 | $this->dropColumnForeignKeyConstraint( $wpdb->give_revenue, 'donation_id' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @inheritDoc |
| 34 | */ |
| 35 | public static function timestamp() { |
| 36 | return strtotime( '01-05-2020 12:45:00' ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Drops the foreign key constraint for a given table and column |
| 41 | * |
| 42 | * @since 2.9.6 |
| 43 | * |
| 44 | * @param string $table |
| 45 | * @param string $column |
| 46 | */ |
| 47 | private function dropColumnForeignKeyConstraint( $table, $column ) { |
| 48 | $constraintName = DB::get_var( |
| 49 | DB::prepare( |
| 50 | " |
| 51 | SELECT constraints.CONSTRAINT_NAME |
| 52 | FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as constraints |
| 53 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE as column_usage |
| 54 | ON constraints.CONSTRAINT_NAME = column_usage.CONSTRAINT_NAME |
| 55 | WHERE constraints.CONSTRAINT_TYPE = 'FOREIGN KEY' |
| 56 | AND constraints.TABLE_NAME = %s |
| 57 | AND column_usage.COLUMN_NAME = %s |
| 58 | ", |
| 59 | $table, |
| 60 | $column |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | if ( ! empty( $constraintName ) ) { |
| 65 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 66 | DB::query( "ALTER TABLE {$table} DROP FOREIGN KEY {$constraintName}" ); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |