feat(domain): sheet document schema v2 — references registry, review_status, vertebral count_range; SQL gate + pgTAP 008
This commit is contained in:
1 parent
6c5b2b7754
commit
3dbbfdf1df
19 files changed
+474
-71
No files matched your search
@@ -0,0 +1,118 @@
|
||||
-- Canonical document schema_version 2 (ANATOMICAL_SHEET_MODEL.md §2, Step 4c):
|
||||
-- shared `references` registry cited by `reference_ids`, `review_status`
|
||||
-- on findings and annotations, optional `count_range` on vertebral regions.
|
||||
-- No version 1 document was ever published (the only row is the official
|
||||
-- equine draft, regenerated by its seed), so version 1 is no longer stored.
|
||||
|
||||
alter table public.anatomical_sheet_versions
|
||||
drop constraint anatomical_sheet_versions_schema_known;
|
||||
|
||||
alter table public.anatomical_sheet_versions
|
||||
add constraint anatomical_sheet_versions_schema_known
|
||||
check (document_schema_version = 2);
|
||||
|
||||
-- Shape: the references registry is one more id-bearing array.
|
||||
create or replace function private.sheet_document_has_shape(p_doc jsonb)
|
||||
returns boolean
|
||||
language sql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$
|
||||
select jsonb_typeof(p_doc) = 'object'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'views', '[]'::jsonb)) = 'array'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'annotations', '[]'::jsonb)) = 'array'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'categories', '[]'::jsonb)) = 'array'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'finding_zones', '[]'::jsonb)) = 'array'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'references', '[]'::jsonb)) = 'array'
|
||||
and jsonb_typeof(coalesce(p_doc -> 'vertebral_config' -> 'regions', '[]'::jsonb)) = 'array';
|
||||
$$;
|
||||
|
||||
-- Uniqueness of the id-bearing collections, references included.
|
||||
create or replace function private.sheet_document_duplicate_issues(p_doc jsonb)
|
||||
returns text[]
|
||||
language plpgsql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_issues text[] := '{}';
|
||||
r record;
|
||||
begin
|
||||
for r in
|
||||
select * from (values
|
||||
('views', 'id', 'view.id.duplicate'),
|
||||
('annotations', 'id', 'annotation.id.duplicate'),
|
||||
('categories', 'id', 'category.id.duplicate'),
|
||||
('finding_zones', 'id', 'finding_zone.id.duplicate'),
|
||||
('references', 'id', 'reference.id.duplicate')
|
||||
) as t(collection, key, code)
|
||||
loop
|
||||
if exists (
|
||||
select 1 from jsonb_array_elements(coalesce(p_doc -> r.collection, '[]'::jsonb)) e
|
||||
group by e ->> r.key having count(*) > 1
|
||||
) then
|
||||
v_issues := private.append_issue(v_issues, r.code);
|
||||
end if;
|
||||
end loop;
|
||||
if exists (
|
||||
select 1 from jsonb_array_elements(coalesce(p_doc -> 'vertebral_config' -> 'regions', '[]'::jsonb)) g
|
||||
group by g ->> 'code' having count(*) > 1
|
||||
) then
|
||||
v_issues := private.append_issue(v_issues, 'region.code.duplicate');
|
||||
end if;
|
||||
return v_issues;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Citations point at declared references (every depth of the finding tree,
|
||||
-- categories included); the usual vertebra count sits inside its range.
|
||||
create or replace function private.sheet_document_reference_issues(p_doc jsonb)
|
||||
returns text[]
|
||||
language plpgsql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_issues text[] := '{}';
|
||||
begin
|
||||
if exists (
|
||||
select 1
|
||||
from jsonb_path_query(coalesce(p_doc -> 'finding_zones', '[]'::jsonb), '$[*].categories[*].**.reference_ids[*]') cited
|
||||
where not exists (
|
||||
select 1 from jsonb_array_elements(coalesce(p_doc -> 'references', '[]'::jsonb)) ref
|
||||
where ref -> 'id' = cited
|
||||
)
|
||||
) then
|
||||
v_issues := private.append_issue(v_issues, 'finding.reference.missing');
|
||||
end if;
|
||||
if exists (
|
||||
select 1
|
||||
from jsonb_array_elements(coalesce(p_doc -> 'vertebral_config' -> 'regions', '[]'::jsonb)) g
|
||||
where jsonb_typeof(g -> 'count_range') = 'object'
|
||||
and not (
|
||||
(g -> 'count_range' ->> 'min')::numeric <= (g ->> 'count')::numeric
|
||||
and (g ->> 'count')::numeric <= (g -> 'count_range' ->> 'max')::numeric
|
||||
)
|
||||
) then
|
||||
v_issues := private.append_issue(v_issues, 'region.count_range.invalid');
|
||||
end if;
|
||||
return v_issues;
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function private.sheet_document_reference_issues(jsonb) from public;
|
||||
|
||||
create or replace function private.sheet_document_issues(p_doc jsonb)
|
||||
returns text[]
|
||||
language sql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$
|
||||
select case
|
||||
when not private.sheet_document_has_shape(p_doc) then array['document.shape']
|
||||
else private.sheet_document_view_issues(p_doc)
|
||||
|| private.sheet_document_duplicate_issues(p_doc)
|
||||
|| private.sheet_document_annotation_issues(p_doc)
|
||||
|| private.sheet_document_reference_issues(p_doc)
|
||||
end;
|
||||
$$;
|
||||
@@ -193,7 +193,7 @@ Aucune donnée métier écartée.
|
||||
|
||||
Non repris par construction :
|
||||
|
||||
- `schema_version` du catalogue (`2.0`) : version du format historique, remplacée par `schema_version: 1` du document canonique ; la valeur d'origine est citée dans `attribution.notes`.
|
||||
- `schema_version` du catalogue (`2.0`) : version du format historique, remplacée par `schema_version: 2` du document canonique ; la valeur d'origine est citée dans `attribution.notes`.
|
||||
- bloc `species` du catalogue (`equine`, Cheval / Horse) : porté par `species_id` du document et par la table `public.species` ; le nom de la fiche est écrit séparément (voir anomalies `label.authored`).
|
||||
- second élément graphique de chaque annotation (marqueur visuel) : reste dans l'asset SVG copié à l'identique, le document ne garde que la zone de clic.
|
||||
- `data-anno-category-name` : répète le slug technique (`articulations`, `os`), ce n'est pas un libellé ; le libellé de catégorie vient de la table de l'importateur (section 4).
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"schema_version": 2,
|
||||
"species_id": "equine",
|
||||
"name": {
|
||||
"fr": "Cheval",
|
||||
@@ -61,6 +61,7 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"references": [],
|
||||
"categories": [
|
||||
{
|
||||
"id": "articulations",
|
||||
|
||||
@@ -23,7 +23,7 @@ select private.insert_sheet_draft(
|
||||
null,
|
||||
'503dc301-a8b1-50d4-9e44-0919b2fc2916',
|
||||
$equine_doc${
|
||||
"schema_version": 1,
|
||||
"schema_version": 2,
|
||||
"species_id": "equine",
|
||||
"name": {
|
||||
"fr": "Cheval",
|
||||
@@ -85,6 +85,7 @@ select private.insert_sheet_draft(
|
||||
}
|
||||
]
|
||||
},
|
||||
"references": [],
|
||||
"categories": [
|
||||
{
|
||||
"id": "articulations",
|
||||
|
||||
@@ -21,7 +21,7 @@ returns jsonb
|
||||
language sql
|
||||
as $$
|
||||
select jsonb_build_object(
|
||||
'schema_version', 1,
|
||||
'schema_version', 2,
|
||||
'species_id', 'equine',
|
||||
'name', jsonb_build_object('fr', 'Cheval test', 'en', 'Test horse'),
|
||||
'description', '{}'::jsonb,
|
||||
@@ -30,6 +30,7 @@ as $$
|
||||
'vertebral_config', jsonb_build_object('regions', jsonb_build_array(
|
||||
jsonb_build_object('code', 'C', 'name', jsonb_build_object('fr', 'Cervicales', 'en', 'Cervical'), 'count', 7)
|
||||
)),
|
||||
'references', '[]'::jsonb,
|
||||
'categories', jsonb_build_array(
|
||||
jsonb_build_object('id', 'articulations', 'name', jsonb_build_object('fr', 'Articulations', 'en', 'Joints'), 'color', null)
|
||||
),
|
||||
@@ -160,7 +161,7 @@ select throws_ok(
|
||||
);
|
||||
select throws_ok(
|
||||
$$ update public.anatomical_sheet_versions
|
||||
set content = jsonb_build_object('schema_version', 1, 'species_id', 'equine', 'pad', repeat('x', 2200000))
|
||||
set content = jsonb_build_object('schema_version', 2, 'species_id', 'equine', 'pad', repeat('x', 2200000))
|
||||
where id = pg_temp.id('v1') $$,
|
||||
'23514', null, 'a document over 2 MiB is refused'
|
||||
);
|
||||
@@ -405,7 +406,7 @@ select throws_ok(
|
||||
);
|
||||
select throws_ok(
|
||||
$$ insert into public.anatomical_sheet_versions (sheet_id, version_number, document_schema_version, content)
|
||||
values (pg_temp.id('sheet'), 9, 1, pg_temp.sheet_doc(null)) $$,
|
||||
values (pg_temp.id('sheet'), 9, 2, pg_temp.sheet_doc(null)) $$,
|
||||
'42501', null, 'bob cannot insert a version row directly'
|
||||
);
|
||||
|
||||
@@ -429,7 +430,7 @@ select is(
|
||||
(select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('v3') and content = '{}'::jsonb),
|
||||
0::bigint, 'bob cannot read the draft'
|
||||
);
|
||||
update public.anatomical_sheet_versions set content = '{"schema_version":1}'::jsonb where id = pg_temp.id('v3');
|
||||
update public.anatomical_sheet_versions set content = '{"schema_version":2}'::jsonb where id = pg_temp.id('v3');
|
||||
select throws_ok(
|
||||
$$ select public.fork_anatomical_sheet(pg_temp.id('v3'), 'Copie', 'copie-bob') $$,
|
||||
'42501', null, 'a draft cannot be copied (hidden from non-editors)'
|
||||
@@ -517,7 +518,7 @@ select is(
|
||||
update public.anatomical_sheet_versions set content = '{}'::jsonb where id = pg_temp.id('v2');
|
||||
select is(
|
||||
(select content ->> 'schema_version' from public.anatomical_sheet_versions where id = pg_temp.id('v2')),
|
||||
'1', 'a deprecated version is hidden from editor UPDATE'
|
||||
'2', 'a deprecated version is hidden from editor UPDATE'
|
||||
);
|
||||
reset role;
|
||||
select throws_ok(
|
||||
|
||||
@@ -17,7 +17,7 @@ grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
|
||||
-- Minimal publishable document with one main view on a given asset.
|
||||
create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$
|
||||
select jsonb_build_object(
|
||||
'schema_version', 1, 'species_id', 'equine',
|
||||
'schema_version', 2, 'species_id', 'equine',
|
||||
'name', jsonb_build_object('fr', 'A', 'en', 'A'),
|
||||
'description', '{}'::jsonb, 'scope_disclaimer', '{}'::jsonb,
|
||||
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
|
||||
|
||||
@@ -28,7 +28,7 @@ select results_eq(
|
||||
select results_eq(
|
||||
$$ select version_number, status::text, document_schema_version, published_at
|
||||
from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet') $$,
|
||||
$$ values (1, 'draft', 1, null::timestamptz) $$,
|
||||
$$ values (1, 'draft', 2, null::timestamptz) $$,
|
||||
'exactly one version, draft v1, never published'
|
||||
);
|
||||
select is((select count(*) from public.anatomical_sheet_assets where sheet_id = pg_temp.id('sheet')), 2::bigint, 'two SVG assets registered');
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
-- Document schema_version 2: references registry, review status, count
|
||||
-- ranges. Only version 2 is stored; the SQL gate re-checks the new rules.
|
||||
begin;
|
||||
select plan(8);
|
||||
|
||||
insert into auth.users (id, email) values ('50000000-0000-0000-0000-00000000000a', 'alice-v2@test.local');
|
||||
|
||||
create function pg_temp.doc(p_refs jsonb, p_cited jsonb, p_range jsonb)
|
||||
returns jsonb
|
||||
language sql
|
||||
as $$
|
||||
select jsonb_build_object(
|
||||
'schema_version', 2,
|
||||
'species_id', 'equine',
|
||||
'name', jsonb_build_object('fr', 'Cheval', 'en', 'Horse'),
|
||||
'description', '{}'::jsonb,
|
||||
'scope_disclaimer', '{}'::jsonb,
|
||||
'attribution', jsonb_build_object('authors', '[]'::jsonb, 'license', null, 'notes', null),
|
||||
'vertebral_config', jsonb_build_object('regions', jsonb_build_array(
|
||||
jsonb_build_object('code', 'Cd', 'name', jsonb_build_object('fr', 'Caudales', 'en', 'Caudal'), 'count', 18, 'count_range', p_range)
|
||||
)),
|
||||
'references', p_refs,
|
||||
'categories', jsonb_build_array(
|
||||
jsonb_build_object('id', 'os', 'name', jsonb_build_object('fr', 'Os', 'en', 'Bones'), 'color', null)
|
||||
),
|
||||
'views', jsonb_build_array(
|
||||
jsonb_build_object('id', 'main', 'kind', 'main', 'name', jsonb_build_object('fr', 'Vue', 'en', 'View'),
|
||||
'svg_asset_id', '00000000-0000-0000-0000-000000000001', 'view_box', null)
|
||||
),
|
||||
'annotations', jsonb_build_array(
|
||||
jsonb_build_object('id', 'hip', 'kind', 'point', 'view_id', 'main', 'category_id', 'os', 'laterality', 'bilateral',
|
||||
'geometry', null, 'review_status', 'inherited', 'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip'),
|
||||
'finding_zone_id', 'hip', 'vertebral_region_code', null)
|
||||
),
|
||||
'finding_zones', jsonb_build_array(
|
||||
jsonb_build_object('id', 'hip', 'name', jsonb_build_object('fr', 'Hanche', 'en', 'Hip'), 'categories', jsonb_build_array(
|
||||
jsonb_build_object('id', 'hip-dysfunctions', 'kind', 'dysfunction',
|
||||
'name', jsonb_build_object('fr', 'Dysfonctions', 'en', 'Dysfunctions'),
|
||||
'reference_ids', '[]'::jsonb,
|
||||
'children', jsonb_build_array(
|
||||
jsonb_build_object('id', 'articular', 'name', jsonb_build_object('fr', 'Articulaire', 'en', 'Articular'),
|
||||
'children', jsonb_build_array(
|
||||
jsonb_build_object('id', 'flexion', 'name', jsonb_build_object('fr', 'Flexion', 'en', 'Flexion'), 'reference_ids', p_cited)
|
||||
))
|
||||
))
|
||||
))
|
||||
)
|
||||
);
|
||||
$$;
|
||||
grant execute on function pg_temp.doc(jsonb, jsonb, jsonb) to authenticated;
|
||||
|
||||
select is(
|
||||
private.sheet_document_issues(pg_temp.doc('[{"id":"nav","type":"standard","citation":"NAV 2017","url":null,"doi":null}]', '["nav"]', '{"min":15,"max":21}')),
|
||||
'{}'::text[], 'a v2 document citing a declared reference inside its count range passes'
|
||||
);
|
||||
select is(
|
||||
private.sheet_document_issues(pg_temp.doc('[]', '["nav"]', 'null')),
|
||||
'{finding.reference.missing}'::text[], 'a citation of an undeclared reference is reported (nested node)'
|
||||
);
|
||||
select is(
|
||||
private.sheet_document_issues(pg_temp.doc('[{"id":"nav","citation":"a"},{"id":"nav","citation":"b"}]', '[]', 'null')),
|
||||
'{reference.id.duplicate}'::text[], 'duplicate reference ids are reported'
|
||||
);
|
||||
select is(
|
||||
private.sheet_document_issues(pg_temp.doc('[]', '[]', '{"min":19,"max":21}')),
|
||||
'{region.count_range.invalid}'::text[], 'a usual count outside its range is reported'
|
||||
);
|
||||
select is(
|
||||
private.sheet_document_issues(pg_temp.doc('[]', '[]', '{"min":18,"max":18}')),
|
||||
'{}'::text[], 'a count on the bounds of its range passes'
|
||||
);
|
||||
select is(
|
||||
private.sheet_document_issues(jsonb_set(pg_temp.doc('[]', '[]', 'null'), '{references}', '"x"')),
|
||||
'{document.shape}'::text[], 'a non-array references registry is a shape error'
|
||||
);
|
||||
|
||||
-- Only schema_version 2 rows are stored.
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"50000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select throws_ok(
|
||||
$$ select public.create_anatomical_sheet('equine', 'Old', 'old-v1', jsonb_set(pg_temp.doc('[]', '[]', 'null'), '{schema_version}', '1')) $$,
|
||||
'23514', null, 'a schema_version 1 document is refused'
|
||||
);
|
||||
select lives_ok(
|
||||
$$ select public.create_anatomical_sheet('equine', 'New', 'new-v2', pg_temp.doc('[]', '[]', 'null')) $$,
|
||||
'a schema_version 2 document is accepted as a draft'
|
||||
);
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
Reference in new issue
Block a user