Skip to content

Commit

Permalink
Allow setting a default for a column that already has a default on Mi…
Browse files Browse the repository at this point in the history
…crosoft SQL Server

Microsoft SQL Server complains because it treates defaults as
constraints and requires you to drop a constraint before adding
one.

We already had code to drop the constraint, but it was only used
for drop_column, not for set_column default previously.
  • Loading branch information
jeremyevans committed Sep 10, 2019
1 parent f7dfdd0 commit 3676231
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Allow setting a default for a column that already has a default on Microsoft SQL Server (jeremyevans)

* Fix keyword argument separation warnings on Ruby master branch in csv_serializer plugin (jeremyevans)

* Add association_multi_add_remove plugin for adding/removing multiple associated objects in a single method call (AlexWayfer, jeremyevans) (#1641)
Expand Down
4 changes: 3 additions & 1 deletion lib/sequel/adapters/shared/mssql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ def alter_table_sql(table, op)
end
"ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{type_literal(:type=>type)} #{'NOT ' unless op[:null]}NULL"
when :set_column_default
"ALTER TABLE #{quote_schema_table(table)} ADD CONSTRAINT #{quote_identifier("sequel_#{table}_#{op[:name]}_def")} DEFAULT #{literal(op[:default])} FOR #{quote_identifier(op[:name])}"
sqls = []
add_drop_default_constraint_sql(sqls, table, op[:name])
sqls << "ALTER TABLE #{quote_schema_table(table)} ADD CONSTRAINT #{quote_identifier("sequel_#{table}_#{op[:name]}_def")} DEFAULT #{literal(op[:default])} FOR #{quote_identifier(op[:name])}"
else
super(table, op)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/integration/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,15 @@
@ds.all.must_equal [{:id=>10}, {:id=>20}]
end

it "should set column defaults correctly if column has existing default" do
@db.create_table!(:items){Integer :id, :default=>10}
@ds.insert
@ds.all.must_equal [{:id=>10}]
@db.alter_table(:items){set_column_default :id, 20}
@ds.insert
@ds.all.must_equal [{:id=>10}, {:id=>20}]
end

it "should set column defaults to nil correctly" do
@db.create_table!(:items){Integer :id}
@ds.insert(:id=>10)
Expand Down

0 comments on commit 3676231

Please sign in to comment.