From 52a3123c8eec4fdb28018e7739f59169c98e6d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B4natas=20Davi=20Paganini?= Date: Fri, 13 Sep 2024 22:04:44 -0300 Subject: [PATCH] Drop continuous aggregates helper --- .../continuous_aggregates_helper.rb | 9 ++++++++ .../continuous_aggregates_helper_spec.rb | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/timescaledb/continuous_aggregates_helper.rb b/lib/timescaledb/continuous_aggregates_helper.rb index 542f012..988ca1a 100644 --- a/lib/timescaledb/continuous_aggregates_helper.rb +++ b/lib/timescaledb/continuous_aggregates_helper.rb @@ -91,6 +91,15 @@ def apply_rollup_rules(select_values) end end + def drop_continuous_aggregates + @aggregates.each do |aggregate_name, _| + @timeframes.reverse_each do |timeframe| + view_name = "#{aggregate_name}_per_#{timeframe}" + connection.execute("DROP MATERIALIZED VIEW IF EXISTS #{view_name} CASCADE") + end + end + end + private def define_continuous_aggregate_classes diff --git a/spec/timescaledb/continuous_aggregates_helper_spec.rb b/spec/timescaledb/continuous_aggregates_helper_spec.rb index f521d41..5f01a38 100644 --- a/spec/timescaledb/continuous_aggregates_helper_spec.rb +++ b/spec/timescaledb/continuous_aggregates_helper_spec.rb @@ -152,4 +152,27 @@ class Download < ActiveRecord::Base end end end + + describe '.drop_continuous_aggregates' do + before do + allow(ActiveRecord::Base.connection).to receive(:execute).and_call_original + end + it 'drops all continuous aggregates' do + test_class.drop_continuous_aggregates + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS total_downloads_per_month CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS total_downloads_per_day CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS total_downloads_per_hour CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS total_downloads_per_minute CASCADE/i) + + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_gem_per_month CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_gem_per_day CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_gem_per_hour CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_gem_per_minute CASCADE/i) + + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_version_per_month CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_version_per_day CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_version_per_hour CASCADE/i) + expect(ActiveRecord::Base.connection).to have_received(:execute).with(/DROP MATERIALIZED VIEW IF EXISTS downloads_by_version_per_minute CASCADE/i) + end + end end