Skip to content

Commit 141373c

Browse files
committed
create_database has an additional parameter for collation
When creating a database you can now optionally specify the collation used instead of having to rely on the server's default collation. The latter is not always ideal when dealing with legacy systems.
1 parent e95d1f4 commit 141373c

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,12 @@ def drop_database(database)
278278
end
279279
end
280280

281-
def create_database(database)
282-
do_execute "CREATE DATABASE #{quote_table_name(database)}"
281+
def create_database(database, collation=@connection_options[:collation])
282+
if collation
283+
do_execute "CREATE DATABASE #{quote_table_name(database)} COLLATE #{collation}"
284+
else
285+
do_execute "CREATE DATABASE #{quote_table_name(database)}"
286+
end
283287
end
284288

285289
def current_database
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'cases/sqlserver_helper'
2+
3+
class DatabaseStatementsTestSqlserver < ActiveRecord::TestCase
4+
5+
self.use_transactional_fixtures = false
6+
7+
setup do
8+
@connection = ActiveRecord::Base.connection
9+
end
10+
11+
should 'create database' do
12+
@connection.create_database 'activerecord_unittest3' #, 'SQL_Latin1_General_CP1_CI_AS'
13+
database_name = @connection.select_value "SELECT name FROM master.dbo.sysdatabases WHERE name = 'activerecord_unittest3'"
14+
puts @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
15+
assert_equal 'activerecord_unittest3', database_name
16+
end
17+
18+
should 'drop database' do
19+
@connection.drop_database 'activerecord_unittest3'
20+
database_name = @connection.select_value "SELECT name FROM master.dbo.sysdatabases WHERE name = 'activerecord_unittest3'"
21+
assert_equal nil, database_name
22+
end
23+
24+
context 'with collation' do
25+
teardown do
26+
@connection.drop_database 'activerecord_unittest3'
27+
end
28+
29+
should 'create database with default collation for the server' do
30+
@connection.create_database 'activerecord_unittest3'
31+
default_collation = @connection.select_value "SELECT SERVERPROPERTY('Collation')"
32+
database_collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
33+
assert_equal default_collation, database_collation
34+
end
35+
36+
should 'create database with collation set by the method' do
37+
@connection.create_database 'activerecord_unittest3', 'SQL_Latin1_General_CP1_CI_AS'
38+
collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
39+
assert_equal 'SQL_Latin1_General_CP1_CI_AS', collation
40+
end
41+
42+
should 'create database with collation set by the config' do
43+
@connection.instance_variable_get(:@connection_options)[:collation] = 'SQL_Latin1_General_CP1_CI_AS'
44+
@connection.create_database 'activerecord_unittest3'
45+
collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
46+
assert_equal 'SQL_Latin1_General_CP1_CI_AS', collation
47+
end
48+
end
49+
end

test/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ default_connection_info: &default_connection_info
99
username: <%= ENV['ACTIVERECORD_UNITTEST_USER'] || 'rails' %>
1010
password: <%= ENV['ACTIVERECORD_UNITTEST_PASS'] || '' %>
1111
azure: <%= !ENV['ACTIVERECORD_UNITTEST_AZURE'].nil? %>
12+
collation: <%= ENV['ACTIVERECORD_UNITTEST_COLLATION'] || nil %>
1213

1314
connections:
1415

0 commit comments

Comments
 (0)