Skip to content

Commit afc102b

Browse files
authored
Merge pull request #220 from dbmdz/subjects_unique_constraint
Table subjects: unique constraint
2 parents 996c17d + 86f4477 commit afc102b

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
## Unreleased
88

9+
### Added
10+
11+
- SQL status code in error message
12+
- All transactional errors cause setting of `ProblemHint.RETRY_RECOMMENDED`
13+
14+
### Fixed
15+
16+
- Check constraint on table `subjects` obeys empty `identifiers`:
17+
If there are no identifiers then type and label must be unique
18+
among those entries without identifiers. Therefore entries are allowed
19+
that have the same type and label but one is with and one w/o
20+
identifiers.
21+
922
## [9.4.1](https://github.com/dbmdz/metadata-service/releases/tag/9.4.1) - 2025-01-09
1023

1124
### Fixed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE OR REPLACE FUNCTION public.subjects_is_unique_type_identifier(puuid uuid, ptype character varying, ids dbidentifier[], plabel jsonb)
2+
RETURNS boolean
3+
STABLE LANGUAGE plpgsql
4+
AS $function$
5+
/**
6+
* Ensures uniqueness of `type` and single identifier across table `subjects`.
7+
*
8+
* If there are not any identifiers then `type` and `label` must be unique.
9+
* It is intended for check constraints and
10+
* returns `false` on violation.
11+
*/
12+
declare
13+
db_ident record; --dbidentifier actually but makes trouble in backups
14+
tuple_exists boolean;
15+
begin
16+
if ids is null or cardinality(ids) = 0 then
17+
execute 'select exists(select 1 from public.subjects where $1 = type and $2 = label and coalesce(identifiers, array[]::dbidentifier[]) = array[]::dbidentifier[] and $3 <> uuid)'
18+
into tuple_exists
19+
using ptype, plabel, puuid;
20+
if tuple_exists then
21+
return false;
22+
end if;
23+
else
24+
foreach db_ident in array ids loop
25+
execute 'select exists(select 1 from public.subjects where $1 = type and $2 = any (identifiers) and $3 <> uuid)'
26+
into tuple_exists
27+
using ptype, db_ident, puuid;
28+
if tuple_exists then
29+
return false;
30+
end if;
31+
end loop;
32+
end if;
33+
return true;
34+
end;
35+
$function$;
36+

0 commit comments

Comments
 (0)