Skip to content

Commit 1e02a4f

Browse files
Etienne Massipkares
authored andcommitted
Handle change_column error when column has default binding or indexes.
Tests for #455.
1 parent 7e6372a commit 1e02a4f

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lib/arjdbc/mssql/adapter.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,24 @@ def rename_column(table_name, column_name, new_column_name)
363363

364364
# @override
365365
def change_column(table_name, column_name, type, options = {})
366+
367+
indexes = []
368+
column_object = columns(table_name).detect { |c| c.name.to_s == column_name.to_s }
369+
370+
if options_include_default?(options) || (column_object && column_object.type != type.to_sym)
371+
remove_default_constraint(table_name, column_name)
372+
indexes = indexes(table_name).select{ |index| index.columns.include?(column_name.to_s) }
373+
remove_indexes(table_name, column_name)
374+
end
375+
366376
clear_cached_table(table_name)
367377
change_column_type(table_name, column_name, type, options)
368378
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
379+
380+
#Add any removed indexes back
381+
indexes.each do |index|
382+
execute "CREATE INDEX #{quote_table_name(index.name)} ON #{quote_table_name(table_name)} (#{index.columns.collect {|c|quote_column_name(c)}.join(', ')})"
383+
end
369384
end
370385

371386
def change_column_type(table_name, column_name, type, options = {})

test/models/entry.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def self.up
33
create_table "entries", :force => true do |t|
44
t.column :title, :string, :limit => 100
55
t.column :content, :text
6+
t.column :status, :string, :default => 'unknown'
67
t.column :rating, :decimal, :precision => 10, :scale => 2
78
t.column :user_id, :integer
89
t.column :updated_on, :datetime # treated as date "_on" convention

test/simple.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,36 @@ def test_add_null_column_with_no_default
618618
end
619619
end
620620

621+
def test_change_column_whithout_default_option_should_drop_existing_default
622+
623+
Entry.reset_column_information
624+
status_column = Entry.columns.find { |c| c.name == 'status' }
625+
assert_equal :string, status_column.type
626+
assert_equal 'unknown', status_column.default
627+
628+
Entry.connection.change_column :entries, :status, :text
629+
630+
Entry.reset_column_information
631+
status_column = Entry.columns.find { |c| c.name == 'status' }
632+
assert_equal :text, status_column.type
633+
assert !status_column.default
634+
end
635+
636+
def test_change_column_whith_default_option_should_set_new_default
637+
638+
Entry.reset_column_information
639+
status_column = Entry.columns.find { |c| c.name == 'status' }
640+
assert_equal :string, status_column.type
641+
assert_equal 'unknown', status_column.default
642+
643+
Entry.connection.change_column :entries, :status, :text, :default => 'new'
644+
645+
Entry.reset_column_information
646+
status_column = Entry.columns.find { |c| c.name == 'status' }
647+
assert_equal :text, status_column.type
648+
assert_equal 'new', status_column.default
649+
end
650+
621651
def test_add_null_column_with_nil_default
622652
# You must specify a default value with most databases
623653
if ActiveRecord::Base.connection.adapter_name =~ /mysql/i
@@ -1164,6 +1194,7 @@ def test_select_rows
11641194
when 'id' then assert_not_nil row[i]
11651195
when 'title' then assert_equal 'title 1', row[i]
11661196
when 'content' then assert_equal 'content 1', row[i]
1197+
when 'status' then assert_equal 'unknown', row[i]
11671198
when 'user_id'
11681199
if defined? JRUBY_VERSION
11691200
assert_equal user.id, row[i]
@@ -1182,6 +1213,7 @@ def test_select_rows
11821213
when 'id' then assert_not_nil row[i]
11831214
when 'title' then assert_equal 'title 2', row[i]
11841215
when 'content' then assert_equal 'content 2', row[i]
1216+
when 'status' then assert_equal 'unknown', row[i]
11851217
when 'user_id'
11861218
if defined? JRUBY_VERSION
11871219
assert_equal user.id, row[i]

0 commit comments

Comments
 (0)