Skip to content

Commit 37af5d7

Browse files
committed
Allow DNS's to not contain a database and use what is in database.yml
1 parent 9402025 commit 37af5d7

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

CHANGELOG

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
MASTER
33

44

5+
* 2.3.6
6+
7+
* Allow DNS's to not contain a database and use what is in database.yml [Marco Mastrodonato]
8+
9+
* Rake tasks methods for vanallia rails :db namespace parity. [Ken Collins]
10+
11+
* IronRuby integrated security fixes [Jimmy Schementi]
12+
13+
514
* 2.3.5
615

716
* Initial IronRuby ADONET connection mode support baked right in. Removed most &block

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def initialize(logger,config)
192192
connect
193193
super(raw_connection, logger)
194194
initialize_sqlserver_caches
195+
use_database
195196
unless SUPPORTED_VERSIONS.include?(database_year)
196197
raise NotImplementedError, "Currently, only #{SUPPORTED_VERSIONS.to_sentence} are supported."
197198
end
@@ -401,6 +402,11 @@ def execute_procedure(proc_name, *variables)
401402
end
402403
end
403404

405+
def use_database(database=nil)
406+
database ||= @connection_options[:database]
407+
do_execute "USE #{database}" unless database.blank?
408+
end
409+
404410
def outside_transaction?
405411
info_schema_query { select_value("SELECT @@TRANCOUNT") == 0 }
406412
end
@@ -722,7 +728,7 @@ def recreate_database!(database=nil)
722728
drop_database(database)
723729
create_database(database)
724730
ensure
725-
do_execute "USE #{current_db}" if this_db
731+
use_database(current_db) if this_db
726732
end
727733

728734
# Remove existing connections and rollback any transactions if we received the message

test/cases/connection_test_sqlserver.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ def setup
7171
end
7272
end
7373

74+
should 'allow usage of :database connection option to remove setting from dsn' do
75+
assert_equal 'activerecord_unittest', @connection.current_database
76+
begin
77+
@connection.use_database('activerecord_unittest2')
78+
assert_equal 'activerecord_unittest2', @connection.current_database
79+
ensure
80+
@connection.use_database
81+
assert_equal 'activerecord_unittest', @connection.current_database, 'Would default back to connection options'
82+
end
83+
end
84+
7485
context 'Connection management' do
7586

7687
setup do

test/connections/native_sqlserver_odbc/connection.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
:mode => 'ODBC',
1111
:host => 'localhost',
1212
:username => 'rails',
13-
:dsn => 'activerecord_unittest'
13+
:dsn => 'activerecord_unittest',
14+
:database => 'activerecord_unittest'
1415
},
1516
'arunit2' => {
1617
:adapter => 'sqlserver',
1718
:mode => 'ODBC',
1819
:host => 'localhost',
1920
:username => 'rails',
20-
:dsn => 'activerecord_unittest2'
21+
:dsn => 'activerecord_unittest2',
22+
:database => 'activerecord_unittest2'
2123
}
2224
}
2325

0 commit comments

Comments
 (0)