-
Notifications
You must be signed in to change notification settings - Fork 563
Fix change_column to preserve old column attributes. #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -196,14 +196,16 @@ def change_column(table_name, column_name, type, options = {}) | |
| end | ||
|
|
||
| column_object = schema_cache.columns(table_name).find { |c| c.name.to_s == column_name.to_s } | ||
| without_constraints = options.key?(:default) || options.key?(:limit) | ||
| changing_type = column_object && column_object.type != type.to_sym | ||
| no_constraint_options = options.key?(:default) || options.key?(:limit) | ||
|
|
||
| default = if !options.key?(:default) && column_object | ||
| column_object.default | ||
| else | ||
| options[:default] | ||
| end | ||
|
|
||
| if without_constraints || (column_object && column_object.type != type.to_sym) | ||
| if no_constraint_options || changing_type | ||
| remove_default_constraint(table_name, column_name) | ||
| indexes = indexes(table_name).select { |index| index.columns.include?(column_name.to_s) } | ||
| remove_indexes(table_name, column_name) | ||
|
|
@@ -212,10 +214,14 @@ def change_column(table_name, column_name, type, options = {}) | |
| sql_commands << "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_expression(options[:default], column_object)} WHERE #{quote_column_name(column_name)} IS NULL" if !options[:null].nil? && options[:null] == false && !options[:default].nil? | ||
| alter_command = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, limit: options[:limit], precision: options[:precision], scale: options[:scale])}" | ||
| alter_command += " COLLATE #{options[:collation]}" if options[:collation].present? | ||
| alter_command += " NOT NULL" if !options[:null].nil? && options[:null] == false | ||
| if !options[:null].nil? | ||
| alter_command += " NOT NULL" if options[:null] == false | ||
| elsif column_object && !column_object.null | ||
| alter_command += " NOT NULL" | ||
| end | ||
| sql_commands << alter_command | ||
|
|
||
| if without_constraints | ||
| if no_constraint_options || (changing_type && default.present?) | ||
| default = quote_default_expression(default, column_object || column_for(table_name, column_name)) | ||
| sql_commands << "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name, column_name)} DEFAULT #{default} FOR #{quote_column_name(column_name)}" | ||
|
Comment on lines
+224
to
226
|
||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,11 +34,13 @@ class MigrationTestSQLServer < ActiveRecord::TestCase | |
| lock_version_column = Person.columns_hash["lock_version"] | ||
| assert_equal :integer, lock_version_column.type | ||
| assert lock_version_column.default.present? | ||
| assert_equal 0, lock_version_column.default | ||
| assert_nothing_raised { connection.change_column "people", "lock_version", :string } | ||
| Person.reset_column_information | ||
| lock_version_column = Person.columns_hash["lock_version"] | ||
| assert_equal :string, lock_version_column.type | ||
| assert lock_version_column.default.nil? | ||
| assert lock_version_column.default.present? | ||
| assert_equal "0", lock_version_column.default | ||
| assert_nothing_raised { connection.change_column "people", "lock_version", :integer } | ||
|
Comment on lines
33
to
44
|
||
| Person.reset_column_information | ||
| end | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.