Created
March 2, 2012 16:41
-
-
Save letmein/1959554 to your computer and use it in GitHub Desktop.
Make Rails 3.0.11 support PostgreSQL temporary tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter | |
def table_exists?(name) | |
name = name.to_s | |
schema, table = name.split('.', 2) | |
unless table | |
table = schema | |
schema = nil | |
end | |
if name =~ /^"/ # Handle quoted table names | |
table = name | |
schema = nil | |
end | |
schema_clause = schema ? "n.nspname = '#{schema}'" | |
: "( n.nspname = ANY (current_schemas(false)) OR n.oid = pg_my_temp_schema() )" | |
query(<<-SQL).first[0].to_i > 0 | |
SELECT COUNT(*) | |
FROM pg_class c | |
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace | |
WHERE c.relkind in ('v','r') | |
AND c.relname = '#{table.gsub(/(^"|"$)/,'')}' | |
AND #{schema_clause} | |
SQL | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment