Skip to content

Commit 75e9f01

Browse files
committed
Better column definitions for default objects. Fixes #412
1 parent 370689c commit 75e9f01

File tree

5 files changed

+20
-0
lines changed

5 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* Improve case comparision performace per column. Fixes #414
77
* DB rollback when reversable add_column has several options. Fixes #359
8+
* Better column definitions for default objects. Fixes #412
89

910

1011
## v4.2.4

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ def column_definitions(table_name)
312312
when /\A\(N'(.*)'\)\Z/m
313313
string_literal = SQLServer::Utils.unquote_string(Regexp.last_match[1])
314314
[string_literal, nil]
315+
when /CREATE DEFAULT/mi
316+
[nil, nil]
315317
else
316318
type = case ci[:type]
317319
when /smallint|int|bigint/ then ci[:_type]

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class SpecificSchemaTestSQLServer < ActiveRecord::TestCase
6262
assert_equal '(NULL)', default.string_with_pretend_null_four
6363
end
6464

65+
it 'default objects work' do
66+
obj = SSTestObjectDefault.create! name: 'MetaSkills'
67+
obj.date.must_be_nil 'since this is set on insert'
68+
obj.reload.date.must_be_instance_of Date
69+
end
70+
6571
# Natural primary keys.
6672

6773
it 'work with identity inserts' do
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SSTestObjectDefault < ActiveRecord::Base
2+
self.table_name = 'sst_defaultobjects'
3+
end

test/schema/sqlserver_specific_schema.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@
109109
)
110110
TINYITPKTABLE
111111

112+
execute "DROP DEFAULT [sst_getdateobject];" rescue nil
113+
execute "CREATE DEFAULT [sst_getdateobject] AS getdate();" rescue nil
114+
create_table 'sst_defaultobjects', force: true do |t|
115+
t.string :name
116+
t.date :date
117+
end
118+
execute "sp_bindefault 'sst_getdateobject', 'sst_defaultobjects.date'"
119+
112120
# Constraints
113121

114122
create_table(:sst_has_fks, force: true) { |t| t.column(:fk_id, :integer, null: false) }

0 commit comments

Comments
 (0)