forked from wangziqi/gongxue-base
107 lines
3.2 KiB
SQL
107 lines
3.2 KiB
SQL
do $$
|
|
declare
|
|
mismatched_version_count bigint;
|
|
mismatched_current_version_count bigint;
|
|
begin
|
|
select count(*)
|
|
into mismatched_version_count
|
|
from public.question_versions v
|
|
join public.questions q on q.id = v.question_id
|
|
where v.tenant_id <> q.tenant_id;
|
|
|
|
if mismatched_version_count > 0 then
|
|
raise exception
|
|
'Cannot enforce question version tenant integrity: % question_versions rows reference a question in another tenant',
|
|
mismatched_version_count;
|
|
end if;
|
|
|
|
select count(*)
|
|
into mismatched_current_version_count
|
|
from public.questions q
|
|
join public.question_versions v on v.id = q.current_version_id
|
|
where q.current_version_id is not null
|
|
and (v.tenant_id <> q.tenant_id or v.question_id <> q.id);
|
|
|
|
if mismatched_current_version_count > 0 then
|
|
raise exception
|
|
'Cannot enforce current question version integrity: % questions point to a version from another tenant or question',
|
|
mismatched_current_version_count;
|
|
end if;
|
|
end
|
|
$$;
|
|
|
|
do $$
|
|
begin
|
|
if not exists (
|
|
select 1
|
|
from pg_constraint
|
|
where conrelid = 'public.questions'::regclass
|
|
and conname = 'questions_tenant_id_id_key'
|
|
) then
|
|
alter table public.questions
|
|
add constraint questions_tenant_id_id_key unique (tenant_id, id);
|
|
end if;
|
|
|
|
if not exists (
|
|
select 1
|
|
from pg_constraint
|
|
where conrelid = 'public.question_versions'::regclass
|
|
and conname = 'question_versions_tenant_question_id_id_key'
|
|
) then
|
|
alter table public.question_versions
|
|
add constraint question_versions_tenant_question_id_id_key
|
|
unique (tenant_id, question_id, id);
|
|
end if;
|
|
end
|
|
$$;
|
|
|
|
do $$
|
|
begin
|
|
if not exists (
|
|
select 1
|
|
from pg_constraint
|
|
where conrelid = 'public.question_versions'::regclass
|
|
and conname = 'question_versions_tenant_question_fkey'
|
|
) then
|
|
alter table public.question_versions
|
|
add constraint question_versions_tenant_question_fkey
|
|
foreign key (tenant_id, question_id)
|
|
references public.questions (tenant_id, id)
|
|
on delete cascade
|
|
not valid;
|
|
end if;
|
|
|
|
if not exists (
|
|
select 1
|
|
from pg_constraint
|
|
where conrelid = 'public.questions'::regclass
|
|
and conname = 'questions_tenant_current_version_fkey'
|
|
) then
|
|
alter table public.questions
|
|
add constraint questions_tenant_current_version_fkey
|
|
foreign key (tenant_id, id, current_version_id)
|
|
references public.question_versions (tenant_id, question_id, id)
|
|
on delete set null (current_version_id)
|
|
not valid;
|
|
end if;
|
|
end
|
|
$$;
|
|
|
|
alter table public.question_versions
|
|
validate constraint question_versions_tenant_question_fkey;
|
|
|
|
alter table public.questions
|
|
validate constraint questions_tenant_current_version_fkey;
|
|
|
|
alter table public.question_versions
|
|
drop constraint if exists question_versions_question_id_fkey;
|
|
|
|
alter table public.questions
|
|
drop constraint if exists questions_current_version_id_fkey;
|
|
|
|
comment on constraint question_versions_tenant_question_fkey on public.question_versions is
|
|
'A question version must belong to the same tenant as its parent question.';
|
|
|
|
comment on constraint questions_tenant_current_version_fkey on public.questions is
|
|
'The current version pointer must reference a version of the same question in the same tenant.';
|