提交 ecf039fc 编写于 作者: A Aaron Patterson 提交者: wycats

mode postgresql adapters table_exists? method take the schema in to account. [#4341 state:resolved]

Signed-off-by: Nwycats <wycats@gmail.com>
上级 803df08d
......@@ -675,6 +675,28 @@ def tables(name = nil)
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 tablename = '#{table.gsub(/(^"|"$)/,'')}'
#{schema ? "AND schemaname = '#{schema}'" : ''}
SQL
end
# Returns the list of all indexes for a table.
def indexes(table_name, name = nil)
schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
......
......@@ -52,6 +52,21 @@ def teardown
@connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
end
def test_table_exists?
[Thing1, Thing2, Thing3, Thing4].each do |klass|
name = klass.table_name
assert @connection.table_exists?(name), "'#{name}' table should exist"
end
end
def test_table_exists_wrong_schema
assert(!@connection.table_exists?("foo.things"), "table should not exist")
end
def test_table_exists_quoted_table
assert(@connection.table_exists?('"things.table"'), "table should exist")
end
def test_with_schema_prefixed_table_name
assert_nothing_raised do
assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{TABLE_NAME}")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册