Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

#### Added

- Default `tds_version` to 7.4 when an `access_token` (Microsoft Entra ID) is configured.
- [#1385](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1385) Added support for Nulls first and last.
- [#1422](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1422) Added ractors support.

#### Changed

- Skip the `WITH (NOLOCK)` table hint on Azure Synapse and Microsoft Fabric warehouses, which reject table hints.
- [#1381](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1381) Fix `change_column` to preserve old column attributes.
- [#1393](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1393) Treat TinyTDS `failed dbsqlsend() function` errors as `ConnectionNotEstablished` so they're retried instead of surfaced as `StatementInvalid`.
- [#1397](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1397) Treat "DBPROCESS is dead or not enabled" as ConnectionNotEstablished.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def data_source_sql(name = nil, type: nil)
sql += " WHEN #{table_schema} = 'dbo' THEN #{table_name}"
sql += " ELSE CONCAT(#{table_schema}, '.', #{table_name})"
sql += " END"
sql += " FROM #{database}INFORMATION_SCHEMA.TABLES WITH (NOLOCK)"
sql += " FROM #{database}INFORMATION_SCHEMA.TABLES#{nolock_hint}"
sql += " WHERE TABLE_CATALOG = #{table_catalog}"
sql += " AND TABLE_SCHEMA = #{quote(scope[:schema])}" if scope[:schema]
sql += " AND TABLE_NAME = #{quote(scope[:name])}" if scope[:name]
Expand Down Expand Up @@ -949,14 +949,19 @@ def view_table_name(table_name)
view_info.present? ? get_table_name(view_info["VIEW_DEFINITION"]) : table_name
end

# Table hints are rejected by Azure Synapse and Microsoft Fabric warehouses.
def nolock_hint
sqlserver_data_warehouse? ? "" : " WITH (NOLOCK)"
end

def view_information(table_name)
@view_information ||= {}

@view_information[table_name] ||= begin
identifier = SQLServer::Utils.extract_identifiers(table_name)
information_query_table = identifier.database.present? ? "[#{identifier.database}].[INFORMATION_SCHEMA].[VIEWS]" : "[INFORMATION_SCHEMA].[VIEWS]"

view_info = select_one("SELECT * FROM #{information_query_table} WITH (NOLOCK) WHERE TABLE_NAME = #{quote(identifier.object)}", "SCHEMA").to_h
view_info = select_one("SELECT * FROM #{information_query_table}#{nolock_hint} WHERE TABLE_NAME = #{quote(identifier.object)}", "SCHEMA").to_h

if view_info.present?
if view_info["VIEW_DEFINITION"].blank? || view_info["VIEW_DEFINITION"].length == 4000
Expand Down
9 changes: 8 additions & 1 deletion lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def ractor_connection_proxy_class # :nodoc:
def initialize(...)
super

@config[:tds_version] = "7.3" unless @config[:tds_version]
# Microsoft Entra ID access tokens (federated authentication) need TDS 7.4.
@config[:tds_version] = (@config[:access_token] ? "7.4" : "7.3") unless @config[:tds_version]
@config[:appname] = self.class.rails_application_name unless @config[:appname]
@config[:login_timeout] = @config[:login_timeout].present? ? @config[:login_timeout].to_i : nil
@config[:timeout] = @config[:timeout].present? ? @config[:timeout].to_i / 1000 : nil
Expand Down Expand Up @@ -398,6 +399,12 @@ def sqlserver_azure?
!!(sqlserver_version =~ /Azure/i)
end

# Azure Synapse dedicated SQL pools and Microsoft Fabric Warehouse report
# themselves as "Microsoft Azure SQL Data Warehouse" and reject table hints.
def sqlserver_data_warehouse?
!!(sqlserver_version =~ /Azure SQL Data Warehouse/i)
end

def database_prefix_remote_server?
return false if database_prefix.blank?

Expand Down
42 changes: 42 additions & 0 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,48 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end
end

describe "with a Microsoft Entra ID access token" do
let(:configuration) do
ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary").configuration_hash.except(:tds_version)
end

it "defaults tds_version to 7.4" do
adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter.new(configuration.merge(access_token: "token"))
assert_equal "7.4", adapter.instance_variable_get(:@config)[:tds_version]
end

it "keeps the 7.3 default without one" do
adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter.new(configuration)
assert_equal "7.3", adapter.instance_variable_get(:@config)[:tds_version]
end

it "keeps an explicit tds_version" do
adapter = ActiveRecord::ConnectionAdapters::SQLServerAdapter.new(configuration.merge(access_token: "token", tds_version: "8.0"))
assert_equal "8.0", adapter.instance_variable_get(:@config)[:tds_version]
end
end

describe "on a data warehouse (Azure Synapse, Microsoft Fabric)" do
it "detects the warehouse from @@VERSION" do
connection.stub(:sqlserver_version, "Microsoft Azure SQL Data Warehouse - 10.0.20000.0") do
assert connection.sqlserver_data_warehouse?
assert connection.sqlserver_azure?
end
connection.stub(:sqlserver_version, "Microsoft SQL Azure (RTM) - 12.0.2000.8") do
assert_not connection.sqlserver_data_warehouse?
end
end

it "leaves out the NOLOCK table hint, which warehouses reject" do
connection.stub(:sqlserver_data_warehouse?, true) do
assert_no_match %r{NOLOCK}, connection.send(:data_source_sql, "accounts", type: "BASE TABLE")
end
connection.stub(:sqlserver_data_warehouse?, false) do
assert_match %r{WITH \(NOLOCK\)}, connection.send(:data_source_sql, "accounts", type: "BASE TABLE")
end
end
end

it "test database exists returns false if database does not exist" do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
configuration = db_config.configuration_hash.merge(database: "nonexistent_activerecord_unittest")
Expand Down
Loading