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

Use view without namespace when dumping schema. #327

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
2 changes: 1 addition & 1 deletion lib/scenic/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions lib/scenic/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/scenic/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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"'
Expand Down