Skip to content

Commit 47eb57a

Browse files
committed
Provide change_column_null which is a new API method for use in migrations to allow you to change a column's allowance of NULL values.
1 parent f4be968 commit 47eb57a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,19 @@ def change_column_default(table_name, column_name, default)
784784
execute "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(default, column_name)} FOR #{quote_column_name(column_name)}"
785785
end
786786

787+
def change_column_null(table_name, column_name, null, default = nil)
788+
column = columns(table_name).find { |c| c.name == column_name.to_s }
789+
790+
unless null || default.nil?
791+
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
792+
end
793+
794+
# TODO - work out what the reason is for column.sql_type != type_to_sql(column.type, column.limit, column.precision, column.scale)
795+
sql = "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql column.type, column.limit, column.precision, column.scale}"
796+
sql << " NOT NULL" unless null
797+
execute sql
798+
end
799+
787800
def remove_column(table_name, column_name)
788801
remove_check_constraints(table_name, column_name)
789802
remove_default_constraint(table_name, column_name)

0 commit comments

Comments
 (0)