diff --git a/lib/scenic/adapters/postgres.rb b/lib/scenic/adapters/postgres.rb index 28199877..7a9b1b93 100644 --- a/lib/scenic/adapters/postgres.rb +++ b/lib/scenic/adapters/postgres.rb @@ -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 diff --git a/spec/scenic/adapters/postgres_spec.rb b/spec/scenic/adapters/postgres_spec.rb index cdc3e277..06081cae 100644 --- a/spec/scenic/adapters/postgres_spec.rb +++ b/spec/scenic/adapters/postgres_spec.rb @@ -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