From 7efb3b81bd2e99e0626bd16376d362d78cb9d2b7 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 20 Sep 2020 18:46:18 -0600 Subject: [PATCH] Fix program sequences db view --- .../migrations/0006-fix_view_program_seq-down.sql | 8 ++++++++ .../migrations/0006-fix_view_program_seq-up.sql | 15 +++++++++++++++ src/database/migrations/mod.rs | 1 + 3 files changed, 24 insertions(+) create mode 100644 src/database/migrations/0006-fix_view_program_seq-down.sql create mode 100644 src/database/migrations/0006-fix_view_program_seq-up.sql diff --git a/src/database/migrations/0006-fix_view_program_seq-down.sql b/src/database/migrations/0006-fix_view_program_seq-down.sql new file mode 100644 index 0000000..13a8bbd --- /dev/null +++ b/src/database/migrations/0006-fix_view_program_seq-down.sql @@ -0,0 +1,8 @@ +DROP VIEW program_sequences; + +CREATE VIEW program_sequences AS +SELECT psi.program_id program_id, + json_group_array(json_object( + 'section_id', psi.section_id, + 'duration', psi.duration)) sequence +FROM program_sequence_items as psi; diff --git a/src/database/migrations/0006-fix_view_program_seq-up.sql b/src/database/migrations/0006-fix_view_program_seq-up.sql new file mode 100644 index 0000000..7873c54 --- /dev/null +++ b/src/database/migrations/0006-fix_view_program_seq-up.sql @@ -0,0 +1,15 @@ +DROP VIEW program_sequences; + +CREATE VIEW program_sequences AS +WITH psi_sorted AS ( + SELECT psi.program_id program_id, + json_object( + 'section_id', psi.section_id, + 'duration', psi.duration) + obj + FROM program_sequence_items AS psi + ORDER BY psi.program_id, psi.seq_num) +SELECT psi_sorted.program_id program_id, + json_group_array(json(psi_sorted.obj)) sequence +FROM psi_sorted +GROUP BY psi_sorted.program_id; diff --git a/src/database/migrations/mod.rs b/src/database/migrations/mod.rs index 6aa06a1..900c7d1 100644 --- a/src/database/migrations/mod.rs +++ b/src/database/migrations/mod.rs @@ -17,6 +17,7 @@ pub fn create_migrations() -> Migrations { migs.add(include_file_migration!(3, "0003-table_programs")); migs.add(include_file_migration!(4, "0004-program_rows")); migs.add(include_file_migration!(5, "0005-view_program_sequence")); + migs.add(include_file_migration!(6, "0006-fix_view_program_seq")); // INSERT MIGRATION ABOVE -- DO NOT EDIT THIS COMMENT migs } \ No newline at end of file