Skip to content

Commit

Permalink
allow to normalize multiple views on the same transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
gagalago committed Mar 9, 2021
1 parent 4b979e6 commit 9b07bcd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/scenic/adapters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ def refresh_materialized_view(name, concurrently: false, cascade: false)
#
# @return [String]
def normalize_sql(sql_definition)
temporary_view_name = "temp_view_for_decompilation"
temporary_view_name = "temp_view_for_normalization"
view_name = quote_table_name(temporary_view_name)
transaction do
execute "CREATE TEMPORARY VIEW #{view_name} AS #{sql_definition};"
normalize_view_sql(temporary_view_name)
view = normalize_view_sql(temporary_view_name)
execute "DROP VIEW IF EXISTS #{view_name};"
view
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/scenic/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ module Adapters
expect(adapter.normalize_sql("SELECT text 'Elliot' AS name"))
.to eq("SELECT 'Elliot'::text AS name;")
end
it "allow to normalize multiple queries on the same transaction" do
adapter = Postgres.new
ActiveRecord::Base.connection.transaction do
expect do
adapter.normalize_sql("SELECT text 'Elliot' AS name")
adapter.normalize_sql("SELECT text 'John' AS first_name")
end.to_not raise_error
end
end
end
describe "#normalize_sql" do
it "returns scenic view objects for plain old views" do
Expand Down

0 comments on commit 9b07bcd

Please sign in to comment.