This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtree-element-mapper-links.sql
163 lines (147 loc) · 4.61 KB
/
tree-element-mapper-links.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
DROP INDEX IF EXISTS identifier_id_version_object_index;
CREATE INDEX identifier_id_version_object_index
ON mapper.identifier (id_number, object_type, version_number);
DROP INDEX IF EXISTS identifier_object_type_index;
CREATE INDEX identifier_object_type_index
ON mapper.identifier (object_type);
-- add all tree_element identifiers
INSERT INTO mapper.identifier (id, id_number, version_number, name_space, object_type, deleted, reason_deleted, updated_at, updated_by, preferred_uri_id)
SELECT
nextval('mapper.mapper_sequence'),
tree_element_id,
tree_version_id,
'apni',
'treeElement',
FALSE,
NULL,
now(),
'pmcneil',
NULL
FROM tree_version_element;
-- create default match preferred uris /tree/treeVersionId/ElementId
INSERT INTO mapper.match (id, uri, deprecated, updated_at, updated_by)
SELECT
nextval('mapper.mapper_sequence'),
('tree/' || version_number || '/' || id_number :: TEXT),
FALSE,
now(),
'pmcneil'
FROM mapper.identifier
WHERE object_type = 'treeElement';
UPDATE mapper.identifier i
SET preferred_uri_id = m.id
FROM mapper.match m
WHERE i.object_type = 'treeElement'
AND m.uri = ('tree/' || i.version_number || '/' || i.id_number);
-- Add tree version links
INSERT INTO mapper.identifier (id, id_number, version_number, name_space, object_type, deleted, reason_deleted, updated_at, updated_by, preferred_uri_id)
SELECT
nextval('mapper.mapper_sequence'),
id,
NULL,
'apni',
'treeVersion',
FALSE,
NULL,
now(),
'pmcneil',
NULL
FROM tree_version;
-- create default match uri /tree/versionId
INSERT INTO mapper.match (id, uri, deprecated, updated_at, updated_by)
SELECT
nextval('mapper.mapper_sequence'),
('tree/' || id_number),
FALSE,
now(),
'pmcneil'
FROM mapper.identifier
WHERE object_type = 'treeVersion';
UPDATE mapper.identifier i
SET preferred_uri_id = m.id
FROM mapper.match m
WHERE i.object_type = 'treeVersion'
AND m.uri = ('tree/' || i.id_number);
-- Add tree links
INSERT INTO mapper.identifier (id, id_number, version_number, name_space, object_type, deleted, reason_deleted, updated_at, updated_by, preferred_uri_id)
SELECT
nextval('mapper.mapper_sequence'),
id,
NULL,
'apni',
'tree',
FALSE,
NULL,
now(),
'pmcneil',
NULL
FROM tree;
-- create default match uri /tree/namespace/tree.name
INSERT INTO mapper.match (id, uri, deprecated, updated_at, updated_by)
SELECT
nextval('mapper.mapper_sequence'),
('tree/' || i.name_space || '/' || t.name),
FALSE,
now(),
'pmcneil'
FROM tree t
JOIN mapper.identifier i ON t.id = i.id_number AND i.object_type = 'tree';
UPDATE mapper.identifier i
SET preferred_uri_id = m.id
FROM tree t, mapper.match m
WHERE i.object_type = 'tree'
AND i.id_number = t.id
AND m.uri = ('tree/' || i.name_space || '/' || t.name);
--
-- map old node ids to new tree ids
DROP FUNCTION IF EXISTS map_nodes_element_identifiers();
CREATE FUNCTION map_nodes_element_identifiers()
RETURNS TABLE(tree_element_id BIGINT, node_mid_id BIGINT, elem_mid_id BIGINT, tree_version BIGINT)
LANGUAGE SQL
AS $$
SELECT
ipath.id,
node_mid.id AS node_id,
elem_mid.id AS elem_id,
max(tvte.tree_version_id)
FROM instance_paths ipath
JOIN tree_version_element tvte ON tvte.tree_element_id = ipath.id
,
jsonb_array_elements(ipath.nodes) AS node,
mapper.identifier node_mid,
mapper.identifier elem_mid
WHERE to_jsonb(node_mid.id_number) = node
AND node_mid.object_type = 'node'
AND elem_mid.id_number = ipath.id
AND elem_mid.version_number = tvte.tree_version_id
AND elem_mid.object_type = 'treeElement'
GROUP BY ipath.id, node_mid.id, elem_mid.id, node;
$$;
-- UPDATE mapper.identifier_identities ii
-- SET identifier_id = mids.elem_mid_id
-- FROM map_nodes_element_identifiers() mids
-- WHERE ii.identifier_id = mids.node_mid_id;
-- global updates
-- join matches to identifiers
INSERT INTO mapper.identifier_identities (match_id, identifier_id)
SELECT
m.id,
i.id
FROM mapper.identifier i
JOIN mapper.match m ON i.preferred_uri_id = m.id
WHERE i.object_type LIKE 'tree%'
AND NOT exists(SELECT 1
FROM mapper.identifier_identities ii
WHERE ii.identifier_id = i.id AND ii.match_id = m.id);
-- add the default host to all matches that don't have it.
INSERT INTO mapper.match_host (match_hosts_id, host_id)
SELECT
m.id,
(SELECT h.id
FROM mapper.host h
WHERE h.preferred)
FROM mapper.match m
WHERE
NOT exists(SELECT 1
FROM mapper.match_host mh
WHERE mh.match_hosts_id = m.id);