Skip to content

Commit

Permalink
Satisfy Hound (Rubocop)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykhaylo Sorochan authored and calebhearth committed Oct 25, 2023
1 parent ff9da19 commit 1431c49
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 80 deletions.
17 changes: 11 additions & 6 deletions lib/scenic/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ def views(stream)
private

def dumpable_views_in_database
return @ordered_dumpable_views_in_database if @ordered_dumpable_views_in_database
if @ordered_dumpable_views_in_database
return @ordered_dumpable_views_in_database
end

existing_views = Scenic.database.views.reject do |view|
ignored?(view.name)
end

@ordered_dumpable_views_in_database = tsorted_views(existing_views.map(&:name)).map do |view_name|
existing_views.find { |ev| ev.name == view_name }
end.compact
@ordered_dumpable_views_in_database =
tsorted_views(existing_views.map(&:name)).map do |view_name|
existing_views.find { |ev| ev.name == view_name }
end.compact
end

# When dumping the views, their order must be topologically sorted to take into account dependencies
# When dumping the views, their order must be topologically
# sorted to take into account dependencies
def tsorted_views(views_names)
views_hash = TSortableHash.new

Expand All @@ -75,7 +79,8 @@ def tsorted_views(views_names)
views_names.delete(dependent)
end

# after dependencies, there might be some views left that don't have any dependencies
# after dependencies, there might be some views left
# that don't have any dependencies
views_names.sort.each { |v| views_hash[v] = [] }

views_hash.tsort
Expand Down
170 changes: 96 additions & 74 deletions spec/scenic/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ class SearchInAHaystack < ActiveRecord::Base
end

describe Scenic::SchemaDumper, :db do
let(:connection) { Search.connection }
let(:conn) { Search.connection }
let(:output) do
stream = StringIO.new
ActiveRecord::SchemaDumper.dump(connection, stream)
ActiveRecord::SchemaDumper.dump(conn, stream)
stream.string
end

it "dumps a create_view for a view in the database" do
view_definition = "SELECT 'needle'::text AS haystack"
connection.create_view :searches, sql_definition: view_definition
conn.create_view :searches, sql_definition: view_definition

expect(output).to include 'create_view "searches", sql_definition: <<-SQL'
expect(output).to include view_definition

connection.drop_view :searches
conn.drop_view :searches

silence_stream(STDOUT) { eval(output) }

Expand All @@ -46,7 +46,8 @@ class SearchInAHaystack < ActiveRecord::Base

it "dumps a create_view for a materialized view in the database" do
view_definition = "SELECT 'needle'::text AS haystack"
connection.create_view :searches, materialized: true, sql_definition: view_definition
conn.create_view :searches,
materialized: true, sql_definition: view_definition

expect(output).to include 'create_view "searches", materialized: true, sql_definition: <<-SQL'
expect(output).to include view_definition
Expand All @@ -55,12 +56,12 @@ 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"
connection.execute "CREATE SCHEMA scenic; SET search_path TO scenic, public"
connection.create_view :"scenic.searches", sql_definition: view_definition
conn.execute "CREATE SCHEMA scenic; SET search_path TO scenic, public"
conn.create_view :"scenic.searches", sql_definition: view_definition

expect(output).to include 'create_view "scenic.searches",'

connection.drop_view :'scenic.searches'
conn.drop_view :'scenic.searches'
end
end

Expand All @@ -80,7 +81,7 @@ class SearchInAHaystack < ActiveRecord::Base

it "ignores tables internal to Rails" do
view_definition = "SELECT 'needle'::text AS haystack"
connection.create_view :searches, sql_definition: view_definition
conn.create_view :searches, sql_definition: view_definition

expect(output).to include 'create_view "searches"'
expect(output).not_to include "pg_stat_statements_info"
Expand All @@ -90,12 +91,13 @@ class SearchInAHaystack < ActiveRecord::Base
context "with views using unexpected characters in name" do
it "dumps a create_view for a view in the database" do
view_definition = "SELECT 'needle'::text AS haystack"
connection.create_view '"search in a haystack"', sql_definition: view_definition
conn.create_view '"search in a haystack"',
sql_definition: view_definition

expect(output).to include 'create_view "\"search in a haystack\"",'
expect(output).to include view_definition

connection.drop_view :'"search in a haystack"'
conn.drop_view :'"search in a haystack"'

silence_stream(STDOUT) { eval(output) }

