Skip to content

Commit d0e5c3b

Browse files
committed
Update table_exists? to work with qualified table names that may include an user prefix.
1 parent 0d2324a commit d0e5c3b

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

CHANGELOG

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
MASTER
33

4-
*
4+
* Update table_exists? to work with qualified table names that may include an user prefix. [Ken Collins]
55

66

77
* 2.2.10/11 * (January 22nd, 2009)
@@ -83,7 +83,7 @@ MASTER
8383
will run quickly. [Ken Collins]
8484

8585
* Add views support. ActiveRecord classes can use views. The connection now has a #views method and
86-
#table_exists? will not fall back to checking views too. [Ken Collins]
86+
#table_exists? will now fall back to checking views too. [Ken Collins]
8787

8888

8989
2.2.0 (November 21st, 2008)

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def view_table_name(table_name)
497497
end
498498

499499
def table_exists?(table_name)
500-
super || views.include?(table_name.to_s)
500+
super || tables.include?(unqualify_table_name(table_name)) || views.include?(table_name.to_s)
501501
end
502502

503503
def indexes(table_name, name = nil)

test/cases/adapter_test_sqlserver.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def setup
3939
assert @connection.supports_ddl_transactions?
4040
end
4141

42+
should 'allow owner table name prefixs like dbo. to still allow table_exists? to return true' do
43+
begin
44+
assert_equal 'tasks', Task.table_name
45+
assert Task.table_exists?
46+
Task.table_name = 'dbo.tasks'
47+
assert Task.table_exists?, 'Tasks table name of dbo.tasks should return true for exists.'
48+
ensure
49+
Task.table_name = 'tasks'
50+
end
51+
end
52+
4253
context 'for database version' do
4354

4455
setup do

0 commit comments

Comments
 (0)