Skip to content
Open
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: 3 additions & 1 deletion lib/timescaledb/migration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def create_hypertable(table_name,
# SQL
#
def create_continuous_aggregate(table_name, query, **options)
raise ArgumentError, 'query must be a string' unless query.is_a?(String)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thinking about old migrations that are already in place.

Suggested change
raise ArgumentError, 'query must be a string' unless query.is_a?(String)
query = query.to_sql if query.respond_to(:to_sql)
raise ArgumentError, 'query must be a string or implement the `to_sql` method ' unless query.is_a?(String)

Copy link
Author

@exterm exterm May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right to think about backwards compatibility, but if I apply your changes then this PR fixes nothing.

The problem is caused by allowing people to pass an ActiveRecord::Relation as an input to create_continuous_aggregate.

If you want to preserve backwards compatibility for now, we could look into making it a warning instead of an exception.


execute <<~SQL
CREATE MATERIALIZED VIEW #{table_name}
WITH (
Expand All @@ -107,7 +109,7 @@ def create_continuous_aggregate(table_name, query, **options)
#{build_with_clause_option_string(:create_group_indexes, options)}
#{build_with_clause_option_string(:finalized, options)}
) AS
#{query.respond_to?(:to_sql) ? query.to_sql : query}
#{query}
WITH #{'NO' unless options[:with_data]} DATA;
SQL

Expand Down