3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 11:15:42 +00:00

Added migration to add created_by to items table

This commit is contained in:
snipe
2026-08-05 15:25:19 +01:00
parent c4ad79149a
commit 5524ab482c

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Adds per-line authorship to order_items. Order.created_by records
* who opened the transaction, but a single Order can accumulate lines
* from multiple operators over time (Alice has Order-42 with two lines,
* Bob later replenishes under the same Order-42 with three more). Storing
* created_by on the OrderItem itself preserves that per-line context.
*
* Nullable so historical rows written before this migration back-fill
* from the parent Order.created_by, and so admins clearing themselves
* out (soft-delete workflow) don't cascade into false authorship.
*/
return new class extends Migration
{
public function up(): void
{
Schema::table('order_items', function (Blueprint $table) {
$table->unsignedBigInteger('created_by')->nullable()->after('price');
$table->index('created_by');
});
// Backfill existing rows from the parent Order.created_by so
// history-tab renders and reports don't show a null column for
// every pre-migration line.
DB::statement('
UPDATE order_items
JOIN orders ON orders.id = order_items.order_id
SET order_items.created_by = orders.created_by
WHERE order_items.created_by IS NULL
');
}
public function down(): void
{
Schema::table('order_items', function (Blueprint $table) {
$table->dropIndex(['created_by']);
$table->dropColumn('created_by');
});
}
};