Skip to content

Commit e3141a9

Browse files
committed
Implement our own binary timestamp type again. Remove substitute_at hack.
1 parent 8eff660 commit e3141a9

File tree

9 files changed

+73
-29
lines changed

9 files changed

+73
-29
lines changed

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ def quote_default_value(value, column)
2323
end
2424
end
2525

26-
def substitute_at(column, _unused = 0)
27-
return nil if column.respond_to?(:sql_type) && column.sql_type == 'timestamp'
28-
super
29-
end
30-
3126
def quoted_true
3227
QUOTED_TRUE
3328
end

lib/active_record/connection_adapters/sqlserver/table_definition.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def uuid(name, options = {})
5858
column(name, :uniqueidentifier, options)
5959
end
6060

61+
def ss_timestamp(name, options = {})
62+
column(name, :ss_timestamp, options)
63+
end
64+
6165
end
6266
end
6367
end

lib/active_record/connection_adapters/sqlserver/type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
require 'active_record/connection_adapters/sqlserver/type/varbinary_max.rb'
3838
# Other Data Types
3939
require 'active_record/connection_adapters/sqlserver/type/uuid.rb'
40+
require 'active_record/connection_adapters/sqlserver/type/timestamp.rb'
4041

4142
module ActiveRecord
4243
module Type
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module ActiveRecord
2+
module ConnectionAdapters
3+
module SQLServer
4+
module Type
5+
class Timestamp < Binary
6+
7+
def type
8+
:ss_timestamp
9+
end
10+
11+
end
12+
end
13+
end
14+
end
15+
end

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ def initialize_type_map(m)
231231
m.register_type 'varbinary(max)', SQLServer::Type::VarbinaryMax.new
232232
# Other Data Types
233233
m.register_type 'uniqueidentifier', SQLServer::Type::Uuid.new
234+
m.register_type 'timestamp', SQLServer::Type::Timestamp.new
234235
end
235236

236237
def translate_exception(e, message)

test/cases/column_test_sqlserver.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,29 @@ def assert_obj_set_and_save(attribute, value)
675675
obj.uniqueidentifier.must_equal "6F9619FF-8B86-D011-B42D-00C04FC964FF"
676676
end
677677

678+
it 'timestamp' do
679+
col = column('timestamp')
680+
col.sql_type.must_equal 'timestamp'
681+
col.null.must_equal true
682+
col.default.must_equal nil
683+
col.default_function.must_equal nil
684+
type = col.cast_type
685+
type.must_be_instance_of Type::Timestamp
686+
type.type.must_equal :ss_timestamp
687+
type.wont_be :number?
688+
type.limit.must_equal nil
689+
type.precision.must_equal nil
690+
type.scale.must_equal nil
691+
# Basic read.
692+
obj.timestamp.must_equal nil
693+
obj.save! ; obj.reload
694+
obj.timestamp.must_match %r|\000|
695+
obj.timestamp
696+
# Can set another attribute
697+
obj.uniqueidentifier = "6F9619FF-8B86-D011-B42D-00C04FC964FF"
698+
obj.save!
699+
end
700+
678701
end
679702

680703
end

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,37 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
4545
assert_line :varbinary_max, type: 'binary', limit: '2147483647', precision: nil, scale: nil, default: nil
4646
# Other Data Types
4747
assert_line :uniqueidentifier, type: 'uuid', limit: nil, precision: nil, scale: nil, default: nil
48+
assert_line :timestamp, type: 'ss_timestamp', limit: nil, precision: nil, scale: nil, default: nil
4849
end
4950

