WS-5c commit 1 of 4 — relational infrastructure for the conditional- logic tree that replaces form_fields.conditional_logic JSON (ARCH- FORM-BUILDER §8; addendum Q3 WS-5c). Tables: groups (nesting via parent_group_id) + conditions (leaves, value JSON nullable for empty/not_empty). Simple FK to form_fields — addendum Q3 explicitly excludes form_field_library from conditional_ logic scope, so no polymorphic morph here. OrganisationScope cap raised 3 → 5 hops. The conditions chain is 4 hops (condition → group → field → schema → organisation_id column) and the new cap gives headroom for future deeper trees without denormalising form_field_id onto conditions. Cascade observer (FormFieldChildTablesCascadeObserver) extended to physically delete the new groups table on FormField delete (hard or soft). Conditions cascade automatically via the group_id FK on the groups table. Factories: FormFieldConditionalLogicGroupFactory, FormFieldConditional LogicConditionFactory, and FormFieldFactory::withConditionalLogic($tree) for concise test fixtures. Tests: 16 new under tests/Feature/FormBuilder/ConditionalLogic/ (relation, scope, cascade, enum catalogue). 3 new scope-cap tests in ScopeLeakageTest verify 4/5-hop chains pass and 6-hop throws. Hardcoded rollback step counts in WS-5a/b migration tests bumped for the 2 new WS-5c migrations. Baseline 1104 → 1122 green (2988 → 3032 assertions). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
72 lines
2.8 KiB
PHP
72 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Observers\FormBuilder;
|
|
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormFieldLibrary;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/**
|
|
* Cascades physical deletion of child rows in the relational tables
|
|
* that hang off `form_fields` / `form_field_library`:
|
|
*
|
|
* - `form_field_bindings` (WS-5a)
|
|
* - `form_field_validation_rules` (WS-5b)
|
|
* - `form_field_configs` (WS-5b commit 5)
|
|
* - `form_field_conditional_logic_groups` (WS-5c — FormField only,
|
|
* per addendum Q3: library
|
|
* is explicitly out of
|
|
* scope for conditional_logic)
|
|
*
|
|
* The conditions sibling table cascades automatically via the `group_id`
|
|
* FK on the groups table — no direct entry here.
|
|
*
|
|
* Children represent current state (not historical intent) — physically
|
|
* deleted even when the owner is only soft-deleted, per addendum Q3: no
|
|
* child table in this family carries a soft-delete semantic of its own.
|
|
*
|
|
* Renamed from `FormFieldBindingsCascadeObserver` during WS-5b commit 1.
|
|
* The `Schema::hasTable` guard keeps the observer safe when this code
|
|
* runs against a database where a later migration has not yet been
|
|
* applied (e.g. during the migration step itself).
|
|
*/
|
|
final class FormFieldChildTablesCascadeObserver
|
|
{
|
|
public function deleted(FormField|FormFieldLibrary $owner): void
|
|
{
|
|
$ownerType = $owner instanceof FormField ? 'form_field' : 'form_field_library';
|
|
$ownerId = $owner->getKey();
|
|
|
|
DB::table('form_field_bindings')
|
|
->where('owner_type', $ownerType)
|
|
->where('owner_id', $ownerId)
|
|
->delete();
|
|
|
|
if (Schema::hasTable('form_field_validation_rules')) {
|
|
DB::table('form_field_validation_rules')
|
|
->where('owner_type', $ownerType)
|
|
->where('owner_id', $ownerId)
|
|
->delete();
|
|
}
|
|
|
|
if (Schema::hasTable('form_field_configs')) {
|
|
DB::table('form_field_configs')
|
|
->where('owner_type', $ownerType)
|
|
->where('owner_id', $ownerId)
|
|
->delete();
|
|
}
|
|
|
|
// Conditional-logic groups only apply to FormField (addendum Q3:
|
|
// library mirror is out of scope). Condition rows cascade via
|
|
// the group_id FK on the conditions table.
|
|
if ($owner instanceof FormField && Schema::hasTable('form_field_conditional_logic_groups')) {
|
|
DB::table('form_field_conditional_logic_groups')
|
|
->where('form_field_id', $ownerId)
|
|
->delete();
|
|
}
|
|
}
|
|
}
|