Skip to content

Commit c86f547

Browse files
Update sourceLayer on data library derived layers when updating
1 parent 7fd4531 commit c86f547

File tree

2 files changed

+462
-46
lines changed

2 files changed

+462
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
--! Previous: sha1:88ea156673a7d82b63e484ab0d633bddc174a5e0
2+
--! Hash: sha1:7b3014cc9d2637d62f0fee1b2ea18fb6d5266dc1
3+
4+
-- Enter migration here
5+
CREATE OR REPLACE FUNCTION public.replace_data_source(data_layer_id integer, data_source_id integer, source_layer text, bounds numeric[], gl_styles jsonb) RETURNS void
6+
LANGUAGE plpgsql SECURITY DEFINER
7+
AS $$
8+
declare
9+
old_source_id integer;
10+
old_source_type text;
11+
old_metadata_is_dynamic boolean;
12+
dl_template_id text;
13+
begin
14+
-- first, determine if a related table_of_contents_item has
15+
-- data_library_template_id set. If so, we need to update the
16+
-- related Toc items that have copied_from_data_library_template_id
17+
-- matching.
18+
19+
select data_library_template_id into dl_template_id from table_of_contents_items where table_of_contents_items.data_layer_id = replace_data_source.data_layer_id and data_library_template_id is not null limit 1;
20+
21+
if dl_template_id is null then
22+
raise exception 'fuck you %', dl_template_id;
23+
end if;
24+
25+
select data_layers.data_source_id into old_source_id from data_layers where id = replace_data_source.data_layer_id;
26+
select type into old_source_type from data_sources where id = old_source_id;
27+
select metadata is null and (old_source_type = 'arcgis-vector' or old_source_type = 'arcgis-dynamic-mapserver') into old_metadata_is_dynamic from table_of_contents_items where table_of_contents_items.data_layer_id = replace_data_source.data_layer_id limit 1;
28+
insert into archived_data_sources (
29+
data_source_id,
30+
data_layer_id,
31+
version,
32+
mapbox_gl_style,
33+
changelog,
34+
source_layer,
35+
bounds,
36+
sublayer,
37+
sublayer_type,
38+
dynamic_metadata,
39+
project_id
40+
) values (
41+
old_source_id,
42+
replace_data_source.data_layer_id,
43+
(
44+
select
45+
coalesce(max(version), 0) + 1
46+
from
47+
archived_data_sources
48+
where archived_data_sources.data_layer_id = replace_data_source.data_layer_id
49+
),
50+
(
51+
select
52+
mapbox_gl_styles
53+
from
54+
data_layers
55+
where id = replace_data_source.data_layer_id
56+
),
57+
(select changelog from data_sources where id = replace_data_source.data_source_id),
58+
(select data_layers.source_layer from data_layers where data_layers.id = replace_data_source.data_layer_id),
59+
(select table_of_contents_items.bounds from table_of_contents_items where table_of_contents_items.data_layer_id = replace_data_source.data_layer_id and table_of_contents_items.bounds is not null limit 1),
60+
(select sublayer from data_layers where id = data_layer_id),
61+
(select sublayer_type from data_layers where id = data_layer_id),
62+
old_metadata_is_dynamic,
63+
(select project_id from data_sources where id = replace_data_source.data_source_id)
64+
);
65+
66+
if dl_template_id is not null then
67+
update
68+
data_sources
69+
set data_library_template_id = dl_template_id
70+
where
71+
id = replace_data_source.data_source_id or
72+
id = any((
73+
select
74+
data_layers.data_source_id
75+
from
76+
data_layers
77+
where
78+
id = any (
79+
select
80+
table_of_contents_items.data_layer_id
81+
from
82+
table_of_contents_items
83+
where
84+
copied_from_data_library_template_id = dl_template_id or
85+
data_library_template_id = dl_template_id
86+
)
87+
)) or id = any ((
88+
select
89+
data_layers.data_source_id
90+
from
91+
data_layers
92+
where
93+
id = replace_data_source.data_layer_id
94+
));
95+
end if;
96+
97+
update
98+
data_layers
99+
set
100+
data_source_id = replace_data_source.data_source_id,
101+
source_layer = replace_data_source.source_layer,
102+
mapbox_gl_styles = coalesce(
103+
gl_styles, data_layers.mapbox_gl_styles
104+
),
105+
sublayer = null
106+
where
107+
id = replace_data_source.data_layer_id;
108+
109+
if dl_template_id is not null then
110+
update
111+
data_layers
112+
set
113+
data_source_id = replace_data_source.data_source_id,
114+
source_layer = replace_data_source.source_layer
115+
where
116+
id = any (
117+
select table_of_contents_items.data_layer_id from table_of_contents_items where copied_from_data_library_template_id = dl_template_id
118+
);
119+
end if;
120+
121+
update
122+
table_of_contents_items
123+
set bounds = replace_data_source.bounds
124+
where
125+
table_of_contents_items.data_layer_id = replace_data_source.data_layer_id or (
126+
case
127+
when dl_template_id is not null then copied_from_data_library_template_id = dl_template_id
128+
else false
129+
end
130+
);
131+
end;
132+
$$;

0 commit comments

Comments
 (0)