Skip to content

Commit 001e723

Browse files
committed
Make sure all datatypes can dump to and from.
1 parent 7c2fabb commit 001e723

File tree

10 files changed

+103
-46
lines changed

10 files changed

+103
-46
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def initialize_native_database_types
168168
decimal: { name: 'decimal' },
169169
money: { name: 'money' },
170170
smallmoney: { name: 'smallmoney' },
171-
float: { name: 'float', limit: 24 },
171+
float: { name: 'float' },
172172
real: { name: 'real' },
173173
date: { name: 'date' },
174174
datetime: { name: 'datetime' },
@@ -183,7 +183,7 @@ def initialize_native_database_types
183183
text: { name: 'nvarchar(max)' },
184184
ntext: { name: 'ntext' },
185185
binary_basic: { name: 'binary' },
186-
varbinary: { name: 'varbinary' },
186+
varbinary: { name: 'varbinary', limit: 8000 },
187187
binary: { name: 'varbinary(max)' },
188188
uuid: { name: 'uniqueidentifier' },
189189
ss_timestamp: { name: 'timestamp' }
@@ -254,7 +254,7 @@ def column_definitions(table_name)
254254
when /^numeric|decimal$/i
255255
"#{ci[:type]}(#{ci[:numeric_precision]},#{ci[:numeric_scale]})"
256256
when /^float|real$/i
257-
"#{ci[:type]}(#{ci[:numeric_precision]})"
257+
"#{ci[:type]}"
258258
when /^char|nchar|varchar|nvarchar|binary|varbinary|bigint|int|smallint$/
259259
ci[:length].to_i == -1 ? "#{ci[:type]}(max)" : "#{ci[:type]}(#{ci[:length]})"
260260
else

lib/active_record/connection_adapters/sqlserver/table_definition.rb

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,61 @@ module ConnectionAdapters
33
module SQLServer
44
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
55

6-
def uuid(name, options = {})
7-
column(name, 'uniqueidentifier', options)
8-
end
9-
106
def primary_key(name, type = :primary_key, options = {})
117
return super unless type == :uuid
128
options[:default] = options.fetch(:default, 'NEWID()')
139
options[:primary_key] = true
1410
column name, type, options
1511
end
1612

13+
def real(name, options = {})
14+
column(name, :real, options)
15+
end
16+
17+
def money(name, options = {})
18+
column(name, :money, options)
19+
end
20+
21+
def smallmoney(name, options = {})
22+
column(name, :smallmoney, options)
23+
end
24+
25+
def char(name, options = {})
26+
column(name, :char, options)
27+
end
28+
29+
def varchar(name, options = {})
30+
column(name, :varchar, options)
31+
end
32+
33+
def varchar_max(name, options = {})
34+
column(name, :varchar_max, options)
35+
end
36+
37+
def text_basic(name, options = {})
38+
column(name, :text_basic, options)
39+
end
40+
41+
def nchar(name, options = {})
42+
column(name, :nchar, options)
43+
end
44+
45+
def ntext(name, options = {})
46+
column(name, :ntext, options)
47+
end
48+
49+
def binary_basic(name, options = {})
50+
column(name, :binary_basic, options)
51+
end
52+
53+
def varbinary(name, options = {})
54+
column(name, :varbinary, options)
55+
end
56+
57+
def uuid(name, options = {})
58+
column(name, 'uniqueidentifier', options)
59+
end
60+
1761
end
1862
end
1963
end

lib/active_record/connection_adapters/sqlserver/type/float.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ class Float < ActiveRecord::Type::Float
66

77
include Castable
88

9-
def initialize(options = {})
10-
super
11-
@limit ||= 24
9+
def type
10+
:float
1211
end
1312

1413
end

lib/active_record/connection_adapters/sqlserver/type/varbinary.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ module SQLServer
44
module Type
55
class Varbinary < Binary
66

7+
def initialize(options = {})
8+
super
9+
@limit = 8000 if @limit.to_i == 0
10+
end
11+
712
def type
813
:varbinary
914
end

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ def initialize_type_map(m)
207207
m.register_type 'money', SQLServer::Type::Money.new
208208
m.register_type 'smallmoney', SQLServer::Type::SmallMoney.new
209209
# Approximate Numerics
210-
register_class_with_limit m, %r{\Afloat}, SQLServer::Type::Float
211-
register_class_with_limit m, %r{\Areal}, SQLServer::Type::Real
210+
m.register_type 'float', SQLServer::Type::Float.new
211+
m.register_type 'real', SQLServer::Type::Real.new
212212
# Date and Time
213213
m.register_type 'date', SQLServer::Type::Date.new
214214
m.register_type 'datetime', SQLServer::Type::DateTime.new

