Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ def drop_database(database)
end
end

def create_database(database)
do_execute "CREATE DATABASE #{quote_table_name(database)}"
def create_database(database, collation=@connection_options[:collation])
if collation
do_execute "CREATE DATABASE #{quote_table_name(database)} COLLATE #{collation}"
else
do_execute "CREATE DATABASE #{quote_table_name(database)}"
end
end

def current_database
Expand Down
48 changes: 48 additions & 0 deletions test/cases/database_statements_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'cases/sqlserver_helper'

class DatabaseStatementsTestSqlserver < ActiveRecord::TestCase

self.use_transactional_fixtures = false

setup do
@connection = ActiveRecord::Base.connection
end

should 'create database' do
@connection.create_database 'activerecord_unittest3' #, 'SQL_Latin1_General_CP1_CI_AS'
database_name = @connection.select_value "SELECT name FROM master.dbo.sysdatabases WHERE name = 'activerecord_unittest3'"
assert_equal 'activerecord_unittest3', database_name
end

should 'drop database' do
@connection.drop_database 'activerecord_unittest3'
database_name = @connection.select_value "SELECT name FROM master.dbo.sysdatabases WHERE name = 'activerecord_unittest3'"
assert_equal nil, database_name
end

context 'with collation' do
teardown do
@connection.drop_database 'activerecord_unittest3'
end

should 'create database with default collation for the server' do
@connection.create_database 'activerecord_unittest3'
default_collation = @connection.select_value "SELECT SERVERPROPERTY('Collation')"
database_collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
assert_equal default_collation, database_collation
end

should 'create database with collation set by the method' do
@connection.create_database 'activerecord_unittest3', 'SQL_Latin1_General_CP1_CI_AS'
collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
assert_equal 'SQL_Latin1_General_CP1_CI_AS', collation
end

should 'create database with collation set by the config' do
@connection.instance_variable_get(:@connection_options)[:collation] = 'SQL_Latin1_General_CP1_CI_AS'
@connection.create_database 'activerecord_unittest3'
collation = @connection.select_value "SELECT DATABASEPROPERTYEX('activerecord_unittest3', 'Collation') SQLCollation"
assert_equal 'SQL_Latin1_General_CP1_CI_AS', collation
end
end
end
1 change: 1 addition & 0 deletions test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ default_connection_info: &default_connection_info
username: <%= ENV['ACTIVERECORD_UNITTEST_USER'] || 'rails' %>
password: <%= ENV['ACTIVERECORD_UNITTEST_PASS'] || '' %>
azure: <%= !ENV['ACTIVERECORD_UNITTEST_AZURE'].nil? %>
collation: <%= ENV['ACTIVERECORD_UNITTEST_COLLATION'] || nil %>

connections:

Expand Down