Skip to content

Commit 1441131

Browse files
Reversible create_continuous_aggregate
1 parent 81d2f97 commit 1441131

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

lib/timescaledb.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require_relative 'timescaledb/stats'
1919
require_relative 'timescaledb/stats_report'
2020
require_relative 'timescaledb/migration_helpers'
21+
require_relative 'timescaledb/command_recorder'
2122
require_relative 'timescaledb/extension'
2223
require_relative 'timescaledb/version'
2324

lib/timescaledb/command_recorder.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Useful methods to run TimescaleDB in you Ruby app.
2+
module Timescaledb
3+
# Migration helpers can help you to setup hypertables by default.
4+
module CommandRecorder
5+
def create_continuous_aggregates(*args)
6+
record(:create_continuous_aggregates, args)
7+
end
8+
9+
def invert_create_continuous_aggregates(args)
10+
[:drop_continuous_aggregates, args.first]
11+
end
12+
end
13+
end
14+
ActiveRecord::Migration::CommandRecorder.include Timescaledb::CommandRecorder

lib/timescaledb/migration_helpers.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,18 @@ def create_continuous_aggregate(table_name, query, **options)
114114
create_continuous_aggregate_policy(table_name, **(options[:refresh_policies] || {}))
115115
end
116116

117+
alias_method :create_continuous_aggregates, :create_continuous_aggregate
118+
117119
# Drop a new continuous aggregate.
118120
#
119121
# It basically DROP MATERIALIZED VIEW for a given @name.
120122
#
121123
# @param name [String, Symbol] The name of the continuous aggregate view.
122-
def drop_continuous_aggregates view_name
124+
def drop_continuous_aggregate view_name
123125
execute "DROP MATERIALIZED VIEW #{view_name}"
124126
end
125127

126-
alias_method :create_continuous_aggregates, :create_continuous_aggregate
128+
alias_method :drop_continuous_aggregates, :drop_continuous_aggregate
127129

128130
def create_continuous_aggregate_policy(table_name, **options)
129131
return if options.empty?

spec/timescaledb/migration_helper_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
RSpec.describe Timescaledb::MigrationHelpers, database_cleaner_strategy: :truncation do
2+
context 'reversible' do
3+
let(:con) { ActiveRecord::Base.connection }
4+
5+
before(:each) do
6+
con.drop_table :ticks, if_exists: true, force: :cascade
7+
con.create_table :ticks, hypertable: hypertable_options, id: false do |t|
8+
t.string :symbol
9+
t.decimal :price
10+
t.integer :volume
11+
t.timestamps
12+
end
13+
end
14+
15+
let(:hypertable_options) do
16+
{
17+
time_column: 'created_at',
18+
chunk_time_interval: '1 min',
19+
compress_segmentby: 'symbol',
20+
compress_orderby: 'created_at',
21+
compress_after: '7 days'
22+
}
23+
end
24+
25+
subject(:migration) do
26+
Class.new(ActiveRecord::Migration::Current) do
27+
def change
28+
query = <<~SQL
29+
SELECT time_bucket('1m', created_at) as time,
30+
symbol,
31+
FIRST(price, created_at) as open,
32+
MAX(price) as high,
33+
MIN(price) as low,
34+
LAST(price, created_at) as close,
35+
SUM(volume) as volume FROM "ticks" GROUP BY 1,2
36+
SQL
37+
create_continuous_aggregates('ohlc_1m', query, with_data: true)
38+
end
39+
end.new
40+
end
41+
42+
it do
43+
expect(con).to receive(:create_continuous_aggregates).once.and_call_original
44+
expect(con).to receive(:drop_continuous_aggregates).once.and_call_original
45+
migration.migrate(:up)
46+
migration.migrate(:down)
47+
end
48+
end
49+
250
describe ".create_table" do
351
let(:con) { ActiveRecord::Base.connection }
452

0 commit comments

Comments
 (0)