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

Add materialized view support helpers. #1004

Merged
merged 1 commit into from
Mar 9, 2024
Merged

Add materialized view support helpers. #1004

merged 1 commit into from
Mar 9, 2024

Conversation

jwoertink
Copy link
Member

Fixes #842

Avram already supports views and materialized views by way of writing custom SQL for the migration, and specifying view in your model.

This PR gives you an easier way to see when a view model is materialized, as well as a handy helper method to refresh the materialized view.

class Report < BaseModel
  view materialized: true do
    column count : Int64
  end
end

# refreshes the materialized view
ReportQuery.refresh_view

Comment on lines +17 to +20
{% if named_args[:materialized_view] %}
def self.refresh_view(*, concurrent : Bool = false)
{{ type }}.database.exec("REFRESH MATERIALIZED VIEW #{concurrent ? "CONCURRENTLY" : ""} #{{{ type }}.table_name}")
end
Copy link
Member Author

Choose a reason for hiding this comment

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

If you don't pass in this arg, should I still define the method, but raise an exception that tells you the model isn't defined as a materialzed view? 🤔

Comment on lines +2 to +4
view :all_the_names, materialized: true do
column name : String
end
Copy link
Member Author

Choose a reason for hiding this comment

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

These technically fail on the SchemaEnforcer... Should materialized views call skip_schema_enforcer by default, or would it be better to figure out why they fail?

@jwoertink jwoertink merged commit 68cc0c6 into main Mar 9, 2024
9 checks passed
@jwoertink jwoertink deleted the issues/842 branch March 9, 2024 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add macro helpers + documentation for supporting materialized views
1 participant