aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-04-11 14:48:49 -0700
committerwycats <wycats@gmail.com>2010-04-11 14:53:24 -0700
commitecf039fc05ac32b7a8cbd005dd4723d7eadc61c0 (patch)
tree209983e4bc54aae7ee1067fe403a670e40bb0bcb /activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
parent803df08d896b82ae3e86c9c1e3c2ea4b6826ef70 (diff)
downloadrails-ecf039fc05ac32b7a8cbd005dd4723d7eadc61c0.tar.gz
rails-ecf039fc05ac32b7a8cbd005dd4723d7eadc61c0.tar.bz2
rails-ecf039fc05ac32b7a8cbd005dd4723d7eadc61c0.zip
mode postgresql adapters table_exists? method take the schema in to account. [#4341 state:resolved]
Signed-off-by: wycats <wycats@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb24
1 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 7169c8f3f0..ceb1adc9e0 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -670,8 +670,30 @@ module ActiveRecord
def tables(name = nil)
query(<<-SQL, name).map { |row| row[0] }
SELECT tablename
+ FROM pg_tables
+ WHERE schemaname = ANY (current_schemas(false))
+ SQL
+ end
+
+ def table_exists?(name)
+ name = name.to_s
+ schema, table = name.split('.', 2)
+
+ unless table # A table was provided without a schema
+ table = schema
+ schema = nil
+ end
+
+ if name =~ /^"/ # Handle quoted table names
+ table = name
+ schema = nil
+ end
+
+ query(<<-SQL).first[0].to_i > 0
+ SELECT COUNT(*)
FROM pg_tables
- WHERE schemaname = ANY (current_schemas(false))
+ WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}'
+ #{schema ? "AND schemaname = '#{schema}'" : ''}
SQL
end