Skip to content

Commit fdaa64c

Browse files
committed
[Rails5] Do not output column collation in schema when same as database.
1 parent 0c403d6 commit fdaa64c

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121

2222
#### Fixed
2323

24-
* ...
24+
* Do not output column collation in schema when same as database.

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ def column_definitions(table_name)
301301
columns.NUMERIC_SCALE AS numeric_scale,
302302
columns.NUMERIC_PRECISION AS numeric_precision,
303303
columns.DATETIME_PRECISION AS datetime_precision,
304-
columns.COLLATION_NAME AS collation,
304+
CASE
305+
WHEN columns.COLLATION_NAME <> DATABASEPROPERTYEX(DB_NAME(), 'Collation') THEN columns.COLLATION_NAME
306+
ELSE NULL
307+
END AS [collation],
305308
columns.ordinal_position,
306309
CASE
307310
WHEN columns.DATA_TYPE IN ('nchar','nvarchar','char','varchar') THEN columns.CHARACTER_MAXIMUM_LENGTH

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
3636
assert_line :time_2, type: 'time', limit: nil, precision: 2, scale: nil, default: nil
3737
end
3838
# Character Strings
39-
assert_line :char_10, type: 'char', limit: 10, precision: nil, scale: nil, default: "1234567890", collation: "SQL_Latin1_General_CP1_CI_AS"
40-
assert_line :varchar_50, type: 'varchar', limit: 50, precision: nil, scale: nil, default: "test varchar_50", collation: "SQL_Latin1_General_CP1_CI_AS"
41-
assert_line :varchar_max, type: 'varchar_max', limit: 2147483647, precision: nil, scale: nil, default: "test varchar_max", collation: "SQL_Latin1_General_CP1_CI_AS"
42-
assert_line :text, type: 'text_basic', limit: 2147483647, precision: nil, scale: nil, default: "test text", collation: "SQL_Latin1_General_CP1_CI_AS"
39+
assert_line :char_10, type: 'char', limit: 10, precision: nil, scale: nil, default: "1234567890", collation: nil
40+
assert_line :varchar_50, type: 'varchar', limit: 50, precision: nil, scale: nil, default: "test varchar_50", collation: nil
41+
assert_line :varchar_max, type: 'varchar_max', limit: 2147483647, precision: nil, scale: nil, default: "test varchar_max", collation: nil
42+
assert_line :text, type: 'text_basic', limit: 2147483647, precision: nil, scale: nil, default: "test text", collation: nil
4343
# Unicode Character Strings
44-
assert_line :nchar_10, type: 'nchar', limit: 10, precision: nil, scale: nil, default: "12345678åå", collation: "SQL_Latin1_General_CP1_CI_AS"
45-
assert_line :nvarchar_50, type: 'string', limit: 50, precision: nil, scale: nil, default: "test nvarchar_50 åå", collation: "SQL_Latin1_General_CP1_CI_AS"
46-
assert_line :nvarchar_max, type: 'text', limit: 2147483647, precision: nil, scale: nil, default: "test nvarchar_max åå", collation: "SQL_Latin1_General_CP1_CI_AS"
47-
assert_line :ntext, type: 'ntext', limit: 2147483647, precision: nil, scale: nil, default: "test ntext åå", collation: "SQL_Latin1_General_CP1_CI_AS"
44+
assert_line :nchar_10, type: 'nchar', limit: 10, precision: nil, scale: nil, default: "12345678åå", collation: nil
45+
assert_line :nvarchar_50, type: 'string', limit: 50, precision: nil, scale: nil, default: "test nvarchar_50 åå", collation: nil
46+
assert_line :nvarchar_max, type: 'text', limit: 2147483647, precision: nil, scale: nil, default: "test nvarchar_max åå", collation: nil
47+
assert_line :ntext, type: 'ntext', limit: 2147483647, precision: nil, scale: nil, default: "test ntext åå", collation: nil
4848
# Binary Strings
4949
assert_line :binary_49, type: 'binary_basic', limit: 49, precision: nil, scale: nil, default: nil
5050
assert_line :varbinary_49, type: 'varbinary', limit: 49, precision: nil, scale: nil, default: nil
@@ -125,7 +125,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
125125
it 'no id with model driven primary key' do
126126
output = generate_schema_for_table 'sst_no_pk_data'
127127
output.must_match %r{create_table "sst_no_pk_data".*id:\sfalse.*do}
128-
assert_line :name, type: 'string', limit: nil, default: nil, collation: "SQL_Latin1_General_CP1_CI_AS"
128+
assert_line :name, type: 'string', limit: nil, default: nil, collation: nil
129129
end
130130

131131

@@ -154,7 +154,7 @@ def line(column_name)
154154
def assert_line(column_name, options={})
155155
line = line(column_name)
156156
assert line, "Count not find line with column name: #{column_name.inspect} in schema:\n#{schema}"
157-
[:type, :limit, :precision, :scale, :default].each do |key|
157+
[:type, :limit, :precision, :scale, :collation, :default].each do |key|
158158
next unless options.key?(key)
159159
actual = key == :type ? line.send(:type_method) : line.send(key)
160160
expected = options[key]

0 commit comments

Comments
 (0)