-- Link bank-register transactions back to the GL line that produced them, so
-- the Buildium GL pull can materialize register rows idempotently for
-- import-mode companies (gl_auto_post = false). NULL = manually-entered or
-- legacy register row with no GL source. NULLs are distinct, so many unlinked
-- rows coexist; the unique index only prevents materializing the same GL line
-- into the register twice (and is usable as an ON CONFLICT target).
alter table accounting.transactions
  add column if not exists journal_entry_line_id uuid
  references accounting.journal_entry_lines(id) on delete set null;

create unique index if not exists transactions_jel_id_uidx
  on accounting.transactions(journal_entry_line_id);
