Skip to content

Commit 0e8125a

Browse files
committed
Add [timestamp] data type support as a binary value. Allow migrations to use :ss_timestamp for a real SQL Server binary timestamp column.
1 parent f57cec9 commit 0e8125a

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

README.rdoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ The SQL Server adapter for ActiveRecord.
66

77
== What's New
88

9-
* Version 3.x now supports rails 3!
9+
* New dblib connection mode using TinyTds!
10+
* Rails 3 support!
1011

1112

1213
==== Testing Rake Tasks Support

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ def initialize_native_database_types
168168
:nchar => { :name => "nchar" },
169169
:nvarchar => { :name => "nvarchar", :limit => 255 },
170170
:nvarchar_max => { :name => "nvarchar(max)" },
171-
:ntext => { :name => "ntext" }
171+
:ntext => { :name => "ntext" },
172+
:ss_timestamp => { :name => 'timestamp' }
172173
})
173174
end
174175

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ def binary_to_string(value)
8787
def type_cast(value)
8888
if value && type == :string && is_utf8?
8989
self.class.string_to_utf8_encoding(value)
90-
elsif value && type == :timestamp
91-
"0x#{value}"
9290
else
9391
super
9492
end
@@ -158,6 +156,7 @@ def simplified_type(field_type)
158156
when /uniqueidentifier/i then :string
159157
when /datetime/i then simplified_datetime
160158
when /varchar\(max\)/ then :text
159+
when /timestamp/ then :binary
161160
else super
162161
end
163162
end

test/cases/column_test_sqlserver.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def setup
149149
@time = SqlServerChronic.columns_hash['time']
150150
@datetime = SqlServerChronic.columns_hash['datetime']
151151
@smalldatetime = SqlServerChronic.columns_hash['smalldatetime']
152+
@timestamp = SqlServerChronic.columns_hash['timestamp']
153+
@ss_timestamp = SqlServerChronic.columns_hash['ss_timestamp']
152154
end
153155

154156
should 'have correct simplified type for uncast datetime' do
@@ -172,6 +174,25 @@ def setup
172174
assert_equal nil, @datetime.limit
173175
end
174176

177+
context 'with timestamps' do
178+
179+
should 'use datetime sql type when using :timestamp in schema statements' do
180+
assert_equal :datetime, @timestamp.type
181+
assert_equal 'datetime', @timestamp.sql_type
182+
end
183+
184+
should 'be able to use real sql server timestamp if you really want to' do
185+
assert_equal :binary, @ss_timestamp.type
186+
assert_equal 'timestamp', @ss_timestamp.sql_type
187+
end
188+
189+
should 'return :timestamp as a binaryish string' do
190+
chronic = SqlServerChronic.create!.reload
191+
assert_match %r|\000|, chronic.ss_timestamp
192+
end
193+
194+
end
195+
175196
context 'For smalldatetime types' do
176197

177198
should 'have created that type using rails migrations' do

test/schema/sqlserver_specific_schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
t.column :time, :time
2626
t.column :datetime, :datetime
2727
t.column :timestamp, :timestamp
28+
t.column :ss_timestamp, :ss_timestamp
2829
t.column :smalldatetime, :smalldatetime
2930
end
3031

0 commit comments

Comments
 (0)