Skip to content

Commit

Permalink
fix UpdateStatusTransfersTable
Browse files Browse the repository at this point in the history
  • Loading branch information
Babichev Maxim committed May 13, 2019
1 parent 72e136b commit 56ba90f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 72 deletions.
9 changes: 7 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.3.2] - 2019-05-13
### Fixed
- patch migrations

## [2.3.1] - 2019-05-13
### Added
- Added require dependency doctrine/dbal in composer.json
Expand Down Expand Up @@ -216,8 +220,9 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/2.3.1...HEAD
[2.3.0]: https://github.com/bavix/laravel-wallet/compare/2.3.0...2.3.1
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/2.3.2...HEAD
[2.3.2]: https://github.com/bavix/laravel-wallet/compare/2.3.1...2.3.2
[2.3.1]: https://github.com/bavix/laravel-wallet/compare/2.3.0...2.3.1
[2.3.0]: https://github.com/bavix/laravel-wallet/compare/2.2.2...2.3.0
[2.2.2]: https://github.com/bavix/laravel-wallet/compare/2.2.1...2.2.2
[2.2.1]: https://github.com/bavix/laravel-wallet/compare/2.2.0...2.2.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,36 @@ protected function table(): string
*/
public function up(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->renameColumn('status', 'tmpStatus');
});

Schema::table($this->table(), function (Blueprint $table) {
$table->renameColumn('status_last', 'tmpStatusLast');
});

Schema::table($this->table(), function (Blueprint $table) {
$enums = [
Transfer::STATUS_TRANSFER,
Transfer::STATUS_PAID,
Transfer::STATUS_REFUND,
Transfer::STATUS_GIFT,
];

$table->enum('status', $enums)
->default(Transfer::STATUS_TRANSFER);

$table->enum('status_last', $enums)
->nullable();
});

DB::table($this->table())
->update([
'status' => DB::raw('tmpStatus'),
'status_last' => DB::raw('tmpStatusLast'),
]);

Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('tmpStatus');
});

Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('tmpStatusLast');
$enums = [
Transfer::STATUS_TRANSFER,
Transfer::STATUS_PAID,
Transfer::STATUS_REFUND,
Transfer::STATUS_GIFT,
];

if (DB::connection() instanceof \Illuminate\Database\MySqlConnection) {
$table = $this->table();
$enumString = \implode('\', \'', $enums);
$default = Transfer::STATUS_TRANSFER;
DB::statement("ALTER TABLE $table CHANGE COLUMN status status ENUM('$enumString') NOT NULL DEFAULT '$default'");
DB::statement("ALTER TABLE $table CHANGE COLUMN status_last status_last ENUM('$enumString') NULL");
return;
}

if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) {
$this->alterEnum($this->table(), 'status', $enums);
$this->alterEnum($this->table(), 'status_last', $enums);
return;
}

Schema::table($this->table(), function (Blueprint $table) use ($enums) {
$table->string('status')
->default(Transfer::STATUS_TRANSFER)
->change();

$table->string('status_last')
->nullable()
->change();
});
}

Expand All @@ -65,48 +60,67 @@ public function up(): void
*/
public function down(): void
{
Schema::table($this->table(), function (Blueprint $table) {
$table->renameColumn('status', 'tmpStatus');
});

Schema::table($this->table(), function (Blueprint $table) {
$table->renameColumn('status_last', 'tmpStatusLast');
});

Schema::table($this->table(), function (Blueprint $table) {
$enums = [
Transfer::STATUS_PAID,
Transfer::STATUS_REFUND,
Transfer::STATUS_GIFT,
];

$table->enum('status', $enums)
->default(Transfer::STATUS_PAID);
DB::table($this->table())
->where('status', Transfer::STATUS_TRANSFER)
->update(['status' => Transfer::STATUS_PAID]);

$table->enum('status_last', $enums)
->nullable();
DB::table($this->table())
->where('status_last', Transfer::STATUS_TRANSFER)
->update(['status_last' => Transfer::STATUS_PAID]);

$enums = [
Transfer::STATUS_PAID,
Transfer::STATUS_REFUND,
Transfer::STATUS_GIFT,
];

if (DB::connection() instanceof \Illuminate\Database\MySqlConnection) {
$table = $this->table();
$enumString = \implode('\', \'', $enums);
$default = Transfer::STATUS_PAID;
DB::statement("ALTER TABLE $table CHANGE COLUMN status status ENUM('$enumString') NOT NULL DEFAULT '$default'");
DB::statement("ALTER TABLE $table CHANGE COLUMN status_last status_last ENUM('$enumString') NULL");
return;
}

if (DB::connection() instanceof \Illuminate\Database\PostgresConnection) {
$this->alterEnum($this->table(), 'status', $enums);
$this->alterEnum($this->table(), 'status_last', $enums);
return;
}

Schema::table($this->table(), function (Blueprint $table) use ($enums) {
$table->string('status')
->default(Transfer::STATUS_PAID)
->change();

$table->string('status_last')
->nullable()
->change();
});
}

DB::table($this->table())
->where('tmpStatus', Transfer::STATUS_TRANSFER)
->update(['tmpStatus' => Transfer::STATUS_PAID]);
/**
* Alter an enum field constraints
* @param $table
* @param $field
* @param array $options
*/
protected function alterEnum($table, $field, array $options): void
{
$check = "${table}_${field}_check";

DB::table($this->table())
->where('tmpStatusLast', Transfer::STATUS_TRANSFER)
->update(['tmpStatusLast' => Transfer::STATUS_PAID]);
$enumList = [];

DB::table($this->table())
->update([
'status' => DB::raw('tmpStatus'),
'status_last' => DB::raw('tmpStatusLast'),
]);
foreach($options as $option) {
$enumList[] = sprintf("'%s'::CHARACTER VARYING", $option);
}

Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('tmpStatus');
});
$enumString = implode(', ', $enumList);

Schema::table($this->table(), function (Blueprint $table) {
$table->dropColumn('tmpStatusLast');
DB::transaction(function () use ($table, $field, $check, $options, $enumString) {
DB::statement(sprintf('ALTER TABLE %s DROP CONSTRAINT %s;', $table, $check));
DB::statement(sprintf('ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s::TEXT = ANY (ARRAY[%s]::TEXT[]))', $table, $check, $field, $enumString));
});
}

Expand Down

0 comments on commit 56ba90f

Please sign in to comment.