fix(spec-079): allow non-UUID ids in inventory_links
This commit is contained in:
parent
d56ba85755
commit
b584cf2090
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$driver = DB::getDriverName();
|
||||
|
||||
if ($driver !== 'pgsql') {
|
||||
// SQLite doesn't enforce UUID types; other drivers are not supported in this app.
|
||||
return;
|
||||
}
|
||||
|
||||
DB::statement('ALTER TABLE inventory_links ALTER COLUMN source_id TYPE text USING source_id::text');
|
||||
DB::statement('ALTER TABLE inventory_links ALTER COLUMN target_id TYPE text USING target_id::text');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$driver = DB::getDriverName();
|
||||
|
||||
if ($driver !== 'pgsql') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Best-effort rollback: non-UUID identifiers are coerced.
|
||||
$uuidRegex = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$';
|
||||
$sentinel = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
DB::statement(
|
||||
"ALTER TABLE inventory_links ALTER COLUMN source_id TYPE uuid USING (CASE WHEN source_id ~* '{$uuidRegex}' THEN source_id::uuid ELSE '{$sentinel}'::uuid END)"
|
||||
);
|
||||
|
||||
DB::statement(
|
||||
"ALTER TABLE inventory_links ALTER COLUMN target_id TYPE uuid USING (CASE WHEN target_id IS NULL THEN NULL WHEN target_id ~* '{$uuidRegex}' THEN target_id::uuid ELSE NULL END)"
|
||||
);
|
||||
}
|
||||
};
|
||||
19
specs/079-inventory-links-non-uuid-ids/plan.md
Normal file
19
specs/079-inventory-links-non-uuid-ids/plan.md
Normal file
@ -0,0 +1,19 @@
|
||||
# Plan: Inventory links support non-UUID IDs
|
||||
|
||||
**Branch**: `079-inventory-links-non-uuid-ids`
|
||||
**Date**: 2026-02-07
|
||||
|
||||
## Approach
|
||||
|
||||
- Add a PostgreSQL migration to change `inventory_links.source_id` and `inventory_links.target_id` from `uuid` to `text`.
|
||||
- Add a pgsql-specific test that asserts the column types are `text` and that upserting an edge with a non-UUID `target_id` does not error.
|
||||
|
||||
## Safety
|
||||
|
||||
- Change is limited to `inventory_links` columns only.
|
||||
- Unique constraint and indexes continue to function on `text` columns.
|
||||
|
||||
## Testing
|
||||
|
||||
- Pest feature test under `tests/Feature/Inventory/`.
|
||||
- Run focused test + existing inventory extraction tests.
|
||||
22
specs/079-inventory-links-non-uuid-ids/spec.md
Normal file
22
specs/079-inventory-links-non-uuid-ids/spec.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Spec 079: Inventory links support non-UUID IDs
|
||||
|
||||
**Date**: 2026-02-07
|
||||
|
||||
## Problem
|
||||
|
||||
Inventory dependency extraction writes edges into `inventory_links`. Some Microsoft Graph / Intune identifiers (notably scope tag IDs) can be non-UUID strings (e.g. `"0"`). The current schema defines `inventory_links.source_id` and `inventory_links.target_id` as UUID columns, causing PostgreSQL failures when non-UUID identifiers are inserted.
|
||||
|
||||
## Goal
|
||||
|
||||
Allow storing non-UUID identifiers in `inventory_links` without crashing inventory sync/extraction.
|
||||
|
||||
## Requirements
|
||||
|
||||
- `inventory_links.source_id` and `inventory_links.target_id` must accept arbitrary string identifiers.
|
||||
- Existing UUID identifiers must continue to work.
|
||||
- Behavior must be covered by tests.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- No redesign of the dependency graph model.
|
||||
- No UI/Filament changes.
|
||||
18
specs/079-inventory-links-non-uuid-ids/tasks.md
Normal file
18
specs/079-inventory-links-non-uuid-ids/tasks.md
Normal file
@ -0,0 +1,18 @@
|
||||
# Tasks: Inventory links support non-UUID IDs
|
||||
|
||||
## Phase 1: Spec + setup
|
||||
|
||||
- [X] T001 Create spec folder and docs
|
||||
|
||||
## Phase 2: Tests (TDD)
|
||||
|
||||
- [X] T002 Add pgsql schema regression test in `tests/Feature/Inventory/InventoryLinksNonUuidIdsTest.php`
|
||||
|
||||
## Phase 3: Implementation
|
||||
|
||||
- [X] T003 Add migration to change `inventory_links.source_id` + `target_id` to `text` on PostgreSQL
|
||||
|
||||
## Phase 4: Validation
|
||||
|
||||
- [X] T004 Run tests: `vendor/bin/sail artisan test --compact tests/Feature/Inventory/InventoryLinksNonUuidIdsTest.php`
|
||||
- [X] T005 Run Pint: `vendor/bin/sail bin pint --dirty`
|
||||
46
tests/Feature/Inventory/InventoryLinksNonUuidIdsTest.php
Normal file
46
tests/Feature/Inventory/InventoryLinksNonUuidIdsTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use App\Models\InventoryItem;
|
||||
use App\Models\InventoryLink;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Inventory\DependencyExtractionService;
|
||||
use App\Support\Enums\RelationshipType;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
it('stores non-UUID identifiers in inventory_links on PostgreSQL', function () {
|
||||
$driver = DB::getDriverName();
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$item = InventoryItem::factory()->for($tenant)->create([
|
||||
'external_id' => '11111111-1111-1111-1111-111111111111',
|
||||
]);
|
||||
|
||||
/** @var DependencyExtractionService $service */
|
||||
$service = app(DependencyExtractionService::class);
|
||||
|
||||
$service->extractForPolicyData($item, [
|
||||
'id' => $item->external_id,
|
||||
'roleScopeTagIds' => ['0'],
|
||||
'assignments' => [],
|
||||
]);
|
||||
|
||||
if ($driver === 'pgsql') {
|
||||
$columnTypes = collect(DB::select(
|
||||
"select column_name, data_type from information_schema.columns where table_name = 'inventory_links' and column_name in ('source_id', 'target_id')"
|
||||
))
|
||||
->mapWithKeys(fn (object $row) => [(string) $row->column_name => (string) $row->data_type]);
|
||||
|
||||
expect($columnTypes->get('source_id'))->toBe('text')
|
||||
->and($columnTypes->get('target_id'))->toBe('text');
|
||||
}
|
||||
|
||||
expect(
|
||||
InventoryLink::query()
|
||||
->where('tenant_id', $tenant->getKey())
|
||||
->where('source_type', 'inventory_item')
|
||||
->where('source_id', $item->external_id)
|
||||
->where('relationship_type', RelationshipType::ScopedBy->value)
|
||||
->where('target_id', '0')
|
||||
->exists()
|
||||
)->toBeTrue();
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user