Skip to content

Commit

Permalink
Include a db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
goetzrrGit committed Jul 15, 2024
1 parent 9ccbc51 commit f7d1294
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
51 changes: 51 additions & 0 deletions deployment/hasura/migrations/Aerie/8_span_event_linkage/down.sql
Original file line number Diff line number Diff line change
@@ -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')

31 changes: 31 additions & 0 deletions deployment/hasura/migrations/Aerie/8_span_event_linkage/up.sql
Original file line number Diff line number Diff line change
@@ -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')
1 change: 1 addition & 0 deletions deployment/postgres-init-db/sql/applied_migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');

0 comments on commit f7d1294

Please sign in to comment.