From 655ede6ac45c07ee85e3622aa33cd37ece92cc1c Mon Sep 17 00:00:00 2001 From: Robert Peralta Date: Thu, 8 Apr 2021 10:54:41 -0400 Subject: [PATCH 1/2] Use view without namespace when dumping schema. Without this, default behavior of `rake db:schema:load` is broken becuase Activerecord config option `schema search path` for Postgres adapters is automatically used to identify where is the schema being loaded to; By explicitly adding the namespace/search_path in the schema.rb it's wrong as you can run rake db:migrate from multiple computers and configurations which result in namespace_from_pc_1.view_name and namespace_from_pc_2.view_name on different commits. + Added a new private method unscoped_view to make sure to not include the namespace when dumping the schema name + Updated tests to make sure the namespace is excluded rather than included. --- lib/scenic/schema_dumper.rb | 2 +- lib/scenic/view.rb | 8 ++++++-- spec/scenic/schema_dumper_spec.rb | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/scenic/schema_dumper.rb b/lib/scenic/schema_dumper.rb index 68aab1b2..74ce2049 100644 --- a/lib/scenic/schema_dumper.rb +++ b/lib/scenic/schema_dumper.rb @@ -15,7 +15,7 @@ def views(stream) dumpable_views_in_database.each do |view| stream.puts(view.to_schema) - indexes(view.name, stream) + indexes(view.unscoped_name, stream) end end diff --git a/lib/scenic/view.rb b/lib/scenic/view.rb index 45df91bf..eddf273d 100644 --- a/lib/scenic/view.rb +++ b/lib/scenic/view.rb @@ -40,12 +40,16 @@ def ==(other) materialized == other.materialized end + # @api private + def unscoped_name + name.split('.').last + end + # @api private def to_schema materialized_option = materialized ? "materialized: true, " : "" - <<-DEFINITION - create_view #{name.inspect}, #{materialized_option}sql_definition: <<-\SQL + create_view #{unscoped_name.inspect}, #{materialized_option}sql_definition: <<-\SQL #{definition.indent(2)} SQL DEFINITION diff --git a/spec/scenic/schema_dumper_spec.rb b/spec/scenic/schema_dumper_spec.rb index 87e683b7..a92f69de 100644 --- a/spec/scenic/schema_dumper_spec.rb +++ b/spec/scenic/schema_dumper_spec.rb @@ -40,7 +40,7 @@ class SearchInAHaystack < ActiveRecord::Base end context "with views in non public schemas" do - it "dumps a create_view including namespace for a view in the database" do + it "dumps a create_view excluding namespace for a view in the database" do view_definition = "SELECT 'needle'::text AS haystack" Search.connection.execute "CREATE SCHEMA scenic; SET search_path TO scenic, public" Search.connection.create_view :"scenic.searches", sql_definition: view_definition @@ -49,7 +49,7 @@ class SearchInAHaystack < ActiveRecord::Base ActiveRecord::SchemaDumper.dump(Search.connection, stream) output = stream.string - expect(output).to include 'create_view "scenic.searches",' + expect(output).to include 'create_view "searches",' Search.connection.drop_view :'scenic.searches' end @@ -89,7 +89,7 @@ class SearchInAHaystack < ActiveRecord::Base end end - context "with views using unexpected characters, name including namespace" do + context "with views using unexpected characters, name excluding namespace" do it "dumps a create_view for a view in the database" do view_definition = "SELECT 'needle'::text AS haystack" Search.connection.execute( @@ -102,7 +102,7 @@ class SearchInAHaystack < ActiveRecord::Base ActiveRecord::SchemaDumper.dump(Search.connection, stream) output = stream.string - expect(output).to include 'create_view "scenic.\"search in a haystack\"",' + expect(output).to include 'create_view "\"search in a haystack\"",' expect(output).to include view_definition Search.connection.drop_view :'scenic."search in a haystack"' From d804ef847fb99e9bafe72610738b10f587f4407e Mon Sep 17 00:00:00 2001 From: Robert Peralta Date: Thu, 8 Apr 2021 10:59:21 -0400 Subject: [PATCH 2/2] Ok Dog. --- lib/scenic/view.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/scenic/view.rb b/lib/scenic/view.rb index eddf273d..c2f59c59 100644 --- a/lib/scenic/view.rb +++ b/lib/scenic/view.rb @@ -42,7 +42,7 @@ def ==(other) # @api private def unscoped_name - name.split('.').last + name.split(".").last end # @api private