diff --git a/deployment/hasura/migrations/Aerie/8_span_event_linkage/down.sql b/deployment/hasura/migrations/Aerie/8_span_event_linkage/down.sql new file mode 100644 index 0000000000..372f81022e --- /dev/null +++ b/deployment/hasura/migrations/Aerie/8_span_event_linkage/down.sql @@ -0,0 +1,51 @@ +-- event table revert +alter table merlin.event + drop column span_id; + +-- span table revert +alter table merlin.span + alter column span_id add generated by default as identity; + +select setval(pg_get_serial_sequence('merlin.span', 'span_id'), coalesce(max(span_id),0) + 1, false) FROM merlin.span; + +with conflicts as ( + with subTable as ( + select p.span_id, + p.dataset_id, + ROW_NUMBER() over(partition by p.span_id order by p.dataset_id) as rk + from merlin.span p) + select s.* + from subTable s + where s.rk > 1) +update merlin.span s + set span_id = nextval(pg_get_serial_sequence('merlin.span', 'span_id')) + from conflicts c + where (c.span_id, c.dataset_id) = (s.span_id, s.dataset_id); + +alter table merlin.span + rename column span_id to id; + +alter table merlin.span + alter column id set generated always; + +drop view merlin.simulated_activity; +create view merlin.simulated_activity as +( + select span.id as id, + sd.id as simulation_dataset_id, + span.parent_id as parent_id, + span.start_offset as start_offset, + span.duration as duration, + span.attributes as attributes, + span.type as activity_type_name, + (span.attributes#>>'{directiveId}')::integer as directive_id, + sd.simulation_start_time + span.start_offset as start_time, + sd.simulation_start_time + span.start_offset + span.duration as end_time + from merlin.span span + join merlin.dataset d on span.dataset_id = d.id + join merlin.simulation_dataset sd on d.id = sd.dataset_id + join merlin.simulation s on s.id = sd.simulation_id +); + +call migrations.mark_migration_rolled_back('8') + diff --git a/deployment/hasura/migrations/Aerie/8_span_event_linkage/up.sql b/deployment/hasura/migrations/Aerie/8_span_event_linkage/up.sql new file mode 100644 index 0000000000..f7609c2f29 --- /dev/null +++ b/deployment/hasura/migrations/Aerie/8_span_event_linkage/up.sql @@ -0,0 +1,31 @@ +-- Span Table update +alter table merlin.span + rename column id to span_id; + +alter table merlin.span + alter column span_id drop identity; + +drop view merlin.simulated_activity; +create view merlin.simulated_activity as +( +select span.span_id as id, + sd.id as simulation_dataset_id, + span.parent_id as parent_id, + span.start_offset as start_offset, + span.duration as duration, + span.attributes as attributes, + span.type as activity_type_name, + (span.attributes#>>'{directiveId}')::integer as directive_id, + sd.simulation_start_time + span.start_offset as start_time, + sd.simulation_start_time + span.start_offset + span.duration as end_time +from merlin.span span + join merlin.dataset d on span.dataset_id = d.id + join merlin.simulation_dataset sd on d.id = sd.dataset_id + join merlin.simulation s on s.id = sd.simulation_id + ); + +-- event table update +alter table merlin.event + add column span_id integer; + +call migrations.mark_migration_applied('8') diff --git a/deployment/postgres-init-db/sql/applied_migrations.sql b/deployment/postgres-init-db/sql/applied_migrations.sql index ce2b97f16e..064eafae07 100644 --- a/deployment/postgres-init-db/sql/applied_migrations.sql +++ b/deployment/postgres-init-db/sql/applied_migrations.sql @@ -10,3 +10,4 @@ call migrations.mark_migration_applied('4'); call migrations.mark_migration_applied('5'); call migrations.mark_migration_applied('6'); call migrations.mark_migration_applied('7'); +call migrations.mark_migration_applied('8');