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