Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 6.1.1 on 2026-10-27 12:16

import django_migrate_sql.operations
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("models", "13002_tile_and_resource_row_metadata"),
]

operations = [
django_migrate_sql.operations.AlterSQL(
name="__arches_get_node_value_sql",
sql="\ncreate or replace function __arches_get_node_value_sql(node nodes) returns text as\n$$\ndeclare\n node_value_sql text;\n select_sql text = '(t.tiledata->>%L)';\n datatype text = 'text';\nbegin\n select_sql = format(select_sql, node.nodeid);\n if (node.config->>'pgDatatype' is not null) then\n datatype = node.config->>'pgDatatype';\n else\n case node.datatype\n when 'geojson-feature-collection' then datatype = 'geometry';\n when 'string' then datatype = 'jsonb';\n when 'number' then datatype = 'numeric';\n when 'boolean' then datatype = 'boolean';\n when 'resource-instance' then datatype = 'jsonb';\n when 'resource-instance-list' then datatype = 'jsonb';\n when 'annotation' then datatype = 'jsonb';\n when 'file-list' then datatype = 'jsonb';\n when 'url' then datatype = 'jsonb';\n when 'date' then datatype = 'timestamp';\n when 'node-value' then datatype = 'uuid';\n when 'domain-value' then datatype = 'uuid';\n when 'domain-value-list' then datatype = 'uuid[]';\n when 'concept' then datatype = 'uuid';\n when 'concept-list' then datatype = 'uuid[]';\n when 'reference' then datatype = 'jsonb';\n else\n datatype = 'text';\n end case;\n end if;\n case datatype\n when 'geometry' then\n select_sql = format('\n st_collect(\n array(\n select st_transform(geom, 4326) from geojson_geometries\n where geojson_geometries.tileid = t.tileid and nodeid = %L\n )\n )',\n node.nodeid\n );\n when 'timestamp' then\n select_sql = format(\n 'to_date(\n t.tiledata->>%L::text,\n %L\n )',\n node.nodeid,\n node.config->>'dateFormat'\n );\n when 'uuid[]' then\n select_sql = format('(\n CASE\n WHEN t.tiledata->>%1$L is null THEN null\n ELSE ARRAY(\n SELECT jsonb_array_elements_text(\n t.tiledata->%1$L\n )::uuid\n )\n END\n )', node.nodeid\n );\n else\n null;\n end case;\n\n\n node_value_sql = format(\n '%s::%s as \"%s\"',\n select_sql,\n datatype,\n node.alias\n );\n return node_value_sql;\nend\n$$ language plpgsql volatile;\n",
reverse_sql="\ncreate or replace function __arches_get_node_value_sql(node nodes) returns text as\n$$\ndeclare\n node_value_sql text;\n select_sql text = '(t.tiledata->>%L)';\n datatype text = 'text';\nbegin\n select_sql = format(select_sql, node.nodeid);\n if (node.config->>'pgDatatype' is not null) then\n datatype = node.config->>'pgDatatype';\n else\n case node.datatype\n when 'geojson-feature-collection' then datatype = 'geometry';\n when 'string' then datatype = 'jsonb';\n when 'number' then datatype = 'numeric';\n when 'boolean' then datatype = 'boolean';\n when 'resource-instance' then datatype = 'jsonb';\n when 'resource-instance-list' then datatype = 'jsonb';\n when 'annotation' then datatype = 'jsonb';\n when 'file-list' then datatype = 'jsonb';\n when 'url' then datatype = 'jsonb';\n when 'date' then datatype = 'timestamp';\n when 'node-value' then datatype = 'uuid';\n when 'domain-value' then datatype = 'uuid';\n when 'domain-value-list' then datatype = 'uuid[]';\n when 'concept' then datatype = 'uuid';\n when 'concept-list' then datatype = 'uuid[]';\n else\n datatype = 'text';\n end case;\n end if;\n case datatype\n when 'geometry' then\n select_sql = format('\n st_collect(\n array(\n select st_transform(geom, 4326) from geojson_geometries\n where geojson_geometries.tileid = t.tileid and nodeid = %L\n )\n )',\n node.nodeid\n );\n when 'timestamp' then\n select_sql = format(\n 'to_date(\n t.tiledata->>%L::text,\n %L\n )',\n node.nodeid,\n node.config->>'dateFormat'\n );\n when 'uuid[]' then\n select_sql = format('(\n CASE\n WHEN t.tiledata->>%1$L is null THEN null\n ELSE ARRAY(\n SELECT jsonb_array_elements_text(\n t.tiledata->%1$L\n )::uuid\n )\n END\n )', node.nodeid\n );\n else\n null;\n end case;\n\n\n node_value_sql = format(\n '%s::%s as \"%s\"',\n select_sql,\n datatype,\n node.alias\n );\n return node_value_sql;\nend\n$$ language plpgsql volatile;\n",
state_reverse_sql="drop function __arches_get_node_value_sql;",
),
django_migrate_sql.operations.AlterSQL(
name="__arches_get_node_display_value",
sql="\nCREATE OR REPLACE FUNCTION public.__arches_get_node_display_value(\n in_tiledata jsonb,\n in_nodeid uuid,\n language_id text DEFAULT 'en')\n RETURNS text\n LANGUAGE 'plpgsql'\n COST 100\n VOLATILE PARALLEL UNSAFE\nAS $BODY$\n declare\n display_value text := '';\n in_node_type text;\n in_node_config json;\n begin\n if in_nodeid is null or in_nodeid is null then\n return '<invalid_nodeid>';\n end if;\n\n if in_tiledata is null then\n return '';\n end if;\n\n select n.datatype, n.config\n into in_node_type, in_node_config\n from nodes n where nodeid = in_nodeid::uuid;\n\n if in_node_type in ('semantic', 'geojson-feature-collection', 'annotation') then\n return 'unsupported node type (' || in_node_type || ')';\n end if;\n\n if in_node_type is null then\n return '';\n end if;\n\n case in_node_type\n when 'string' then\n display_value := ((in_tiledata -> in_nodeid::text) -> language_id) ->> 'value';\n when 'concept' then\n display_value := __arches_get_concept_label((in_tiledata ->> in_nodeid::text)::uuid);\n when 'concept-list' then\n display_value := __arches_get_concept_list_label(in_tiledata -> in_nodeid::text);\n when 'edtf' then\n display_value := (in_tiledata ->> in_nodeid::text);\n when 'file-list' then\n display_value := __arches_get_file_list_label(in_tiledata -> in_nodeid::text, language_id);\n when 'domain-value' then\n display_value := __arches_get_domain_label((in_tiledata ->> in_nodeid::text)::uuid, in_nodeid, language_id);\n when 'domain-value-list' then\n display_value := __arches_get_domain_list_label(in_tiledata -> in_nodeid::text, in_nodeid, language_id);\n when 'url' then\n display_value := ((in_tiledata -> in_nodeid::text)::jsonb ->> 'url');\n when 'node-value' then\n display_value := __arches_get_nodevalue_label((in_tiledata -> in_nodeid::text)::uuid, in_nodeid, language_id);\n when 'resource-instance' then\n display_value := __arches_get_resourceinstance_label(in_tiledata -> in_nodeid::text, 'name', language_id);\n when 'resource-instance-list' then\n display_value := __arches_get_resourceinstance_list_label(in_tiledata -> in_nodeid::text, 'name', language_id);\n when 'reference' then\n select coalesce(string_agg(reference_label, ', '), '')\n into display_value\n from jsonb_array_elements_text(\n __arches_controlled_lists_get_reference_label_list(in_tiledata -> in_nodeid::text, language_id)\n ) as reference_label;\n else\n display_value := (in_tiledata ->> in_nodeid::text)::text;\n\n end case;\n\n return display_value;\n end;\n$BODY$;\n",
reverse_sql="\nCREATE OR REPLACE FUNCTION public.__arches_get_node_display_value(\n in_tiledata jsonb,\n in_nodeid uuid,\n language_id text DEFAULT 'en')\n RETURNS text\n LANGUAGE 'plpgsql'\n COST 100\n VOLATILE PARALLEL UNSAFE\nAS $BODY$\n declare\n display_value text := '';\n in_node_type text;\n in_node_config json;\n begin\n if in_nodeid is null or in_nodeid is null then\n return '<invalid_nodeid>';\n end if;\n\n if in_tiledata is null then\n return '';\n end if;\n\n select n.datatype, n.config\n into in_node_type, in_node_config\n from nodes n where nodeid = in_nodeid::uuid;\n\n if in_node_type in ('semantic', 'geojson-feature-collection', 'annotation') then\n return 'unsupported node type (' || in_node_type || ')';\n end if;\n\n if in_node_type is null then\n return '';\n end if;\n\n case in_node_type\n when 'string' then\n display_value := ((in_tiledata -> in_nodeid::text) -> language_id) ->> 'value';\n when 'concept' then\n display_value := __arches_get_concept_label((in_tiledata ->> in_nodeid::text)::uuid);\n when 'concept-list' then\n display_value := __arches_get_concept_list_label(in_tiledata -> in_nodeid::text);\n when 'edtf' then\n display_value := (in_tiledata ->> in_nodeid::text);\n when 'file-list' then\n display_value := __arches_get_file_list_label(in_tiledata -> in_nodeid::text, language_id);\n when 'domain-value' then\n display_value := __arches_get_domain_label((in_tiledata ->> in_nodeid::text)::uuid, in_nodeid, language_id);\n when 'domain-value-list' then\n display_value := __arches_get_domain_list_label(in_tiledata -> in_nodeid::text, in_nodeid, language_id);\n when 'url' then\n display_value := ((in_tiledata -> in_nodeid::text)::jsonb ->> 'url');\n when 'node-value' then\n display_value := __arches_get_nodevalue_label((in_tiledata -> in_nodeid::text)::uuid, in_nodeid, language_id);\n when 'resource-instance' then\n display_value := __arches_get_resourceinstance_label(in_tiledata -> in_nodeid::text, 'name', language_id);\n when 'resource-instance-list' then\n display_value := __arches_get_resourceinstance_list_label(in_tiledata -> in_nodeid::text, 'name', language_id);\n else\n display_value := (in_tiledata ->> in_nodeid::text)::text;\n\n end case;\n\n return display_value;\n end;\n$BODY$;\n",
state_reverse_sql="drop function __arches_get_node_display_value;",
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ AS $BODY$
display_value := __arches_get_resourceinstance_label(in_tiledata -> in_nodeid::text, 'name', language_id);
when 'resource-instance-list' then
display_value := __arches_get_resourceinstance_list_label(in_tiledata -> in_nodeid::text, 'name', language_id);
when 'reference' then
select coalesce(string_agg(reference_label, ', '), '')
into display_value
from jsonb_array_elements_text(
__arches_controlled_lists_get_reference_label_list(in_tiledata -> in_nodeid::text, language_id)
) as reference_label;
else
display_value := (in_tiledata ->> in_nodeid::text)::text;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ begin
when 'domain-value-list' then datatype = 'uuid[]';
when 'concept' then datatype = 'uuid';
when 'concept-list' then datatype = 'uuid[]';
when 'reference' then datatype = 'jsonb';
else
datatype = 'text';
end case;
Expand Down
1 change: 1 addition & 0 deletions releases/8.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
- Resources created through `/api/node_value/` record the requesting user as their principal user, so their creator can see them under the default-deny permission framework [#13012](https://github.com/archesproject/arches/pull/13012)
- Under the default-deny permission framework, a resource's principal user can see it through the Elasticsearch-backed instance filter (used by `/geojson` and vector tiles), matching search and the database fallback [#13012](https://github.com/archesproject/arches/pull/13012)
- Fix `__arches_slugify` keeping hyphens, which produced node aliases that could not be used as attribute names (e.g. in arches-querysets `aliased_data`); relational views and their columns are now named by node alias [#12773](https://github.com/archesproject/arches/pull/12773)
- Fix relational view columns for `reference` nodes being typed as `text` instead of `jsonb`, which stored values written through the view as JSON strings; `__arches_get_node_display_value` now returns labels for `reference` nodes [#13064](https://github.com/archesproject/arches/issues/13064)
- Fix the maplibre-gl "Critical dependency: the request of a dependency is an expression" webpack warning; webpack had replaced maplibre's worker URL code with a lookup that throws when static files are served from another origin [#13038](https://github.com/archesproject/arches/issues/13038)

### Deprecations
Expand Down
72 changes: 71 additions & 1 deletion tests/models/relational_data_model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import json
import os
import uuid
from tests import test_settings
from tests.base_test import ArchesTestCase
from django.core import management
from django.test.utils import captured_stdout
from arches.app.models.models import DDataType, Node
from arches.app.models.models import DDataType, Node, ResourceInstance, TileModel
from arches.app.models.graph import Graph
from arches.extensions.controlled_lists.models import List, ListItem, ListItemValue
from django.db import connection

# these tests can be run from the command line via
Expand Down Expand Up @@ -130,3 +133,70 @@ def test_views_named_by_node_alias(self):
rows = cursor.fetchall()

self.assertEqual(rows, [("aliased_group", "aliased_string")])

@staticmethod
def build_reference_tile_value(*labels):
controlled_list = List.objects.create(name="Relational view references")
reference_tile_value = []
for sortorder, label in enumerate(labels):
list_item = ListItem.objects.create(
list=controlled_list,
sortorder=sortorder,
uri=f"http://example.com/{sortorder}",
)
ListItemValue.objects.create(
list_item=list_item,
valuetype_id="prefLabel",
language_id="en",
value=label,
)
reference_tile_value.append(list_item.build_tile_value())
return reference_tile_value

def test_reference_column_round_trips_as_jsonb(self):
reference_nodeid = "9712762c-936d-11ed-b671-5254004d77d3"
Node.objects.filter(pk=reference_nodeid).update(datatype="reference", config={})
reference_tile_value = self.build_reference_tile_value("Fixture label")
resource_instance = ResourceInstance.objects.create(
graph_id=RelationalDataModelTests.custom_data_type_graphid
)
tileid = uuid.uuid4()

with connection.cursor() as cursor:
cursor.execute(
"select __arches_create_resource_model_views(%s::uuid);",
[RelationalDataModelTests.custom_data_type_graphid],
)
cursor.execute("""
select data_type from information_schema.columns
where table_schema = 'relational_data_model_tests'
and table_name = 'custom_datatypes'
and column_name = 'extended_string_datatype'
""")
self.assertEqual(cursor.fetchone(), ("jsonb",))
cursor.execute(
"""
insert into relational_data_model_tests.custom_datatypes
(tileid, resourceinstanceid, extended_string_datatype)
values (%s, %s, %s::jsonb);
""",
[tileid, resource_instance.pk, json.dumps(reference_tile_value)],
)

tile = TileModel.objects.get(pk=tileid)
self.assertEqual(tile.data[reference_nodeid], reference_tile_value)

def test_reference_display_value(self):
reference_nodeid = "9712762c-936d-11ed-b671-5254004d77d3"
Node.objects.filter(pk=reference_nodeid).update(datatype="reference", config={})
reference_tile_value = self.build_reference_tile_value("First", "Second")

with connection.cursor() as cursor:
cursor.execute(
"select __arches_get_node_display_value(%s::jsonb, %s::uuid, 'en');",
[
json.dumps({reference_nodeid: reference_tile_value}),
reference_nodeid,
],
)
self.assertEqual(cursor.fetchone(), ("First, Second",))
Loading