Skip to content

Commit 14dbe72

Browse files
committed
diagnostics - possible fix
1 parent 1357877 commit 14dbe72

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

lib/rails_sql_views/connection_adapters/postgresql_adapter.rb

+35-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,50 @@ def self.included(base)
1010
unless method_defined?(:tables_with_views_included)
1111
base.alias_method_chain :tables, :views_included
1212
else
13-
alias_method :tables, :tables_with_views_included
13+
alias_method :tables, :tables_with_views_included # still doesn't run the right one, grump
1414
end
15-
15+
base.alias_method_chain :table_exists?, :views_included
16+
1617
puts method_defined?(:tables_without_views_included)
1718
puts base.method_defined?(:tables_without_views_included)
19+
puts method_defined?(:table_exists_without_views_included?)
20+
puts base.method_defined?(:table_exists_without_views_included?)
1821
end
22+
23+
1924
# Returns true as this adapter supports views.
2025
def supports_views?
2126
true
2227
end
2328

29+
def table_exists_with_views_included?(name)
30+
puts "\nmy postgres table_exists?", "name #{name}"
31+
name = name.to_s
32+
schema, table = name.split('.', 2)
33+
unless table # A table was provided without a schema
34+
table = schema
35+
schema = nil
36+
end
37+
if name =~ /^"/ # Handle quoted table names
38+
table = name
39+
schema = nil
40+
end
41+
puts "schema #{schema}", "table #{table}"
42+
43+
query(<<-SQL).first[0].to_i > 0
44+
SELECT COUNT(*)
45+
FROM (
46+
select schemaname, tablename as itemname
47+
from pg_tables
48+
union
49+
select schemaname, viewname
50+
from pg_views
51+
) combo
52+
WHERE itemname = '#{table.gsub(/(^"|"$)/,'')}'
53+
#{schema ? "AND schemaname = '#{schema}'" : ''}
54+
SQL
55+
end
56+
2457
def tables_with_views_included(name = nil)
2558
puts "\nmy postgres tables"
2659
q = <<-SQL

0 commit comments

Comments
 (0)