Skip to content

Instantly share code, notes, and snippets.

@letmein
Created March 2, 2012 16:41
Show Gist options
  • Save letmein/1959554 to your computer and use it in GitHub Desktop.
Save letmein/1959554 to your computer and use it in GitHub Desktop.
Make Rails 3.0.11 support PostgreSQL temporary tables
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