Skip to content

Commit 59c71a7

Browse files
committed
Default unicode datatypes!
1 parent e38cc37 commit 59c71a7

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

README.rdoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The SQL Server adapter for ActiveRecord.
77
== What's New
88

99
* Rails 3.1 prepared statement support leverages cached query plans.
10+
* Default unicode datatypes! Disable with #enable_default_unicode_types to false.
1011
* New #lowercase_schema_reflection configuration option for legacy DBs.
1112
* New dblib connection mode using TinyTds!
1213

@@ -79,12 +80,17 @@ By default any :binary column created by migrations will create a 'varbinary(max
7980

8081
==== Setting Unicode Types As Default
8182

82-
By default the adapter will use non-unicode safe data types for :string and :text types when defining/changing the schema. If you choose, you can set the following class attribute in a config/initializers file that will change this behavior. When set to true it has the equivalent meaning as the two lower items. These examples show detail level alternatives to achieve similar effects.
83+
By default the adapter will use unicode safe data types for :string and :text types when defining/changing the schema!!! This was changed in version 3.1 since it is about time we push better unicode support and since we default to TinyTDS (DBLIB) which supports unicode queries and data. If you choose, you can set the following class attribute in a config/initializers file that will disable this behavior.
8384

85+
# Default
8486
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
85-
8687
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(max)'
8788
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
89+
90+
# Disabled
91+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = false
92+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(max)'
93+
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'varchar'
8894

8995
It is important to remember that unicode types in SQL Server have approximately half the storage capacity as their counter parts. So where a normal string would max out at (8000) a unicode string will top off at (4000).
9096

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ class SQLServerAdapter < AbstractAdapter
171171
:log_info_schema_queries, :enable_default_unicode_types, :auto_connect,
172172
:cs_equality_operator, :lowercase_schema_reflection
173173

174+
self.enable_default_unicode_types = true
175+
176+
174177
def initialize(logger,config)
175178
@connection_options = config
176179
connect

test/cases/migration_test_sqlserver.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ def test_coerced_test_migrator_db_has_no_schema_migrations_table ; assert true ;
7171
end
7272
end
7373

74+
class ChangeTableMigrationsTest < ActiveRecord::TestCase
75+
COERCED_TESTS = [:test_string_creates_string_column]
76+
include SqlserverCoercedTest
77+
def test_coerced_string_creates_string_column
78+
with_change_table do |t|
79+
@connection.expects(:add_column).with(:delete_me, :foo, 'nvarchar(255)', {})
80+
@connection.expects(:add_column).with(:delete_me, :bar, 'nvarchar(255)', {})
81+
t.string :foo, :bar
82+
end
83+
end
84+
end

test/cases/sqlserver_helper.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ def method_added(method)
7272
end
7373
end
7474

75-
# Set weather to test unicode string defaults or not. Used from rake task.
76-
77-
if ENV['ENABLE_DEFAULT_UNICODE_TYPES'] != 'false'
78-
puts "With enabled unicode string types"
79-
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
80-
end
8175

8276
# Our changes/additions to ActiveRecord test helpers specific for SQL Server.
8377

0 commit comments

Comments
 (0)