Skip to content

Commit

Permalink
Make it possible to use create_or_replace_view with materialized view.
Browse files Browse the repository at this point in the history
It's not possible to use CREATE OR REPLACE with materialized view in Postgres so we should fallback
to `drop_view`/`create_view` calls when we use `create_or_replace_view` method with `:materialized`
option.
  • Loading branch information
nashby committed Mar 27, 2024
1 parent 235a62a commit 1ffa233
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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]
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

0 comments on commit 1ffa233

Please sign in to comment.