Skip to content

Commit

Permalink
Avoid hardcoding 'public' and rely on current schema instead for nami…
Browse files Browse the repository at this point in the history
…ng conventions
  • Loading branch information
smudge committed Jan 17, 2024
1 parent 104d888 commit 5a3a81c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/scenic/adapters/postgres/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def views_from_postgres
def to_scenic_view(result)
namespace, viewname = result.values_at "namespace", "viewname"

if namespace != "public"
if namespace != current_schema
namespaced_viewname = "#{pg_identifier(namespace)}.#{pg_identifier(viewname)}"
else
namespaced_viewname = pg_identifier(viewname)
Expand All @@ -56,6 +56,10 @@ def to_scenic_view(result)
)
end

def current_schema
@current_schema ||= connection.execute("SELECT CURRENT_SCHEMA()").first["current_schema"]
end

def pg_identifier(name)
return name if name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/

Expand Down
2 changes: 1 addition & 1 deletion spec/scenic/adapters/postgres_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ module Adapters
ActiveRecord::Base.connection.execute <<-SQL
CREATE SCHEMA scenic;
CREATE VIEW scenic.parents AS SELECT text 'Maarten' AS name;
SET search_path TO scenic, public;
SET search_path TO public, scenic;
SQL

expect(adapter.views.map(&:name)).to eq [
Expand Down
26 changes: 24 additions & 2 deletions spec/scenic/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,38 @@ class SearchInAHaystack < ActiveRecord::Base
context "with views in non public schemas" do
it "dumps a create_view including 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.execute "CREATE SCHEMA scenic; SET search_path TO public, scenic"
Search.connection.create_view :"scenic.searches", sql_definition: view_definition
Search.connection.create_view :"public.searches", sql_definition: view_definition
stream = StringIO.new

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'
Search.connection.drop_view :'public.searches'
end

context 'when "public" is not the primary search path' do
it "dumps a create_view including 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
Search.connection.create_view :"public.searches", sql_definition: view_definition
stream = StringIO.new

ActiveRecord::SchemaDumper.dump(Search.connection, stream)

output = stream.string
expect(output).to include 'create_view "searches",'
expect(output).to include 'create_view "public.searches",'

Search.connection.drop_view :'scenic.searches'
Search.connection.drop_view :'public.searches'
end
end
end

Expand Down Expand Up @@ -123,7 +145,7 @@ class SearchInAHaystack < ActiveRecord::Base
it "dumps a create_view 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",
"CREATE SCHEMA scenic; SET search_path TO public, scenic",
)
Search.connection.create_view 'scenic."search in a haystack"',
sql_definition: view_definition
Expand Down

0 comments on commit 5a3a81c

Please sign in to comment.