Skip to content

Commit ef4c1d3

Browse files
aidanharanAidan Haran
andauthored
Fix application name sent to TinyTDS (#941)
* Fix application name * Updated changelog * No longer support configuring the application name by overriding the 'configure_application_name' method. Co-authored-by: Aidan Haran <[email protected]>
1 parent b7e52f7 commit ef4c1d3

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Changed
88

9-
- ...
9+
- [#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.
1010

1111
#### Added
1212

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,34 @@ end
8282
```
8383

8484

85-
#### Configure Connection & App Name
85+
#### Configure Connection
8686

87-
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.
87+
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.
8888

8989
```ruby
9090
module ActiveRecord
9191
module ConnectionAdapters
9292
class SQLServerAdapter < AbstractAdapter
93-
9493
def configure_connection
9594
raw_connection_do "SET TEXTSIZE #{64.megabytes}"
9695
end
97-
98-
def configure_application_name
99-
"myapp_#{$$}_#{Thread.current.object_id}".to(29)
100-
end
101-
10296
end
10397
end
10498
end
10599
```
106100

101+
#### Configure Application Name
102+
103+
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.
104+
105+
Below shows how you might use the database.yml file to use the process ID in your application name.
106+
107+
```yaml
108+
development:
109+
adapter: sqlserver
110+
appname: <%= myapp_#{Process.pid} %>
111+
```
112+
107113
#### Executing Stored Procedures
108114
109115
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:

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,23 @@ def dblib_connect(config)
105105
end
106106

107107
def config_appname(config)
108-
config[:appname] || configure_application_name || Rails.application.class.name.split("::").first rescue nil
108+
if self.instance_methods.include?(:configure_application_name)
109+
ActiveSupport::Deprecation.warn <<~MSG.squish
110+
Configuring the application name used by TinyTDS by overriding the
111+
`ActiveRecord::ConnectionAdapters::SQLServerAdapter#configure_application_name`
112+
instance method is no longer supported. The application name should configured
113+
using the `appname` setting in the `database.yml` file instead. Consult the
114+
README for further information."
115+
MSG
116+
end
117+
118+
config[:appname] || rails_application_name
119+
end
120+
121+
def rails_application_name
122+
return nil if Rails.application.nil?
123+
124+
Rails.application.class.name.split("::").first
109125
end
110126

111127
def config_login_timeout(config)
@@ -483,8 +499,6 @@ def connection_errors
483499
end
484500
end
485501

486-
def configure_application_name; end
487-
488502
def initialize_dateformatter
489503
@database_dateformat = user_options_dateformat
490504
a, b, c = @database_dateformat.each_char.to_a

0 commit comments

Comments
 (0)