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

Make it possible to use create_or_replace_view with materialized view. #2144

Merged
merged 1 commit into from
Mar 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/sequel/database/schema_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ def create_table_generator(&block)
# For databases where replacing a view is not natively supported, support
# is emulated by dropping a view with the same name before creating the view.
def create_or_replace_view(name, source, options = OPTS)
if supports_create_or_replace_view?
if supports_create_or_replace_view? && !options[:materialized]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if it's too specific for postgres and we should have separate create_or_replace_view specifically for postgres. let me know if you want to change that

options = options.merge(:replace=>true)
else
swallow_database_error{drop_view(name)}
swallow_database_error{drop_view(name, options)}
end

create_view(name, source, options)
Expand Down
9 changes: 9 additions & 0 deletions spec/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,15 @@ def c.exec_prepared(*); super; nil end
@db.views(@opts).must_include :items_view
end if DB.server_version >= 90300

it "should support replacing materialized views" do
@opts = {:materialized=>true}
@db[:items].insert(15)
@db.create_or_replace_view(:items_view, @db[:items].where{number > 10}, @opts)
@db[:items_view].select_order_map(:number).must_equal [15, 20]
@db.create_or_replace_view(:items_view, @db[:items].where{number > 15}, @opts)
@db[:items_view].select_order_map(:number).must_equal [20]
end if DB.server_version >= 90300

it "should support refreshing materialized views concurrently" do
@opts = {:materialized=>true}
@db.create_view(:items_view, @db[:items].where{number >= 10}, @opts)
Expand Down