1+ # frozen_string_literal: true
2+
13module ActiveRecord
24 module ConnectionAdapters
35 module SQLServer
@@ -152,8 +154,9 @@ def change_column(table_name, column_name, type, options = {})
152154 remove_indexes ( table_name , column_name )
153155 end
154156 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?
155- sql_commands << "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 ] ) } "
156- sql_commands . last << ' NOT NULL' if !options [ :null ] . nil? && options [ :null ] == false
157+ 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 ] ) } "
158+ alter_command += ' NOT NULL' if !options [ :null ] . nil? && options [ :null ] == false
159+ sql_commands << alter_command
157160 if without_constraints
158161 default = quote_default_expression ( default , column_object || column_for ( table_name , column_name ) )
159162 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 ) } "
@@ -267,7 +270,7 @@ def change_column_null(table_name, column_name, allow_null, default = nil)
267270 do_execute ( "UPDATE #{ table_id } SET #{ column_id } =#{ quote ( default ) } WHERE #{ column_id } IS NULL" )
268271 end
269272 sql = "ALTER TABLE #{ table_id } ALTER COLUMN #{ column_id } #{ type_to_sql column . type , limit : column . limit , precision : column . precision , scale : column . scale } "
270- sql << ' NOT NULL' if !allow_null . nil? && allow_null == false
273+ sql += ' NOT NULL' if !allow_null . nil? && allow_null == false
271274 do_execute sql
272275 end
273276
@@ -281,12 +284,12 @@ def data_source_sql(name = nil, type: nil)
281284 scope = quoted_scope name , type : type
282285 table_name = lowercase_schema_reflection_sql 'TABLE_NAME'
283286 sql = "SELECT #{ table_name } "
284- sql << ' FROM INFORMATION_SCHEMA.TABLES WITH (NOLOCK)'
285- sql << ' WHERE TABLE_CATALOG = DB_NAME()'
286- sql << " AND TABLE_SCHEMA = #{ quote ( scope [ :schema ] ) } "
287- sql << " AND TABLE_NAME = #{ quote ( scope [ :name ] ) } " if scope [ :name ]
288- sql << " AND TABLE_TYPE = #{ quote ( scope [ :type ] ) } " if scope [ :type ]
289- sql << " ORDER BY #{ table_name } "
287+ sql += ' FROM INFORMATION_SCHEMA.TABLES WITH (NOLOCK)'
288+ sql += ' WHERE TABLE_CATALOG = DB_NAME()'
289+ sql += " AND TABLE_SCHEMA = #{ quote ( scope [ :schema ] ) } "
290+ sql += " AND TABLE_NAME = #{ quote ( scope [ :name ] ) } " if scope [ :name ]
291+ sql += " AND TABLE_TYPE = #{ quote ( scope [ :type ] ) } " if scope [ :type ]
292+ sql += " ORDER BY #{ table_name } "
290293 sql
291294 end
292295
0 commit comments