osb/database/migrations/2022_04_22_144438_optimise_payments.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2022-04-22 05:23:08 +00:00
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE payments MODIFY account_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payments MODIFY checkout_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payments MODIFY source_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE payments MODIFY pending_status tinyint(1) DEFAULT NULL');
Schema::table('payments', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->datetime('created_at')->nullable()->after('id');
$table->datetime('updated_at')->nullable()->after('created_at');
$table->boolean('active')->nullable()->after('updated_at');
$table->date('paid_at')->nullable()->after('payment_date');
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
$table->foreign(['source_id','site_id'])->references(['id','site_id'])->on('users');
});
// Convert out dates
foreach (\App\Models\Payment::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->payment_date)
$o->paid_at = \Carbon\Carbon::createFromTimestamp($o->payment_date);
$o->save();
}
Schema::table('payments', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_last','payment_date']);
});
DB::statement('ALTER TABLE payment_items MODIFY payment_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payment_items MODIFY invoice_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE payment_items RENAME COLUMN alloc_amt TO amount');
Schema::table('payment_items', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->boolean('active')->nullable()->after('site_id');
$table->dropColumn(['date_orig','date_last']);
$table->foreign(['payment_id','site_id'])->references(['id','site_id'])->on('payments');
});
DB::statement('UPDATE payment_items SET active=1');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
};