-- patient_facts / patient_return_visits 加 source_unit(发证命名空间)。
-- 集团模型:subject_id / external_id 品牌级,合 tenant 会撞键;source_unit 进键消歧。
-- 存量 source_unit 默认 '',新键唯一性与旧键等价,迁移安全。

DROP INDEX "patient_facts_host_id_tenant_id_subject_id_version_key";
DROP INDEX "patient_return_visits_host_id_tenant_id_external_id_key";

ALTER TABLE "patient_facts" ADD COLUMN "source_unit" TEXT NOT NULL DEFAULT '';
ALTER TABLE "patient_return_visits" ADD COLUMN "source_unit" TEXT NOT NULL DEFAULT '';

CREATE UNIQUE INDEX "patient_facts_host_id_tenant_id_source_unit_subject_id_vers_key" ON "patient_facts"("host_id", "tenant_id", "source_unit", "subject_id", "version");
CREATE UNIQUE INDEX "patient_return_visits_host_id_tenant_id_source_unit_externa_key" ON "patient_return_visits"("host_id", "tenant_id", "source_unit", "external_id");