Expand All @@ -106,16 +108,16 @@ class SearchInAHaystack < ActiveRecord::Base
context "with views using unexpected characters, name including namespace" do
it "dumps a create_view for a view in the database" do
view_definition = "SELECT 'needle'::text AS haystack"
connection.execute(
conn.execute(
"CREATE SCHEMA scenic; SET search_path TO scenic, public",
)
connection.create_view 'scenic."search in a haystack"',
conn.create_view 'scenic."search in a haystack"',
sql_definition: view_definition

expect(output).to include 'create_view "scenic.\"search in a haystack\"",'
expect(output).to include view_definition

connection.drop_view :'scenic."search in a haystack"'
conn.drop_view :'scenic."search in a haystack"'

silence_stream(STDOUT) { eval(output) }

Expand All @@ -132,12 +134,12 @@ class SearchInAHaystack < ActiveRecord::Base

context "without dependencies" do
it "sorts views without dependencies" do
connection.create_view "cucumber_needles",
sql_definition: "SELECT 'kukumbas'::text as needle"
connection.create_view "vip_needles",
sql_definition: "SELECT 'vip'::text as needle"
connection.create_view "none_needles",
sql_definition: "SELECT 'none_needles'::text as needle"
conn.create_view "cucumber_needles",
sql_definition: "SELECT 'kukumbas'::text as needle"
conn.create_view "vip_needles",
sql_definition: "SELECT 'vip'::text as needle"
conn.create_view "none_needles",
sql_definition: "SELECT 'none_needles'::text as needle"

# Same here, no dependencies among existing views, all views are sorted
sorted_views = %w[cucumber_needles vip_needles none_needles].sort
Expand All @@ -148,47 +150,53 @@ class SearchInAHaystack < ActiveRecord::Base

context "with dependencies" do
it "sorts according to dependencies" do
connection.create_table(:tasks) { |t| t.integer :performer_id }
connection.create_table(:notes) { |t| t.text :title; t.integer :author_id }
connection.create_table(:users) { |t| t.text :nickname }
connection.create_table(:roles) { |t| t.text :name; t.integer :user_id }

connection.create_view "recent_tasks",
sql_definition: <<-SQL
SELECT id, performer_id from tasks where id > 42
SQL
connection.create_view "old_roles",
sql_definition: <<-SQL
SELECT id, name from roles where id > 56
SQL
connection.create_view "nirvana_notes",
sql_definition: <<-SQL
SELECT notes.id, notes.title, notes.author_id, old_roles.name FROM notes
JOIN old_roles ON notes.author_id = old_roles.id
SQL
connection.create_view "angry_zombies",
sql_definition: <<-SQL
SELECT users.nickname, recent_tasks.id as task_id, nirvana_notes.title as talk FROM users
JOIN roles ON roles.user_id = users.id
JOIN nirvana_notes ON nirvana_notes.author_id = roles.id
JOIN recent_tasks ON recent_tasks.performer_id = roles.id
SQL
connection.create_view "doctor_zombies",
sql_definition: <<-SQL
SELECT id FROM old_roles WHERE name LIKE '%Dr%'
SQL
connection.create_view "xenomorphs",
sql_definition: <<-SQL
SELECT id, name FROM roles WHERE name = 'xeno'
SQL
connection.create_view "important_messages",
sql_definition: <<-SQL
SELECT id, title FROM notes WHERE title LIKE '%NSFW%'
SQL
conn.create_table(:tasks) { |t| t.integer :performer_id }
conn.create_table(:notes) do |t|
t.text :title
t.integer :author_id
end
conn.create_table(:users) { |t| t.text :nickname }
conn.create_table(:roles) do |t|
t.text :name
t.integer :user_id
end

conn.create_view "recent_tasks",
sql_definition: <<-SQL
SELECT id, performer_id from tasks where id > 42
SQL
conn.create_view "old_roles",
sql_definition: <<-SQL
SELECT id, name from roles where id > 56
SQL
conn.create_view "nirvana_notes",
sql_definition: <<-SQL
SELECT notes.id, notes.title, notes.author_id, old_roles.name FROM notes
JOIN old_roles ON notes.author_id = old_roles.id
SQL
conn.create_view "angry_zombies",
sql_definition: <<-SQL
SELECT users.nickname, recent_tasks.id as task_id, nirvana_notes.title as talk FROM users
JOIN roles ON roles.user_id = users.id
JOIN nirvana_notes ON nirvana_notes.author_id = roles.id
JOIN recent_tasks ON recent_tasks.performer_id = roles.id
SQL
conn.create_view "doctor_zombies",
sql_definition: <<-SQL
SELECT id FROM old_roles WHERE name LIKE '%Dr%'
SQL
conn.create_view "xenomorphs",
sql_definition: <<-SQL
SELECT id, name FROM roles WHERE name = 'xeno'
SQL
conn.create_view "important_messages",
sql_definition: <<-SQL
SELECT id, title FROM notes WHERE title LIKE '%NSFW%'
SQL

# Converted with https://github.com/ggerganov/dot-to-ascii
# digraph {
# rankdir = "RL";
# rankdir = "BT";
# xenomorphs;
# important_messages;
# doctor_zombies -> old_roles;
Expand All @@ -197,22 +205,36 @@ class SearchInAHaystack < ActiveRecord::Base
# angry_zombies -> recent_tasks;
# }
#
# +--------------------+
# | recent_tasks |
# +--------------------+
# ^
# |
# |
# +----------------+ +-----------+ +---------------+ +--------------------+
# | doctor_zombies | --> | old_roles | <-- | nirvana_notes | <-- | angry_zombies |
# +----------------+ +-----------+ +---------------+ +--------------------+
# +--------------------+
# | important_messages |
# +--------------------+
# +--------------------+
# | xenomorphs |
# +--------------------+
expect(views).to eq(%w[old_roles nirvana_notes recent_tasks angry_zombies doctor_zombies important_messages xenomorphs])
# +--------------------+
# | doctor_zombies |
# +--------------------+
# |
# |
# v
# +--------------------+
# | old_roles |
# +--------------------+
# ^
# |
# |
# +--------------------+
# | nirvana_notes |
# +--------------------+
# ^
# |
# |
# +--------------------+ +--------------+
# | angry_zombies | --> | recent_tasks |
# +--------------------+ +--------------+
# +--------------------+
# | important_messages |
# +--------------------+
# +--------------------+
# | xenomorphs |
# +--------------------+
expect(views).to eq(%w[old_roles nirvana_notes recent_tasks
angry_zombies doctor_zombies
important_messages xenomorphs])
end
end
end
Expand Down

0 comments on commit 1431c49

Please sign in to comment.