Browse files

Added a #enable_default_unicode_types class attribute access to make …

…all new added or changed string types like :string/:text default to unicode/national data types. See the README for full details. Added a rake task that assists setting this to true when running tests.
  • Loading branch information...
1 parent c2e8c63 commit e67df499a66a2accf510e392a98f89075261854e @metaskills metaskills committed Jan 8, 2009
View
4 CHANGELOG
@@ -1,7 +1,9 @@
MASTER
-*
+* Added a #enable_default_unicode_types class attribute access to make all new added or changed string types
+ like :string/:text default to unicode/national data types. See the README for full details. Added a rake
+ task that assists setting this to true when running tests. [Ken Collins]
* 2.2.6 (January 8th, 2008)
View
27 README.rdoc
@@ -12,7 +12,7 @@ The SQL Server adapter for rails is back for ActiveRecord 2.2 and up! We are cur
* Implementation for #disable_referential_integrity used by ActiveRecord's Fixtures class.
* Pessimistic locking suppot. See the #add_lock! method for details.
* Enabled #case_sensitive_equality_operator used by unique validations.
-* Unicode character support for nchar, nvarchar and ntext data types.
+* Unicode character support for nchar, nvarchar and ntext data types. Configuration option for defaulting all string data types to the unicode safe types.
* View support for table names, identity inserts, and column defaults.
==== Date/Time Data Type Hinting
@@ -51,23 +51,44 @@ For example:
Manually creating a varchar(max) on SQL Server 2005 is not necessary since this is the default type created when specifying a :text field. As time goes on we will be testing other SQL Server specific data types are handled correctly when created in a migration.
-==== Native Text/Binary Data Type Accessor
+==== Native Text/String/Binary Data Type Accessor
To pass the ActiveRecord tests we had to implement an class accessor for the native type created for :text columns. By default any :text column created by migrations will create these native types.
* SQL Server 2000 is 'text'
* SQL Server 2005 is 'varchar(max)'
-During testing this type is set to 'varchar(8000)' for both versions. The reason is that rails expects to be able to use SQL = operators on text data types and this is not possible with a native 'text' data type in SQL Server. The default 'varchar(max)' for SQL Server 2005 can be queried using the SQL = operator and has plenty of storage space which is why we made it the default for 2005. If for some reason you want to change the data type created during migrations for any SQL Server version, you can configure this line to your liking in a config/initializers file.
+During testing this type is set to 'varchar(8000)' for SQL Server 2000. The reason is that rails expects to be able to use SQL = operators on text data types and this is not possible with a native 'text' data type in SQL Server. The default 'varchar(max)' for SQL Server 2005 can be queried using the SQL = operator and has plenty of storage space which is why we made it the default for 2005. If for some reason you want to change the data type created during migrations for any SQL Server version, you can configure this line to your liking in a config/initializers file.
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
+
+Also, there is a class attribute setter for the native string database type. This is the same for both SQL Server 2000 and 2005, 'varchar'. However in can be used instead of the #enable_default_unicode_types below for finer grain control over which types you want unicode safe when adding or changing the schema.
+
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
By default any :binary column created by migrations will create these native types
* SQL Server 2000 is 'image'
* SQL Server 2005 is 'varbinary(max)'
+==== Setting Unicode Types As Default
+
+By default the adapter will use non-unicode safe data types for :string and :text types when DEFINING or 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.
+
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
+
+ # SQL Server 2000
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'ntext'
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
+
+ # SQL Server 2005
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(max)'
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
+
+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).
+
+
==== Schema Information Logging
By default all queries to the INFORMATION_SCHEMA table is silenced. If you think logging these queries are useful, you can enable it by adding this like to a config/initializers file.
View
8 Rakefile
@@ -27,6 +27,7 @@ task :recreate_databases => [:drop_databases, :create_databases]
for adapter in %w( sqlserver sqlserver_odbc )
+
Rake::TestTask.new("test_#{adapter}") { |t|
t.libs << "test"
t.libs << "test/connections/native_#{adapter}"
@@ -40,5 +41,12 @@ for adapter in %w( sqlserver sqlserver_odbc )
namespace adapter do
task :test => "test_#{adapter}"
end
+
end
+desc 'Test with unicode types enabled.'
+Rake::TestTask.new(:test_unicode_types) do |t|
+ ENV['ENABLE_DEFAULT_UNICODE_TYPES'] = 'true'
+ test = Rake::Task['test_sqlserver_odbc']
+ test.invoke
+end
View
22 lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -155,7 +155,8 @@ class SQLServerAdapter < AbstractAdapter
SUPPORTED_VERSIONS = [2000,2005].freeze
LIMITABLE_TYPES = ['string','integer','float','char','nchar','varchar','nvarchar'].freeze
- cattr_accessor :native_text_database_type, :native_binary_database_type, :log_info_schema_queries
+ cattr_accessor :native_text_database_type, :native_binary_database_type, :native_string_database_type,
+ :log_info_schema_queries, :enable_default_unicode_types
class << self
@@ -216,12 +217,21 @@ def inspect
"#<#{self.class} version: #{version}, year: #{database_year}, connection_options: #{@connection_options.inspect}>"
end
+ def native_string_database_type
+ @@native_string_database_type || (enable_default_unicode_types ? 'nvarchar' : 'varchar')
+ end
+
def native_text_database_type
- self.class.native_text_database_type || (sqlserver_2005? ? 'varchar(max)' : 'text')
+ @@native_text_database_type ||
+ if sqlserver_2005?
+ enable_default_unicode_types ? 'nvarchar(max)' : 'varchar(max)'
+ else
+ enable_default_unicode_types ? 'ntext' : 'text'
+ end
end
def native_binary_database_type
- self.class.native_binary_database_type || (sqlserver_2005? ? 'varbinary(max)' : 'image')
+ @@native_binary_database_type || (sqlserver_2005? ? 'varbinary(max)' : 'image')
end
# QUOTING ==================================================#
@@ -426,16 +436,16 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
def native_database_types
{
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
- :string => { :name => "varchar", :limit => 255 },
- :text => { :name => native_text_database_type },
+ :string => { :name => native_string_database_type, :limit => 255 },
+ :text => { :name => native_text_database_type },
:integer => { :name => "int", :limit => 4 },
:float => { :name => "float", :limit => 8 },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "datetime" },
:date => { :name => "datetime" },
- :binary => { :name => native_binary_database_type },
+ :binary => { :name => native_binary_database_type },
:boolean => { :name => "bit"},
# These are custom types that may move somewhere else for good schema_dumper.rb hacking to output them.
:char => { :name => 'char' },
View
43 test/cases/adapter_test_sqlserver.rb
@@ -194,6 +194,35 @@ def setup
end
+ context 'testing #enable_default_unicode_types configuration' do
+
+ should 'use non-unicode types when set to false' do
+ with_enable_default_unicode_types(false) do
+ if sqlserver_2000?
+ assert_equal 'varchar', @connection.native_string_database_type
+ assert_equal 'text', @connection.native_text_database_type
+ elsif sqlserver_2005?
+ assert_equal 'varchar', @connection.native_string_database_type
+ assert_equal 'varchar(max)', @connection.native_text_database_type
+ end
+ end
+ end
+
+ should 'use unicode types when set to true' do
+ with_enable_default_unicode_types(true) do
+ if sqlserver_2000?
+ assert_equal 'nvarchar', @connection.native_string_database_type
+ assert_equal 'ntext', @connection.native_text_database_type
+ elsif sqlserver_2005?
+ assert_equal 'nvarchar', @connection.native_string_database_type
+ assert_equal 'nvarchar(max)', @connection.native_text_database_type
+ end
+ end
+ end
+
+ end
+
+
end
context 'For chronic data types' do
@@ -530,6 +559,20 @@ def order_to_min_set(order)
@connection.send :order_to_min_set, order
end
+ def with_enable_default_unicode_types(setting)
+ old_setting = ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types
+ old_text = ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type
+ old_string = ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = setting
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = nil
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = nil
+ yield
+ ensure
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = old_setting
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = old_text
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = old_string
+ end
+
end
View
38 test/cases/migration_test_sqlserver.rb
@@ -40,7 +40,6 @@ class MigrationTest < ActiveRecord::TestCase
include SqlserverCoercedTest
-
def test_coerced_test_add_column_not_null_without_default
Person.connection.create_table :testings do |t|
t.column :foo, :string
@@ -53,5 +52,42 @@ def test_coerced_test_add_column_not_null_without_default
Person.connection.drop_table :testings rescue nil
end
+end
+
+class ChangeTableMigrationsTest < ActiveRecord::TestCase
+
+ COERCED_TESTS = [:test_string_creates_string_column]
+
+ include SqlserverCoercedTest
+
+ def setup
+ @connection = Person.connection
+ @connection.create_table :delete_me, :force => true do |t|
+ end
+ end
+
+ def teardown
+ @connection.drop_table :delete_me rescue nil
+ end
+
+ def test_coerced_string_creates_string_column
+ with_sqlserver_change_table do |t|
+ @connection.expects(:add_column).with(:delete_me, :foo, sqlserver_string_column, {})
+ @connection.expects(:add_column).with(:delete_me, :bar, sqlserver_string_column, {})
+ t.string :foo, :bar
+ end
+ end
+
+ protected
+
+ def with_sqlserver_change_table
+ @connection.change_table :delete_me do |t|
+ yield t
+ end
+ end
+
+ def sqlserver_string_column
+ "#{@connection.native_string_database_type}(255)"
+ end
end
View
18 test/cases/sqlserver_helper.rb
@@ -47,16 +47,30 @@ def coerced_tests
self.const_get(:COERCED_TESTS) rescue nil
end
def method_added(method)
- undef_method(method) if coerced_tests && coerced_tests.include?(method)
+ if coerced_tests && coerced_tests.include?(method)
+ undef_method(method)
+ STDOUT.puts("Undefined coerced test: #{self.name}##{method}")
+ end
end
end
end
+# Set weather to test unicode string defaults or not. Used from rake task.
+
+if ENV['ENABLE_DEFAULT_UNICODE_TYPES'] == 'true'
+ puts "With enabled unicode string types"
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
+end
+
# Change the text database type to support ActiveRecord's tests for = on text columns which
# is not supported in SQL Server text columns, so use varchar(8000) instead.
if ActiveRecord::Base.connection.sqlserver_2000?
- ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
+ if ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'nvarchar(4000)'
+ else
+ ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'varchar(8000)'
+ end
end
# Our changes/additions to ActiveRecord test helpers specific for SQL Server.

0 comments on commit e67df49

Please sign in to comment.