From 000ff975fcc508d66e8b68e35456c772fb1c5fc5 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Wed, 1 Sep 2021 14:10:13 +0100 Subject: [PATCH 1/3] Fix application name --- lib/active_record/connection_adapters/sqlserver_adapter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/active_record/connection_adapters/sqlserver_adapter.rb b/lib/active_record/connection_adapters/sqlserver_adapter.rb index adc5f0c0b..f4df7645f 100644 --- a/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -119,6 +119,8 @@ def config_timeout(config) def config_encoding(config) config[:encoding].present? ? config[:encoding] : nil end + + def configure_application_name; end end def initialize(connection, logger, _connection_options, config) @@ -483,8 +485,6 @@ def connection_errors end end - def configure_application_name; end - def initialize_dateformatter @database_dateformat = user_options_dateformat a, b, c = @database_dateformat.each_char.to_a From 07638375b196054a3e354ba2ff7de06ee558b30a Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Wed, 1 Sep 2021 14:28:52 +0100 Subject: [PATCH 2/3] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d249696ac..9fb9d4a78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Fixed - [#940](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/940) Primary key violation should result in RecordNotUnique error +- [#941](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/941) Fix application name sent to TinyTDS #### Changed From d93791589b7a7b4ad7823a82702ec3427a3365fc Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Thu, 2 Sep 2021 16:43:52 +0100 Subject: [PATCH 3/3] No longer support configuring the application name by overriding the 'configure_application_name' method. --- CHANGELOG.md | 3 +-- README.md | 22 ++++++++++++------- .../connection_adapters/sqlserver_adapter.rb | 20 ++++++++++++++--- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fb9d4a78..35da98a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,11 +3,10 @@ #### Fixed - [#940](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/940) Primary key violation should result in RecordNotUnique error -- [#941](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/941) Fix application name sent to TinyTDS #### Changed -- ... +- [#941](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/941) No longer support configuring the application name by overriding the 'configure_application_name' method. #### Added diff --git a/README.md b/README.md index 9c6727c88..9ac67f0b7 100644 --- a/README.md +++ b/README.md @@ -82,28 +82,34 @@ end ``` -#### Configure Connection & App Name +#### Configure Connection -We currently conform to an unpublished and non-standard AbstractAdapter interface to configure connections made to the database. To do so, just override the `configure_connection` method in an initializer like so. In this case below we are setting the `TEXTSIZE` to 64 megabytes. Also, TinyTDS supports an application name when it logs into SQL Server. This can be used to identify the connection in SQL Server's activity monitor. By default it will use the `appname` from your database.yml file or a lowercased version of your Rails::Application name. It is now possible to define a `configure_application_name` method that can give you per instance details. Below shows how you might use this to get the process id and thread id of the current connection. +We currently conform to an unpublished and non-standard AbstractAdapter interface to configure connections made to the database. To do so, just override the `configure_connection` method in an initializer like so. In this case below we are setting the `TEXTSIZE` to 64 megabytes. ```ruby module ActiveRecord module ConnectionAdapters class SQLServerAdapter < AbstractAdapter - def configure_connection raw_connection_do "SET TEXTSIZE #{64.megabytes}" end - - def configure_application_name - "myapp_#{$$}_#{Thread.current.object_id}".to(29) - end - end end end ``` +#### Configure Application Name + +TinyTDS supports an application name when it logs into SQL Server. This can be used to identify the connection in SQL Server's activity monitor. By default it will use the `appname` from your database.yml file or your Rails::Application name. + +Below shows how you might use the database.yml file to use the process ID in your application name. + +```yaml +development: + adapter: sqlserver + appname: <%= myapp_#{Process.pid} %> +``` + #### Executing Stored Procedures Every class that sub classes ActiveRecord::Base will now have an execute_procedure class method to use. This method takes the name of the stored procedure which can be a string or symbol and any number of variables to pass to the procedure. Arguments will automatically be quoted per the connection's standards as normal. For example: diff --git a/lib/active_record/connection_adapters/sqlserver_adapter.rb b/lib/active_record/connection_adapters/sqlserver_adapter.rb index f4df7645f..22b420b8c 100644 --- a/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -105,7 +105,23 @@ def dblib_connect(config) end def config_appname(config) - config[:appname] || configure_application_name || Rails.application.class.name.split("::").first rescue nil + if self.instance_methods.include?(:configure_application_name) + ActiveSupport::Deprecation.warn <<~MSG.squish + Configuring the application name used by TinyTDS by overriding the + `ActiveRecord::ConnectionAdapters::SQLServerAdapter#configure_application_name` + instance method is no longer supported. The application name should configured + using the `appname` setting in the `database.yml` file instead. Consult the + README for further information." + MSG + end + + config[:appname] || rails_application_name + end + + def rails_application_name + return nil if Rails.application.nil? + + Rails.application.class.name.split("::").first end def config_login_timeout(config) @@ -119,8 +135,6 @@ def config_timeout(config) def config_encoding(config) config[:encoding].present? ? config[:encoding] : nil end - - def configure_application_name; end end def initialize(connection, logger, _connection_options, config)