5051
it 'sst_datatypes_migration' do
5152
columns = SSTestDatatypeMigration.columns_hash
5253
generate_schema_for_table 'sst_datatypes_migration'
5354
# Simple Rails conventions
54-
columns['integer_col'].sql_type.must_equal 'int(4)'
55-
columns['bigint_col'].sql_type.must_equal 'bigint(8)'
56-
columns['boolean_col'].sql_type.must_equal 'bit'
57-
columns['decimal_col'].sql_type.must_equal 'decimal(18,0)'
58-
columns['float_col'].sql_type.must_equal 'float'
59-
columns['string_col'].sql_type.must_equal 'nvarchar(4000)'
60-
columns['text_col'].sql_type.must_equal 'nvarchar(max)'
61-
columns['datetime_col'].sql_type.must_equal 'datetime'
62-
columns['timestamp_col'].sql_type.must_equal 'datetime'
63-
columns['time_col'].sql_type.must_equal 'time(7)'
64-
columns['date_col'].sql_type.must_equal 'date'
65-
columns['binary_col'].sql_type.must_equal 'varbinary(max)'
66-
assert_line :integer_col, type: 'integer', limit: '4', precision: nil, scale: nil, default: nil
67-
assert_line :bigint_col, type: 'bigint', limit: '8', precision: nil, scale: nil, default: nil
68-
assert_line :boolean_col, type: 'boolean', limit: nil, precision: nil, scale: nil, default: nil
69-
assert_line :decimal_col, type: 'decimal', limit: nil, precision: '18', scale: '0', default: nil
70-
assert_line :float_col, type: 'float', limit: nil, precision: nil, scale: nil, default: nil
71-
assert_line :string_col, type: 'string', limit: '4000', precision: nil, scale: nil, default: nil
72-
assert_line :text_col, type: 'text', limit: '2147483647', precision: nil, scale: nil, default: nil
73-
assert_line :datetime_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
74-
assert_line :timestamp_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
75-
assert_line :time_col, type: 'time', limit: nil, precision: nil, scale: nil, default: nil
76-
assert_line :date_col, type: 'date', limit: nil, precision: nil, scale: nil, default: nil
77-
assert_line :binary_col, type: 'binary', limit: '2147483647', precision: nil, scale: nil, default: nil
55+
columns['integer_col'].sql_type.must_equal 'int(4)'
56+
columns['bigint_col'].sql_type.must_equal 'bigint(8)'
57+
columns['boolean_col'].sql_type.must_equal 'bit'
58+
columns['decimal_col'].sql_type.must_equal 'decimal(18,0)'
59+
columns['float_col'].sql_type.must_equal 'float'
60+
columns['string_col'].sql_type.must_equal 'nvarchar(4000)'
61+
columns['text_col'].sql_type.must_equal 'nvarchar(max)'
62+
columns['datetime_col'].sql_type.must_equal 'datetime'
63+
columns['timestamp_col'].sql_type.must_equal 'datetime'
64+
columns['time_col'].sql_type.must_equal 'time(7)'
65+
columns['date_col'].sql_type.must_equal 'date'
66+
columns['binary_col'].sql_type.must_equal 'varbinary(max)'
67+
assert_line :integer_col, type: 'integer', limit: '4', precision: nil, scale: nil, default: nil
68+
assert_line :bigint_col, type: 'bigint', limit: '8', precision: nil, scale: nil, default: nil
69+
assert_line :boolean_col, type: 'boolean', limit: nil, precision: nil, scale: nil, default: nil
70+
assert_line :decimal_col, type: 'decimal', limit: nil, precision: '18', scale: '0', default: nil
71+
assert_line :float_col, type: 'float', limit: nil, precision: nil, scale: nil, default: nil
72+
assert_line :string_col, type: 'string', limit: '4000', precision: nil, scale: nil, default: nil
73+
assert_line :text_col, type: 'text', limit: '2147483647', precision: nil, scale: nil, default: nil
74+
assert_line :datetime_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
75+
assert_line :timestamp_col, type: 'datetime', limit: nil, precision: nil, scale: nil, default: nil
76+
assert_line :time_col, type: 'time', limit: nil, precision: nil, scale: nil, default: nil
77+
assert_line :date_col, type: 'date', limit: nil, precision: nil, scale: nil, default: nil
78+
assert_line :binary_col, type: 'binary', limit: '2147483647', precision: nil, scale: nil, default: nil
7879
# Our type methods.
7980
columns['real_col'].sql_type.must_equal 'real'
8081
columns['money_col'].sql_type.must_equal 'money'
@@ -87,6 +88,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
8788
columns['binary_basic_col'].sql_type.must_equal 'binary(1)'
8889
columns['varbinary_col'].sql_type.must_equal 'varbinary(8000)'
8990
columns['uuid_col'].sql_type.must_equal 'uniqueidentifier'
91+
columns['sstimestamp_col'].sql_type.must_equal 'timestamp'
9092
assert_line :real_col, type: 'real', limit: nil, precision: nil, scale: nil, default: nil
9193
assert_line :money_col, type: 'money', limit: nil, precision: '19', scale: '4', default: nil
9294
assert_line :smallmoney_col, type: 'smallmoney', limit: nil, precision: '10', scale: '4', default: nil
@@ -98,6 +100,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
98100
assert_line :binary_basic_col, type: 'binary_basic', limit: '1', precision: nil, scale: nil, default: nil
99101
assert_line :varbinary_col, type: 'varbinary', limit: '8000', precision: nil, scale: nil, default: nil
100102
assert_line :uuid_col, type: 'uuid', limit: nil, precision: nil, scale: nil, default: nil
103+
assert_line :sstimestamp_col, type: 'ss_timestamp', limit: nil, precision: nil, scale: nil, default: nil
101104
end
102105

103106
# Special Cases

test/schema/datatypes/2012.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ CREATE TABLE [sst_datatypes] (
4444
[varbinary_max] [varbinary](max) NULL,
4545
-- Other Data Types
4646
[uniqueidentifier] [uniqueidentifier] NULL DEFAULT NEWID(),
47+
[timestamp] [timestamp] NULL,
4748
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
4849

4950
-- Date and Time (TODO)

test/schema/sqlserver_specific_schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
t.binary_basic :binary_basic_col
3131
t.varbinary :varbinary_col
3232
t.uuid :uuid_col
33+
t.ss_timestamp :sstimestamp_col
3334
end
3435

3536
# Edge Cases

0 commit comments

Comments
 (0)