Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Last active August 26, 2020 22:07
Show Gist options
  • Save BinaryKitten/de3e61f2d2ef0f9993f8442fb8c9c811 to your computer and use it in GitHub Desktop.
Save BinaryKitten/de3e61f2d2ef0f9993f8442fb8c9c811 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemovePercentageCommissionFeesFromAuctionListings extends Migration
{
public function up(): void
{
Schema::table(
'auction_listings',
static function (Blueprint $table) {
$table->dropMoney('percentage_fee_amount');
}
);
}
public function down(): void
{
Schema::table(
'auction_listings',
static function (Blueprint $table) {
$table->money('percentage_fee_amount')->after('percentage_fee');
}
);
}
}
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class RemovePercentageCommissionFeesFromAuctionListings extends Migration
{
public function up(): void
{
Schema::table(
'auction_listings',
static function (Blueprint $table) {
$table->dropColumn('percentage_fee_amount');
$table->dropColumn('percentage_fee_currency');
}
);
}
public function down(): void
{
Schema::table(
'auction_listings',
static function (Blueprint $table) {
$table->unsignedInteger('percentage_fee_amount')->after('percentage_fee');
$table->string('percentage_fee_currency', 3)->default('GBP')->after('percentage_fee_amount');
}
);
}
}
<?php
declare(strict_types=1);
use Illuminate\Database\Schema\Blueprint;
Blueprint::macro(
'money',
function (string $amountColumnName = 'money', string $currencyColumnDefault = '', string $currencyColumnName = '') {
/** @var Blueprint $this */
if (!str_ends_with($amountColumnName, '_amount')) {
$amountColumnName .= '_amount';
}
if (empty($currencyColumnName)) {
$currencyColumnName = str_replace('_amount', '_currency', $amountColumnName);
} elseif (!str_ends_with($currencyColumnName, '_currency')) {
$currencyColumnName .= '_currency';
}
$amountColumn = $this->unsignedInteger($amountColumnName);
/*
* With `$this->string($currencyColumnName, 3)->default($currencyColumnDefault);` both migrations work in tests
*/
$this->string($currencyColumnName, 3)->default($currencyColumnDefault)->after($amountColumnName);
return $amountColumn;
}
);
Blueprint::macro(
'dropMoney',
function (string $amountColumnName = 'money', string $currencyColumnName = '') {
/** @var Blueprint $this */
if (!str_ends_with($amountColumnName, '_amount')) {
$amountColumnName .= '_amount';
}
if (empty($currencyColumnName)) {
$currencyColumnName = str_replace('_amount', '_currency', $amountColumnName);
} elseif (!str_ends_with($currencyColumnName, '_currency')) {
$currencyColumnName .= '_currency';
}
$this->dropColumn($currencyColumnName);
return $this->dropColumn($amountColumnName);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment