Skip to content

Commit

Permalink
New #add_column_options wiht identity support. Support NotNullViolati…
Browse files Browse the repository at this point in the history
…on error.

Fixes the following tests.

* ActiveRecord::AdapterTest#test_not_null_violations_are_translated_to_specific_exception
* ActiveRecord::Migration::ChangeSchemaTest#test_create_table_with_not_null_column
* ActiveRecord::Migration::ChangeSchemaTest#test_add_column_not_null_with_default
* ActiveRecord::Migration::ChangeSchemaTest#test_add_column_not_null_without_default
* LegacyPrimaryKeyTest#test_legacy_bigint_primary_key_should_not_be_auto_incremented
* LegacyPrimaryKeyTest#test_legacy_primary_key_should_be_auto_incremented
* LegacyPrimaryKeyTest#test_legacy_integer_primary_key_should_not_be_auto_incremented
* PrimaryKeyWithAutoIncrementTest#test_primary_key_with_bigint
* PrimaryKeyWithAutoIncrementTest#test_primary_key_with_integer
  • Loading branch information
metaskills committed May 22, 2017
1 parent b7b3aa8 commit a62f7b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/active_record/connection_adapters/sqlserver/schema_creation.rb
Expand Up @@ -16,6 +16,20 @@ def visit_TableDefinition(o)
end
end

def add_column_options!(sql, options)
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
if options[:null] == false
sql << " NOT NULL"
end
if options[:is_identity] == true
sql << " IDENTITY(1,1)"
end
if options[:primary_key] == true
sql << " PRIMARY KEY"
end
sql
end

def action_sql(action, dependency)
case dependency
when :restrict
Expand Down
Expand Up @@ -5,7 +5,9 @@ module SQLServer
module ColumnMethods

def primary_key(name, type = :primary_key, **options)
if type == :uuid
if [:integer, :bigint].include?(type)
options[:is_identity] = true unless options.key?(:default)
elsif type == :uuid
options[:default] = options.fetch(:default, 'NEWID()')
options[:primary_key] = true
end
Expand Down Expand Up @@ -103,6 +105,8 @@ def new_column_definition(name, type, **options)
case type
when :datetime
type = :datetime2 if options[:precision]
when :primary_key
options[:is_identity] = true
end
super
end
Expand Down
2 changes: 2 additions & 0 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Expand Up @@ -337,6 +337,8 @@ def translate_exception(e, message)
target_table: pk_id.schema,
primary_key: pk_id.object
)
when /Cannot insert the value NULL into column.*does not allow nulls/
NotNullViolation.new(message)
else
super
end
Expand Down

0 comments on commit a62f7b9

Please sign in to comment.