Skip to content

Commit 2094cab

Browse files
authored
Merge pull request rails-sqlserver#1029 from frederikspang/main
Handle views defined in other databases
2 parents 8e031b7 + d628941 commit 2094cab

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## Unreleased
22

3+
#### Fixed
4+
5+
- [#1029](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1029) Handle views defined in other databases.
6+
7+
#### Changed
8+
39
- [#1021](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1021) Freeze the SQL sent to instrumentation.
410

511
## v7.0.0.0

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ def view_information(table_name)
580580
@view_information ||= {}
581581
@view_information[table_name] ||= begin
582582
identifier = SQLServer::Utils.extract_identifiers(table_name)
583-
view_info = select_one "SELECT * FROM INFORMATION_SCHEMA.VIEWS WITH (NOLOCK) WHERE TABLE_NAME = #{quote(identifier.object)}", "SCHEMA"
583+
information_query_table = identifier.database.present? ? "[#{identifier.database}].[INFORMATION_SCHEMA].[VIEWS]" : "[INFORMATION_SCHEMA].[VIEWS]"
584+
view_info = select_one "SELECT * FROM #{information_query_table} WITH (NOLOCK) WHERE TABLE_NAME = #{quote(identifier.object)}", "SCHEMA"
584585
if view_info
585586
view_info = view_info.with_indifferent_access
586587
if view_info[:VIEW_DEFINITION].blank? || view_info[:VIEW_DEFINITION].length == 4000

test/cases/adapter_test_sqlserver.rb

+17
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,28 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
373373
assert_match(/CREATE VIEW sst_customers_view/, view_info["VIEW_DEFINITION"])
374374
end
375375

376+
it "allows connection#view_information to work with qualified object names" do
377+
view_info = connection.send(:view_information, "[activerecord_unittest].[dbo].[sst_customers_view]")
378+
assert_equal("sst_customers_view", view_info["TABLE_NAME"])
379+
assert_match(/CREATE VIEW sst_customers_view/, view_info["VIEW_DEFINITION"])
380+
end
381+
382+
it "allows connection#view_information to work across databases when using qualified object names" do
383+
# College is defined in activerecord_unittest2 database.
384+
view_info = College.connection.send(:view_information, "[activerecord_unittest].[dbo].[sst_customers_view]")
385+
assert_equal("sst_customers_view", view_info["TABLE_NAME"])
386+
assert_match(/CREATE VIEW sst_customers_view/, view_info["VIEW_DEFINITION"])
387+
end
388+
376389
it "allow the connection#view_table_name method to return true table_name for the view" do
377390
assert_equal "customers", connection.send(:view_table_name, "sst_customers_view")
378391
assert_equal "topics", connection.send(:view_table_name, "topics"), "No view here, the same table name should come back."
379392
end
380393

394+
it "allow the connection#view_table_name method to return true table_name for the view for other connections" do
395+
assert_equal "customers", College.connection.send(:view_table_name, "[activerecord_unittest].[dbo].[sst_customers_view]")
396+
assert_equal "topics", College.connection.send(:view_table_name, "topics"), "No view here, the same table name should come back."
397+
end
381398
# With same column names
382399

383400
it "have matching column objects" do

0 commit comments

Comments
 (0)