Skip to content

A free open source IT asset/license management system

Updated backfill migrations

snipec573765d2138
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 22a00d43cd..7fe80c0d9a 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
@@ -5,22 +5,32 @@ use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
- * Purchase-order data model. `orders` is one row per real-world PO (a
- * supplier + date + optional PO number). `order_items` is the line-items
- * on that PO, polymorphic to the inventory model so a single PO can carry
- * accessories, consumables, components, assets, and license seats.
+ * Acquisition-transaction data model. `orders` is one row per
+ * transaction (a supplier, a date, an optional supplier-issued
+ * `order_number`, a currency). `order_items` is the polymorphic line
+ * items on that transaction so a single acquisition can carry
+ * accessories, consumables, components, and assets side by side.
*
* Replaces the parent-level `order_number` column that used to live on
* each inventory table. The old column misrepresented current state on
- * models that get replenished across many POs (accessories / consumables
- * / components); consolidating into a dedicated Order model + polymorphic
- * line-items lets each purchase event be recorded independently, and
- * lets action_logs point at the Order (per-event PO context) rather than
- * carrying a raw string.
+ * models that get replenished across many transactions (accessories,
+ * consumables, components). The dedicated Order model + polymorphic
+ * line items records each transaction independently and lets
+ * action_logs point at the Order for per-event acquisition context
+ * rather than carrying a raw string.
*
- * No UI or permission model is included yet — this migration only handles
- * the storage. Rows arrive via the adjust-quantity flow, the importer,
- * and the backfill migrations that follow this one.
+ * Explicitly NOT a purchase-order workflow (no state machine, no
+ * approvals, no receiving). Just the "what was acquired, when, from
+ * whom" record that the adjust-quantity flow, the AssetObserver, and
+ * the CSV importers write to.
+ *
+ * Licenses are intentionally out of scope for this migration —
+ * per-seat product-key semantics need their own design pass before
+ * the License model can join the Orders flow cleanly.
+ *
+ * No UI or permission model is included yet. Rows arrive via the
+ * adjust-quantity flow, the importer, and the backfill migrations that
+ * follow this one.
*/
return new class extends Migration
{
@@ -82,22 +92,27 @@ return new class extends Migration
$table->index(['item_type', 'item_id']);
});
- // FK from every action_log row that references an Order (today
- // that's just QuantityAdjust events; other action types can
- // adopt it as they grow acquisition semantics). Plain bigint +
- // index — no DB-level FK constraint per Snipe-IT convention
- // (schema shifts often enough that constraints cause churn).
+ // FK from every action_log row that references a specific
+ // acquisition line (today that's just QuantityAdjust events;
+ // other action types can adopt it as they grow acquisition
+ // semantics). Points at order_items, not orders — a single
+ // Order deduped across staggered receipts under one order_number
+ // carries multiple lines, and the log entry has to reach the
+ // exact line for its event, not just the shared Order header.
+ // Plain bigint + index, no DB-level FK constraint per Snipe-IT
+ // convention (schema shifts often enough that constraints cause
+ // churn).
Schema::table('action_logs', function (Blueprint $table) {
- $table->unsignedBigInteger('order_id')->nullable()->after('quantity');
- $table->index('order_id');
+ $table->unsignedBigInteger('order_item_id')->nullable()->after('quantity');
+ $table->index('order_item_id');
});
}
public function down(): void
{
Schema::table('action_logs', function (Blueprint $table) {
- $table->dropIndex(['order_id']);
- $table->dropColumn('order_id');
+ $table->dropIndex(['order_item_id']);
+ $table->dropColumn('order_item_id');
});
Schema::dropIfExists('order_items');
Schema::dropIfExists('orders');
diff --git a/database/migrations/2026_08_03_142000_backfill_orders_from_inventory_tables.php b/database/migrations/2026_08_03_142000_backfill_orders_from_inventory_tables.php
index 74a64dc58d..9d2a46759a 100644
--- a/database/migrations/2026_08_03_142000_backfill_orders_from_inventory_tables.php
+++ b/database/migrations/2026_08_03_142000_backfill_orders_from_inventory_tables.php
@@ -6,11 +6,14 @@ use Illuminate\Support\Facades\Schema;
/**
* Backfill the Orders / OrderItems tables from the pre-existing
- * order_number columns on accessories, consumables, components, assets,
- * and licenses. One Order row is created per unique
- * (order_number, supplier_id, company_id) tuple across all five tables,
- * and each source inventory row becomes an OrderItem line under that
- * Order.
+ * order_number columns on accessories, consumables, components, and
+ * assets. One Order row is created per unique (order_number,
+ * supplier_id, company_id) tuple across all four tables, and each
+ * source inventory row becomes an OrderItem line under that Order.
+ *
+ * Licenses are intentionally out of scope. Per-seat product-key
+ * semantics need their own design pass before License can join the
+ * Orders flow cleanly.
*
* The dedupe key is intentionally (order_number, supplier_id, company_id)
* rather than just order_number: a raw string like "PO-42" can legitimately
@@ -21,8 +24,8 @@ use Illuminate\Support\Facades\Schema;
* inventory rows never associated with any order, and there's nothing
* for the Orders table to record.
*
- * The follow-up migration (`drop_order_number_from_inventory_tables`)
- * removes the source columns once this backfill has run.
+ * The follow-up rename migration (`2026_08_03_143000_...`) renames the
+ * source columns to `legacy_*` names once this backfill has run.
*/
return new class extends Migration
{
@@ -37,7 +40,6 @@ return new class extends Migration
\App\Models\Consumable::class => 'consumables',
\App\Models\Component::class => 'components',
\App\Models\Asset::class => 'assets',
- \App\Models\License::class => 'licenses',
];
public function up(): void
@@ -65,8 +67,7 @@ return new class extends Migration
continue;
}
- $qtyColumn = ($table === 'licenses') ? 'seats' : 'qty';
- $hasQtyColumn = Schema::hasColumn($table, $qtyColumn);
+ $hasQtyColumn = Schema::hasColumn($table, 'qty');
DB::table($table)
->select([
@@ -82,14 +83,15 @@ return new class extends Migration
->whereNotNull('order_number')
->where('order_number', '!=', '')
->orderBy('id')
- ->chunkById(500, function ($rows) use ($modelClass, $table, $hasQtyColumn, $qtyColumn, &$orderIndex) {
- // Chunked pull grabs qty separately since not every
- // source table has the same qty-column name and we
- // want the primary select above to stay portable.
+ ->chunkById(500, function ($rows) use ($modelClass, $table, $hasQtyColumn, &$orderIndex) {
+ // Chunked pull grabs qty separately since assets
+ // don't have a qty column (1:1 with the asset row)
+ // and the primary select stays portable across the
+ // four source tables.
$qtyMap = $hasQtyColumn
? DB::table($table)
->whereIn('id', $rows->pluck('id'))
- ->pluck($qtyColumn, 'id')
+ ->pluck('qty', 'id')
->all()
: [];