-- Migration: Add owner_user_id to hdb_program_registration
-- Spec ref: Section 15 - Migration Strategy

ALTER TABLE hdb_program_registration
  ADD COLUMN IF NOT EXISTS owner_user_id BIGINT REFERENCES "users"(id);

-- Backfill: existing registrations owned by their creator (Section 15.2)
UPDATE hdb_program_registration
SET owner_user_id = created_by
WHERE owner_user_id IS NULL AND created_by IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_hpr_owner_user_id
  ON hdb_program_registration(owner_user_id);
