Skip to content

Commit

Permalink
Drop continuous aggregates helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatas committed Sep 14, 2024
1 parent 24f6a51 commit 52a3123
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/timescaledb/continuous_aggregates_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions spec/timescaledb/continuous_aggregates_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 52a3123

Please sign in to comment.