Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenic extension prepend instead of include #63

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.scenic.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
timescaledb (0.2.7)
timescaledb (0.2.8)
activerecord
activesupport
pg (~> 1.2)
Expand Down Expand Up @@ -56,7 +56,7 @@ GEM
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
pg (1.4.5)
pg (1.5.6)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down
34 changes: 29 additions & 5 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ end
This example shows a ticks table grouping ticks as OHLCV histograms for every
minute.

First make sure you have the model with the `acts_as_hypertable` method to be
able to extract the query from it.

```ruby
class Tick < ActiveRecord::Base
self.table_name = 'ticks'
acts_as_hypertable
end
```

Then, inside your migration:

```ruby
hypertable_options = {
time_column: 'created_at',
Expand All @@ -39,11 +51,6 @@ create_table :ticks, hypertable: hypertable_options, id: false do |t|
t.integer :volume
t.timestamps
end
Tick = Class.new(ActiveRecord::Base) do
self.table_name = 'ticks'
self.primary_key = 'symbol'
acts_as_hypertable
end

query = Tick.select(<<~QUERY)
time_bucket('1m', created_at) as time,
Expand Down Expand Up @@ -73,4 +80,21 @@ If you're interested in candlesticks and need to get the OHLC values, take a loo
at the [toolkit ohlc](/toolkit_ohlc) function that do the same but through a
function that can be reusing candlesticks from smaller timeframes.

!!! note "Disable ddl transactions in your migration to start with data"

If you want to start `with_data: true`, remember that you'll need to
`disable_ddl_transaction!` in your migration file.

```ruby
class CreateCaggsWithData < ActiveRecord::Migration[7.0]
disable_ddl_transaction!

def change
create_continuous_aggregate('ohlc_1m', query, with_data: true)
# ...
end
end
```


[1]: https://ideia.me/timescale-continuous-aggregates-with-ruby
3 changes: 1 addition & 2 deletions lib/timescaledb/scenic/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def create_materialized_view(name, sql_definition, with: nil, no_data: false)

# @override Scenic::Adapters::Postgres#create_view
# to add the `with: ` keyword that can be used for such option.
#
def create_view(name, version: nil, with: nil, sql_definition: nil, materialized: false, no_data: false)
if version.present? && sql_definition.present?
raise(
Expand Down Expand Up @@ -69,4 +68,4 @@ def create_scenic_continuous_aggregate(name)


Scenic::Adapters::Postgres.include(Timescaledb::Scenic::Extension)
ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Timescaledb::Scenic::MigrationHelpers)
ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(Timescaledb::Scenic::MigrationHelpers)
Loading