Skip to content

Fix application name sent to TinyTDS #941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 2, 2021
Merged
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#### 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

Expand Down
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 17 additions & 3 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -483,8 +499,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
Expand Down