Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on search_path (instead of "public") when deciding to fully-qualify view name #401

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/scenic/adapters/postgres/views.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def views_from_postgres
c.relname as viewname,
pg_get_viewdef(c.oid) AS definition,
c.relkind AS kind,
n.nspname AS namespace
n.nspname AS namespace,
current_schema() AS current_schema
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE
Expand All @@ -41,9 +42,9 @@ def views_from_postgres
end

def to_scenic_view(result)
namespace, viewname = result.values_at "namespace", "viewname"
namespace, viewname, current_schema = result.values_at "namespace", "viewname", "current_schema"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [106/80]


namespaced_viewname = if namespace != "public"
namespaced_viewname = if namespace != current_schema
"#{pg_identifier(namespace)}.#{pg_identifier(viewname)}"
else
pg_identifier(viewname)
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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [89/80]

Search.connection.create_view :"scenic.searches", sql_definition: view_definition
Search.connection.create_view :"public.searches", sql_definition: view_definition
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [87/80]

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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [91/80]

Search.connection.create_view :"scenic.searches", sql_definition: view_definition
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [89/80]

Search.connection.create_view :"public.searches", sql_definition: view_definition
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [89/80]

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