31+32 - Fix entries migration: nullable unit_price/total_cost, consolidate rename migration into source
This commit is contained in:
parent
cdb1e268e4
commit
195e316da5
3 changed files with 30 additions and 83 deletions
|
|
@ -1,33 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
Schema::create('purchases', function (Blueprint $table) {
|
|
||||||
$table->id();
|
|
||||||
$table->date('date');
|
|
||||||
$table->decimal('shares', 12, 6); // Supports fractional shares
|
|
||||||
$table->decimal('price_per_share', 8, 4); // Price in euros
|
|
||||||
$table->decimal('total_cost', 12, 2); // Total cost in euros
|
|
||||||
$table->timestamps();
|
|
||||||
|
|
||||||
$table->index('date');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::dropIfExists('purchases');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('entries', function (Blueprint $table): void {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('tracker_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->date('date');
|
||||||
|
$table->decimal('quantity', 12, 6);
|
||||||
|
$table->decimal('unit_price', 12, 4)->nullable();
|
||||||
|
$table->decimal('total_cost', 12, 2)->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->index(['tracker_id', 'date']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('entries');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1,50 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
// Rename table
|
|
||||||
Schema::rename('purchases', 'entries');
|
|
||||||
|
|
||||||
Schema::table('entries', function (Blueprint $table) {
|
|
||||||
// Rename columns
|
|
||||||
$table->renameColumn('shares', 'quantity');
|
|
||||||
$table->renameColumn('price_per_share', 'unit_price');
|
|
||||||
|
|
||||||
// Add tracker_id FK (nullable first so we can backfill)
|
|
||||||
$table->foreignId('tracker_id')->nullable()->after('id')->constrained()->cascadeOnDelete();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Backfill tracker_id: assign all entries to the first tracker (single-user app)
|
|
||||||
$trackerId = DB::table('trackers')->value('id');
|
|
||||||
if ($trackerId) {
|
|
||||||
DB::table('entries')->update(['tracker_id' => $trackerId]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make tracker_id non-nullable now that it's backfilled
|
|
||||||
Schema::table('entries', function (Blueprint $table) {
|
|
||||||
$table->unsignedBigInteger('tracker_id')->nullable(false)->change();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::table('entries', function (Blueprint $table) {
|
|
||||||
$table->dropForeign(['tracker_id']);
|
|
||||||
$table->dropColumn('tracker_id');
|
|
||||||
});
|
|
||||||
|
|
||||||
Schema::table('entries', function (Blueprint $table) {
|
|
||||||
$table->renameColumn('unit_price', 'price_per_share');
|
|
||||||
$table->renameColumn('quantity', 'shares');
|
|
||||||
});
|
|
||||||
|
|
||||||
Schema::rename('entries', 'purchases');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Loading…
Reference in a new issue