test/cases/adapter_test_sqlserver.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
333333
end
334334

335335
it 'create floats when no limit supplied' do
336-
assert_equal 'float(24)', connection.type_to_sql(:float)
337-
end
338-
339-
it 'create floats when limit is supplied' do
340-
assert_equal 'float(27)', connection.type_to_sql(:float, 27)
336+
assert_equal 'float', connection.type_to_sql(:float)
341337
end
342338

343339
end

test/cases/column_test_sqlserver.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ def assert_obj_set_and_save(attribute, value)
241241
# Float limits are adjusted to 24 or 53 by the database as per http://msdn.microsoft.com/en-us/library/ms173773.aspx
242242
# Floats with a limit of <= 24 are reduced to reals by sqlserver on creation.
243243

244-
it 'float(53)' do
244+
it 'float' do
245245
col = column('float')
246-
col.sql_type.must_equal 'float(53)'
246+
col.sql_type.must_equal 'float'
247247
col.null.must_equal true
248248
col.default.must_equal 123.00000001
249249
obj.float.must_equal 123.00000001
@@ -252,7 +252,7 @@ def assert_obj_set_and_save(attribute, value)
252252
type.must_be_instance_of Type::Float
253253
type.type.must_equal :float
254254
type.must_be :number?
255-
type.limit.must_equal 53
255+
type.limit.must_equal nil
256256
type.precision.must_equal nil
257257
type.scale.must_equal nil
258258
obj.float = '214748.36461'
@@ -261,21 +261,9 @@ def assert_obj_set_and_save(attribute, value)
261261
obj.reload.float.must_equal 214748.36461
262262
end
263263

