Skip to content

A free open source IT asset/license management system

Fixed migration

snipede6e19db329e
diff --git a/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php b/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php
index 7fe80c0d9a..004b9a7118 100644
--- a/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php
+++ b/database/migrations/2026_08_03_141000_create_orders_and_order_items_tables.php
@@ -80,6 +80,15 @@ return new class extends Migration
// currencies and low-per-unit fractional prices without
// rounding drift on aggregate totals.
$table->decimal('price', 20, 4)->nullable();
+ // Per-line authorship. Order.created_by records who opened
+ // the transaction, but a single Order can accumulate lines
+ // from multiple operators over time (Alice writes two lines
+ // under Order-42, Bob replenishes with three more under the
+ // same Order-42 later). Storing created_by on the OrderItem
+ // itself preserves that per-line context. Nullable so
+ // backfill rows written from parent inventory tables
+ // without a captured actor don't fail.
+ $table->unsignedBigInteger('created_by')->nullable();
$table->timestamps();
// SoftDeletes so a force-delete on a referenced inventory
// row (Accessory / Consumable / Component / Asset / License)
@@ -90,6 +99,7 @@ return new class extends Migration
$table->index('order_id');
$table->index(['item_type', 'item_id']);
+ $table->index('created_by');
});
// FK from every action_log row that references a specific
diff --git a/database/migrations/2026_08_03_146000_add_created_by_to_order_items.php b/database/migrations/2026_08_03_146000_add_created_by_to_order_items.php
deleted file mode 100644
index 65b7643828..0000000000
--- a/database/migrations/2026_08_03_146000_add_created_by_to_order_items.php
+++ /dev/null
@@ -1,51 +0,0 @@
-<?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. Subquery form is portable across
- // MariaDB (prod) and SQLite (tests) — UPDATE...JOIN differs
- // between the two.
- DB::statement('
- UPDATE order_items
- SET created_by = (
- SELECT orders.created_by
- FROM orders
- WHERE orders.id = order_items.order_id
- )
- WHERE created_by IS NULL
- ');
- }
-
- public function down(): void
- {
- Schema::table('order_items', function (Blueprint $table) {
- $table->dropIndex(['created_by']);
- $table->dropColumn('created_by');
- });
- }
-};