-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing some features but working okay
- Loading branch information
1 parent
4bb65f4
commit b696481
Showing
10 changed files
with
767 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,346 @@ | ||
--! Previous: sha1:e6c5fc331e8c572d3e8b7f38f6049415b3e7a7f3 | ||
--! Hash: sha1:38f62a9cfc5d9c555f68f320b486722c2454c64c | ||
|
||
-- Enter migration here | ||
CREATE OR REPLACE FUNCTION public.publish_table_of_contents("projectId" integer) RETURNS SETOF public.table_of_contents_items | ||
LANGUAGE plpgsql SECURITY DEFINER | ||
AS $$ | ||
declare | ||
lid int; | ||
item table_of_contents_items; | ||
source_id int; | ||
copied_source_id int; | ||
acl_type access_control_list_type; | ||
acl_id int; | ||
orig_acl_id int; | ||
new_toc_id int; | ||
new_interactivity_settings_id int; | ||
begin | ||
-- check permissions | ||
if session_is_admin("projectId") = false then | ||
raise 'Permission denied. Must be a project admin'; | ||
end if; | ||
-- delete existing published table of contents items, layers, sources, and interactivity settings | ||
delete from | ||
interactivity_settings | ||
where | ||
id in ( | ||
select | ||
data_layers.interactivity_settings_id | ||
from | ||
data_layers | ||
inner JOIN | ||
table_of_contents_items | ||
on | ||
data_layers.id = table_of_contents_items.data_layer_id | ||
where | ||
table_of_contents_items.project_id = "projectId" and | ||
is_draft = false | ||
); | ||
|
||
delete from data_sources where data_sources.id in ( | ||
select | ||
data_source_id | ||
from | ||
data_layers | ||
inner JOIN | ||
table_of_contents_items | ||
on | ||
data_layers.id = table_of_contents_items.data_layer_id | ||
where | ||
table_of_contents_items.project_id = "projectId" and | ||
is_draft = false | ||
); | ||
delete from data_layers where id in ( | ||
select | ||
data_layer_id | ||
from | ||
table_of_contents_items | ||
where | ||
project_id = "projectId" and | ||
is_draft = false | ||
); | ||
delete from | ||
table_of_contents_items | ||
where | ||
project_id = "projectId" and | ||
is_draft = false; | ||
|
||
-- one-by-one, copy related layers and link table of contents items | ||
for item in | ||
select | ||
* | ||
from | ||
table_of_contents_items | ||
where | ||
is_draft = true and | ||
project_id = "projectId" | ||
loop | ||
if item.is_folder = false then | ||
-- copy interactivity settings first | ||
insert into interactivity_settings ( | ||
type, | ||
short_template, | ||
long_template, | ||
cursor, | ||
title | ||
) select | ||
type, | ||
short_template, | ||
long_template, | ||
cursor, | ||
title | ||
from | ||
interactivity_settings | ||
where | ||
interactivity_settings.id = ( | ||
select interactivity_settings_id from data_layers where data_layers.id = item.data_layer_id | ||
) | ||
returning | ||
id | ||
into | ||
new_interactivity_settings_id; | ||
|
||
insert into data_layers ( | ||
project_id, | ||
data_source_id, | ||
source_layer, | ||
sublayer, | ||
render_under, | ||
mapbox_gl_styles, | ||
interactivity_settings_id, | ||
z_index | ||
) | ||
select "projectId", | ||
data_source_id, | ||
source_layer, | ||
sublayer, | ||
render_under, | ||
mapbox_gl_styles, | ||
new_interactivity_settings_id, | ||
z_index | ||
from | ||
data_layers | ||
where | ||
id = item.data_layer_id | ||
returning id into lid; | ||
else | ||
lid = item.data_layer_id; | ||
end if; | ||
-- TODO: this will have to be modified with the addition of any columns | ||
insert into table_of_contents_items ( | ||
is_draft, | ||
project_id, | ||
path, | ||
stable_id, | ||
parent_stable_id, | ||
title, | ||
is_folder, | ||
show_radio_children, | ||
is_click_off_only, | ||
metadata, | ||
bounds, | ||
data_layer_id, | ||
sort_index, | ||
hide_children, | ||
geoprocessing_reference_id, | ||
translated_props | ||
) values ( | ||
false, | ||
"projectId", | ||
item.path, | ||
item.stable_id, | ||
item.parent_stable_id, | ||
item.title, | ||
item.is_folder, | ||
item.show_radio_children, | ||
item.is_click_off_only, | ||
item.metadata, | ||
item.bounds, | ||
lid, | ||
item.sort_index, | ||
item.hide_children, | ||
item.geoprocessing_reference_id, | ||
item.translated_props | ||
) returning id into new_toc_id; | ||
select | ||
type, id into acl_type, orig_acl_id | ||
from | ||
access_control_lists | ||
where | ||
table_of_contents_item_id = ( | ||
select | ||
id | ||
from | ||
table_of_contents_items | ||
where is_draft = true and stable_id = item.stable_id | ||
); | ||
-- copy access control list settings | ||
if acl_type != 'public' then | ||
update | ||
access_control_lists | ||
set type = acl_type | ||
where table_of_contents_item_id = new_toc_id | ||
returning id into acl_id; | ||
if acl_type = 'group' then | ||
insert into | ||
access_control_list_groups ( | ||
access_control_list_id, | ||
group_id | ||
) | ||
select | ||
acl_id, | ||
group_id | ||
from | ||
access_control_list_groups | ||
where | ||
access_control_list_id = orig_acl_id; | ||
end if; | ||
end if; | ||
end loop; | ||
-- one-by-one, copy related sources and update foreign keys of layers | ||
for source_id in | ||
select distinct(data_source_id) from data_layers where id in ( | ||
select | ||
data_layer_id | ||
from | ||
table_of_contents_items | ||
where | ||
is_draft = false and | ||
project_id = "projectId" and | ||
is_folder = false | ||
) | ||
loop | ||
-- TODO: This function will have to be updated whenever the schema | ||
-- changes since these columns are hard coded... no way around it. | ||
insert into data_sources ( | ||
project_id, | ||
type, | ||
attribution, | ||
bounds, | ||
maxzoom, | ||
minzoom, | ||
url, | ||
scheme, | ||
tiles, | ||
tile_size, | ||
encoding, | ||
buffer, | ||
cluster, | ||
cluster_max_zoom, | ||
cluster_properties, | ||
cluster_radius, | ||
generate_id, | ||
line_metrics, | ||
promote_id, | ||
tolerance, | ||
coordinates, | ||
urls, | ||
query_parameters, | ||
use_device_pixel_ratio, | ||
import_type, | ||
original_source_url, | ||
enhanced_security, | ||
byte_length, | ||
supports_dynamic_layers, | ||
uploaded_source_filename, | ||
uploaded_source_layername, | ||
normalized_source_object_key, | ||
normalized_source_bytes, | ||
geostats, | ||
upload_task_id, | ||
translated_props, | ||
arcgis_fetch_strategy | ||
) | ||
select | ||
"projectId", | ||
type, | ||
attribution, | ||
bounds, | ||
maxzoom, | ||
minzoom, | ||
url, | ||
scheme, | ||
tiles, | ||
tile_size, | ||
encoding, | ||
buffer, | ||
cluster, | ||
cluster_max_zoom, | ||
cluster_properties, | ||
cluster_radius, | ||
generate_id, | ||
line_metrics, | ||
promote_id, | ||
tolerance, | ||
coordinates, | ||
urls, | ||
query_parameters, | ||
use_device_pixel_ratio, | ||
import_type, | ||
original_source_url, | ||
enhanced_security, | ||
byte_length, | ||
supports_dynamic_layers, | ||
uploaded_source_filename, | ||
uploaded_source_layername, | ||
normalized_source_object_key, | ||
normalized_source_bytes, | ||
geostats, | ||
upload_task_id, | ||
translated_props, | ||
arcgis_fetch_strategy | ||
from | ||
data_sources | ||
where | ||
id = source_id | ||
returning id into copied_source_id; | ||
-- update data_layers that should now reference the copy | ||
update | ||
data_layers | ||
set data_source_id = copied_source_id | ||
where | ||
data_source_id = source_id and | ||
data_layers.id in (( | ||
select distinct(data_layer_id) from table_of_contents_items where is_draft = false and | ||
project_id = "projectId" and | ||
is_folder = false | ||
)); | ||
end loop; | ||
update | ||
projects | ||
set | ||
draft_table_of_contents_has_changes = false, | ||
table_of_contents_last_published = now() | ||
where | ||
id = "projectId"; | ||
-- return items | ||
return query select * from table_of_contents_items | ||
where project_id = "projectId" and is_draft = false; | ||
end; | ||
$$; | ||
|
||
|
||
create or replace function interactivity_settings_update_draft_toc_has_changes() | ||
returns trigger | ||
security definer | ||
as | ||
$$ | ||
begin | ||
if tg_op = 'INSERT' or tg_op = 'UPDATE' or tg_op = 'DELETE' then | ||
update projects set draft_table_of_contents_has_changes = true where id = ( | ||
select project_id from table_of_contents_items where data_layer_id = any( | ||
select id from data_layers where interactivity_settings_id = NEW.id | ||
) | ||
); | ||
end if; | ||
return NEW; | ||
end; | ||
$$ language plpgsql; | ||
|
||
drop trigger if exists interactivity_settings_update_draft_toc_has_changes_trigger on interactivity_settings; | ||
create trigger interactivity_settings_update_draft_toc_has_changes_trigger | ||
after update | ||
on interactivity_settings | ||
for each row | ||
execute procedure interactivity_settings_update_draft_toc_has_changes(); |
Oops, something went wrong.