-- Phase 1 of the nested document subfolders feature (master UAT line 728+): -- -- 1. Add a nullable `interest_id` column to `files` so uploads scoped -- to a deal can be filed under the interest subfolder while still -- rolling up to the parent client folder. Mirrors the existing -- `documents.interest_id` semantics: scoping FK that holds a -- "from-interest" attribution even when the parent client/yacht/ -- company shifts. -- -- 2. Index on `(port_id, interest_id)` for the aggregated-projection -- queries that will surface "this-deal" files vs "from-client" files -- in InterestDocumentsTab. -- -- 3. Soft FK (`ON DELETE SET NULL`) so a hard-deleted interest doesn't -- orphan the file — the audit trail stays intact and the file remains -- findable under the parent client folder. -- -- Apply in dev: -- PGPASSWORD=changeme psql -h localhost -p 5434 -U crm -d port_nimara_crm \ -- -f src/lib/db/migrations/0078_files_interest_id.sql ALTER TABLE files ADD COLUMN IF NOT EXISTS interest_id text REFERENCES interests(id) ON DELETE SET NULL; CREATE INDEX IF NOT EXISTS idx_files_interest ON files (interest_id); CREATE INDEX IF NOT EXISTS idx_files_port_interest ON files (port_id, interest_id);