264-
it 'float(25)' do
265-
col = column('float_25')
266-
col.sql_type.must_equal 'float(53)'
267-
col.null.must_equal true
268-
col.default.must_equal 420.11
269-
col.default_function.must_equal nil
270-
type = col.cast_type
271-
type.must_be_instance_of Type::Float
272-
type.type.must_equal :float
273-
type.limit.must_equal 53
274-
end
275-
276-
it 'real(24)' do
264+
it 'real' do
277265
col = column('real')
278-
col.sql_type.must_equal 'real(24)'
266+
col.sql_type.must_equal 'real'
279267
col.null.must_equal true
280268
col.default.must_be_close_to 123.45, 0.01
281269
obj.real.must_be_close_to 123.45, 0.01
@@ -284,7 +272,7 @@ def assert_obj_set_and_save(attribute, value)
284272
type.must_be_instance_of Type::Real
285273
type.type.must_equal :real
286274
type.must_be :number?
287-
type.limit.must_equal 24
275+
type.limit.must_equal nil
288276
type.precision.must_equal nil
289277
type.scale.must_equal nil
290278
obj.real = '214748.36461'

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
2121
assert_line :money, type: 'money', limit: nil, precision: '19', scale: '4', default: '4.2'
2222
assert_line :smallmoney, type: 'smallmoney', limit: nil, precision: '10', scale: '4', default: '4.2'
2323
# Approximate Numerics
24-
assert_line :float, type: 'float', limit: '53', precision: nil, scale: nil, default: '123.00000001'
25-
assert_line :float_25, type: 'float', limit: '53', precision: nil, scale: nil, default: '420.11'
26-
assert_line :real, type: 'real', limit: '24', precision: nil, scale: nil, default: %r{123.4[45]}
24+
assert_line :float, type: 'float', limit: nil, precision: nil, scale: nil, default: '123.00000001'
25+
assert_line :real, type: 'real', limit: nil, precision: nil, scale: nil, default: %r{123.4[45]}
2726
# Date and Time
2827
assert_line :date, type: 'date', limit: nil, precision: nil, scale: nil, default: "'0001-01-01'"
2928
assert_line :datetime, type: 'datetime', limit: nil, precision: nil, scale: nil, default: "'1753-01-01 00:00:00'"
@@ -54,7 +53,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
5453
columns['bigint_col'].sql_type.must_equal 'bigint(8)'
5554
columns['boolean_col'].sql_type.must_equal 'bit'
5655
columns['decimal_col'].sql_type.must_equal 'decimal(18,0)'
57-
columns['float_col'].sql_type.must_equal 'real(24)'
56+
columns['float_col'].sql_type.must_equal 'float'
5857
columns['string_col'].sql_type.must_equal 'nvarchar(4000)'
5958
columns['text_col'].sql_type.must_equal 'nvarchar(max)'
6059
columns['datetime_col'].sql_type.must_equal 'datetime'
@@ -66,14 +65,35 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
6665
assert_line :bigint_col, type: 'bigint', limit: '8', precision: nil, scale: nil, default: nil
6766
assert_line :boolean_col, type: 'boolean', limit: nil, precision: nil, scale: nil, default: nil
6867
assert_line :decimal_col, type: 'decimal', limit: nil, precision: '18', scale: '0', default: nil
69-
assert_line :float_col, type: 'real', limit: '24', precision: nil, scale: nil, default: nil
68+
assert_line :float_col, type: 'float', limit: nil, precision: nil, scale: nil, default: nil
7069
assert_line :string_col, type: 'string', limit: '4000', precision: nil, scale: nil, default: nil
7170
assert_line :text_col, type: 'text', limit: '2147483647', precision: nil, scale: nil, default: nil
7271
assert_line :datetime_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
7372
assert_line :timestamp_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
7473
assert_line :time_col, type: 'time', limit: nil, precision: '7', scale: nil, default: nil
7574
assert_line :date_col, type: 'date', limit: nil, precision: nil, scale: nil, default: nil
7675
assert_line :binary_col, type: 'binary', limit: '2147483647', precision: nil, scale: nil, default: nil
76+
# Our type methods.
77+
columns['real_col'].sql_type.must_equal 'real'
78+
columns['money_col'].sql_type.must_equal 'money'
79+
columns['smallmoney_col'].sql_type.must_equal 'smallmoney'
80+
columns['char_col'].sql_type.must_equal 'char(1)'
81+
columns['varchar_col'].sql_type.must_equal 'varchar(8000)'
82+
columns['text_basic_col'].sql_type.must_equal 'text'
83+
columns['nchar_col'].sql_type.must_equal 'nchar(1)'
84+
columns['ntext_col'].sql_type.must_equal 'ntext'
85+
columns['binary_basic_col'].sql_type.must_equal 'binary(1)'
86+
columns['varbinary_col'].sql_type.must_equal 'varbinary(8000)'
87+
assert_line :real_col, type: 'real', limit: nil, precision: nil, scale: nil, default: nil
88+
assert_line :money_col, type: 'money', limit: nil, precision: '19', scale: '4', default: nil
89+
assert_line :smallmoney_col, type: 'smallmoney', limit: nil, precision: '10', scale: '4', default: nil
90+
assert_line :char_col, type: 'char', limit: '1', precision: nil, scale: nil, default: nil
91+
assert_line :varchar_col, type: 'varchar', limit: '8000', precision: nil, scale: nil, default: nil
92+
assert_line :text_basic_col, type: 'text_basic', limit: '2147483647', precision: nil, scale: nil, default: nil
93+
assert_line :nchar_col, type: 'nchar', limit: '1', precision: nil, scale: nil, default: nil
94+
assert_line :ntext_col, type: 'ntext', limit: '2147483647', precision: nil, scale: nil, default: nil
95+
assert_line :binary_basic_col, type: 'binary_basic', limit: '1', precision: nil, scale: nil, default: nil
96+
assert_line :varbinary_col, type: 'varbinary', limit: '8000', precision: nil, scale: nil, default: nil
7797
end
7898

7999
# Special Cases

test/schema/datatypes/2012.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ CREATE TABLE [sst_datatypes] (
2121
[smallmoney] [smallmoney] NULL DEFAULT 4.20,
2222
-- Approximate Numerics
2323
[float] [float] NULL DEFAULT 123.00000001,
24-
[float_25] [float](25) NULL DEFAULT 420.11,
2524
[real] [real] NULL DEFAULT 123.45,
2625
-- Date and Time
2726
[date] [date] NULL DEFAULT '0001-01-01',
@@ -45,7 +44,6 @@ CREATE TABLE [sst_datatypes] (
4544
[varbinary_max] [varbinary](max) NULL,
4645
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
4746

48-
4947
-- Date and Time (TODO)
5048
-- --------------------
5149
-- [datetime2_7] [datetime2](7) NULL,

test/schema/sqlserver_specific_schema.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818
t.time :time_col
1919
t.date :date_col
2020
t.binary :binary_col
21-
22-
23-
# Our type methods. Make sure we can reverse schema dump for [sst_datatypes] table.
24-
# ...
21+
# Our type methods.
22+
t.real :real_col
23+
t.money :money_col
24+
t.smallmoney :smallmoney_col
25+
t.char :char_col
26+
t.varchar :varchar_col
27+
t.text_basic :text_basic_col
28+
t.nchar :nchar_col
29+
t.ntext :ntext_col
30+
t.binary_basic :binary_basic_col
31+
t.varbinary :varbinary_col
2532
end
2633

2734
# Edge Cases

0 commit comments

Comments
 (0)