-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit 21d8022.
- Loading branch information
Victor
committed
Mar 25, 2024
1 parent
5e12069
commit 2cc45a6
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
targets/hasura/migrations/default/1709817126727_contribution_description_not_null/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alter table contribution.answers drop constraint check_description_not_null; |
88 changes: 88 additions & 0 deletions
88
targets/hasura/migrations/default/1709817126727_contribution_description_not_null/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
with _content_get as ( | ||
select id, | ||
content as "content" | ||
from contribution.answers | ||
where content_type in ('ANSWER', 'GENERIC_NO_CDT') | ||
union | ||
select a.id, | ||
d.text as "content" | ||
from contribution.answers a | ||
inner join public.documents d on d.cdtn_id = a.content_service_public_cdtn_id | ||
where content_type = 'SP' | ||
union | ||
select a.id, | ||
coalesce(d."text", ag."content") as "content" | ||
from contribution.answers a | ||
inner join contribution.answers ag on a.question_id = ag.question_id | ||
and ag.agreement_id = '0000' | ||
left join public.documents d on d.cdtn_id = ag.content_service_public_cdtn_id | ||
where a.content_type in ('NOTHING', 'UNKNOWN', 'UNFAVOURABLE', 'CDT') | ||
), | ||
_content_formatted as ( | ||
select id, | ||
substring( | ||
trim( | ||
REGEXP_REPLACE( | ||
REGEXP_REPLACE("content", '(<[^>]*>?)|( )', ' ', 'gm'), | ||
'[ ]{2,}', | ||
' ', | ||
'gm' | ||
) | ||
), | ||
1, | ||
156 | ||
) as "content" | ||
from _content_get | ||
where "content" is not null | ||
and "content" <> '' | ||
), | ||
_content_points as ( | ||
select id, | ||
case | ||
when length("content") = 156 then substring( | ||
"content", | ||
1, | ||
156 - position(' ' in reverse("content")) | ||
) || ' ...' | ||
else "content" | ||
end as description | ||
from _content_formatted | ||
), | ||
_update_contribution as ( | ||
update contribution.answers a | ||
set description = cp.description | ||
from _content_points cp | ||
where cp.id = a.id | ||
and ( | ||
a.description is null | ||
or a.description = '' | ||
) | ||
) | ||
update public.documents d | ||
set "document" = case when "document"->'description' is null | ||
then jsonb_insert( | ||
d."document", | ||
'{description}', | ||
jsonb_build_object('description', cp.description)->'description' | ||
) | ||
else jsonb_set( | ||
d."document", | ||
'{description}', | ||
jsonb_build_object('description', cp.description)->'description' | ||
) | ||
end | ||
from _content_points cp | ||
where cp.id::text = d.initial_id | ||
and ( | ||
"document"->'description' is null | ||
or "document"->>'description' = '' | ||
); | ||
alter table contribution.answers | ||
add constraint check_description_not_null check ( | ||
content_type = 'NOTHING' | ||
or content_type is null | ||
or ( | ||
description is not null | ||
and description <> '' | ||
) | ||
); |