@@ -1556,15 +1556,33 @@ if Code.ensure_loaded?(Postgrex) do
15561556 end
15571557
15581558 defp column_change ( table , { :modify , name , type , opts } ) do
1559- [
1560- drop_reference_expr ( opts [ :from ] , table , name ) ,
1561- "ALTER COLUMN " ,
1562- quote_name ( name ) ,
1563- " TYPE " ,
1564- column_type ( type , opts ) ,
1565- modify_null ( name , opts ) ,
1566- modify_default ( name , type , opts )
1567- ]
1559+ column_type = column_type ( type , opts )
1560+
1561+ extract_type = fn
1562+ { type , _ } when is_atom ( type ) -> type
1563+ type when is_atom ( type ) -> type
1564+ _ -> nil
1565+ end
1566+
1567+ from_type = extract_type . ( opts [ :from ] )
1568+
1569+ if column_type == column_type ( from_type , opts ) do
1570+ [
1571+ drop_reference_expr ( opts [ :from ] , table , name ) ,
1572+ modify_null ( name , Keyword . put ( opts , :prefix_with_comma , false ) ) ,
1573+ modify_default ( name , type , opts )
1574+ ]
1575+ else
1576+ [
1577+ drop_reference_expr ( opts [ :from ] , table , name ) ,
1578+ "ALTER COLUMN " ,
1579+ quote_name ( name ) ,
1580+ " TYPE " ,
1581+ column_type ,
1582+ modify_null ( name , opts ) ,
1583+ modify_default ( name , type , opts )
1584+ ]
1585+ end
15681586 end
15691587
15701588 defp column_change ( _table , { :remove , name } ) , do: [ "DROP COLUMN " , quote_name ( name ) ]
@@ -1591,9 +1609,12 @@ if Code.ensure_loaded?(Postgrex) do
15911609 do: [ "DROP COLUMN IF EXISTS " , quote_name ( name ) ]
15921610
15931611 defp modify_null ( name , opts ) do
1612+ prefix_with_comma = Keyword . get ( opts , :prefix_with_comma , true )
1613+ prefix = if prefix_with_comma , do: ", " , else: ""
1614+
15941615 case Keyword . get ( opts , :null ) do
1595- true -> [ ", ALTER COLUMN ", quote_name ( name ) , " DROP NOT NULL" ]
1596- false -> [ ", ALTER COLUMN ", quote_name ( name ) , " SET NOT NULL" ]
1616+ true -> [ prefix , " ALTER COLUMN ", quote_name ( name ) , " DROP NOT NULL" ]
1617+ false -> [ prefix , " ALTER COLUMN ", quote_name ( name ) , " SET NOT NULL" ]
15971618 nil -> [ ]
15981619 end
15991620 end
0 commit comments