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 838385e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/scenic/adapters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def refresh_materialized_view(name, concurrently: false, cascade: false)
#
# @return [String]
def normalize_sql(sql_definition)
temporary_view_name = "temp_view_for_decompilation"
# max 63 bytes length (https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS)
suffix = SecureRandom.hex(16)
temporary_view_name = "temp_view_for_normalization_#{suffix}"
view_name = quote_table_name(temporary_view_name)
transaction do
execute "CREATE TEMPORARY VIEW #{view_name} AS #{sql_definition};"
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 838385e

Please sign in to comment.