|
| 1 | +<?php |
| 2 | + |
| 3 | +use Bavix\Wallet\Models\Transfer; |
| 4 | +use Illuminate\Database\Migrations\Migration; |
| 5 | +use Illuminate\Database\MySqlConnection; |
| 6 | +use Illuminate\Database\PostgresConnection; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | + |
| 9 | +class AddExchangeStatusTransfersTable extends Migration |
| 10 | +{ |
| 11 | + |
| 12 | + /** |
| 13 | + * @return string |
| 14 | + */ |
| 15 | + protected function table(): string |
| 16 | + { |
| 17 | + return (new Transfer())->getTable(); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @return void |
| 22 | + */ |
| 23 | + public function up(): void |
| 24 | + { |
| 25 | + $enums = [ |
| 26 | + Transfer::STATUS_EXCHANGE, |
| 27 | + Transfer::STATUS_TRANSFER, |
| 28 | + Transfer::STATUS_PAID, |
| 29 | + Transfer::STATUS_REFUND, |
| 30 | + Transfer::STATUS_GIFT, |
| 31 | + ]; |
| 32 | + |
| 33 | + if (DB::connection() instanceof MySqlConnection) { |
| 34 | + $table = $this->table(); |
| 35 | + $enumString = implode('\', \'', $enums); |
| 36 | + $default = Transfer::STATUS_TRANSFER; |
| 37 | + DB::statement("ALTER TABLE $table CHANGE COLUMN status status ENUM('$enumString') NOT NULL DEFAULT '$default'"); |
| 38 | + DB::statement("ALTER TABLE $table CHANGE COLUMN status_last status_last ENUM('$enumString') NULL"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (DB::connection() instanceof PostgresConnection) { |
| 43 | + $this->alterEnum($this->table(), 'status', $enums); |
| 44 | + $this->alterEnum($this->table(), 'status_last', $enums); |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function down(): void |
| 53 | + { |
| 54 | + $enums = [ |
| 55 | + Transfer::STATUS_TRANSFER, |
| 56 | + Transfer::STATUS_PAID, |
| 57 | + Transfer::STATUS_REFUND, |
| 58 | + Transfer::STATUS_GIFT, |
| 59 | + ]; |
| 60 | + |
| 61 | + if (DB::connection() instanceof MySqlConnection) { |
| 62 | + $table = $this->table(); |
| 63 | + $enumString = implode('\', \'', $enums); |
| 64 | + $default = Transfer::STATUS_TRANSFER; |
| 65 | + DB::statement("ALTER TABLE $table CHANGE COLUMN status status ENUM('$enumString') NOT NULL DEFAULT '$default'"); |
| 66 | + DB::statement("ALTER TABLE $table CHANGE COLUMN status_last status_last ENUM('$enumString') NULL"); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (DB::connection() instanceof PostgresConnection) { |
| 71 | + $this->alterEnum($this->table(), 'status', $enums); |
| 72 | + $this->alterEnum($this->table(), 'status_last', $enums); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Alter an enum field constraints |
| 79 | + * @param $table |
| 80 | + * @param $field |
| 81 | + * @param array $options |
| 82 | + */ |
| 83 | + protected function alterEnum($table, $field, array $options): void |
| 84 | + { |
| 85 | + $check = "${table}_${field}_check"; |
| 86 | + |
| 87 | + $enumList = []; |
| 88 | + |
| 89 | + foreach ($options as $option) { |
| 90 | + $enumList[] = sprintf("'%s'::CHARACTER VARYING", $option); |
| 91 | + } |
| 92 | + |
| 93 | + $enumString = implode(', ', $enumList); |
| 94 | + |
| 95 | + DB::transaction(function () use ($table, $field, $check, $options, $enumString) { |
| 96 | + DB::statement(sprintf('ALTER TABLE %s DROP CONSTRAINT %s;', $table, $check)); |
| 97 | + DB::statement(sprintf('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s::TEXT = ANY (ARRAY[%s]::TEXT[]))', $table, $check, $field, $enumString)); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments