Skip to content

Commit 89ccbe4

Browse files
committed
Pass PrimaryKeyIntegerNilDefaultTest tests.
* Allow create tables with PKs like `[id] integer DEFAULT NUL NOT NULL PRIMARY KEY` * Note how IDENTITY(1,1) incrementing is not present. Not allowed with columns that have defaults.
1 parent 3f66c43 commit 89ccbe4

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def action_sql(action, dependency)
2828
end
2929
end
3030

31+
def options_include_default?(options)
32+
super || options_primary_key_with_nil_default?(options)
33+
end
34+
35+
def options_primary_key_with_nil_default?(options)
36+
options[:primary_key] && options.include?(:default) && options[:default].nil?
37+
end
38+
3139
end
3240
end
3341
end

lib/active_record/connection_adapters/sqlserver/schema_dumper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ module SchemaDumper
55

66
private
77

8+
def explicit_primary_key_default?(column)
9+
column.is_primary? && !column.is_identity?
10+
end
11+
812
def schema_collation(column)
913
return unless column.collation
1014
column.collation if column.collation != collation
1115
end
1216

17+
def default_primary_key?(column)
18+
super && column.is_primary? && column.is_identity?
19+
end
20+
1321
end
1422
end
1523
end

lib/active_record/connection_adapters/sqlserver/table_definition.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ module SQLServer
55
module ColumnMethods
66

77
def primary_key(name, type = :primary_key, **options)
8-
return super unless type == :uuid
9-
options[:default] = options.fetch(:default, 'NEWID()')
10-
options[:primary_key] = true
11-
column name, type, options
8+
if type == :uuid
9+
options[:default] = options.fetch(:default, 'NEWID()')
10+
options[:primary_key] = true
11+
end
12+
super
1213
end
1314

1415
def primary_key_nonclustered(*args, **options)
@@ -98,9 +99,12 @@ def json(*args, **options)
9899
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
99100
include ColumnMethods
100101

101-
def new_column_definition(name, type, options)
102-
type = :datetime2 if type == :datetime && options[:precision]
103-
super name, type, options
102+
def new_column_definition(name, type, **options)
103+
case type
104+
when :datetime
105+
type = :datetime2 if options[:precision]
106+
end
107+
super
104108
end
105109
end
106110

0 commit comments

Comments
 (0)