@@ -1532,15 +1532,15 @@ if Code.ensure_loaded?(Postgrex) do
15321532 [
15331533 quote_name ( name ) ,
15341534 ?\s ,
1535- reference_column_type ( ref . type , opts ) ,
1535+ add_reference_column_type ( ref . type , opts ) ,
15361536 column_options ( ref . type , opts ) ,
15371537 ", " ,
15381538 reference_expr ( ref , table , name )
15391539 ]
15401540 end
15411541
15421542 defp column_definition ( _table , { :add , name , type , opts } ) do
1543- [ quote_name ( name ) , ?\s , column_type ( type , opts ) , column_options ( type , opts ) ]
1543+ [ quote_name ( name ) , ?\s , add_column_type ( type , opts ) , column_options ( type , opts ) ]
15441544 end
15451545
15461546 defp column_changes ( table , columns ) do
@@ -1552,23 +1552,29 @@ if Code.ensure_loaded?(Postgrex) do
15521552 "ADD COLUMN " ,
15531553 quote_name ( name ) ,
15541554 ?\s ,
1555- reference_column_type ( ref . type , opts ) ,
1555+ add_reference_column_type ( ref . type , opts ) ,
15561556 column_options ( ref . type , opts ) ,
15571557 ", ADD " ,
15581558 reference_expr ( ref , table , name )
15591559 ]
15601560 end
15611561
15621562 defp column_change ( _table , { :add , name , type , opts } ) do
1563- [ "ADD COLUMN " , quote_name ( name ) , ?\s , column_type ( type , opts ) , column_options ( type , opts ) ]
1563+ [
1564+ "ADD COLUMN " ,
1565+ quote_name ( name ) ,
1566+ ?\s ,
1567+ add_column_type ( type , opts ) ,
1568+ column_options ( type , opts )
1569+ ]
15641570 end
15651571
15661572 defp column_change ( table , { :add_if_not_exists , name , % Reference { } = ref , opts } ) do
15671573 [
15681574 "ADD COLUMN IF NOT EXISTS " ,
15691575 quote_name ( name ) ,
15701576 ?\s ,
1571- reference_column_type ( ref . type , opts ) ,
1577+ add_reference_column_type ( ref . type , opts ) ,
15721578 column_options ( ref . type , opts ) ,
15731579 ", ADD " ,
15741580 reference_expr ( ref , table , name )
@@ -1580,7 +1586,7 @@ if Code.ensure_loaded?(Postgrex) do
15801586 "ADD COLUMN IF NOT EXISTS " ,
15811587 quote_name ( name ) ,
15821588 ?\s ,
1583- column_type ( type , opts ) ,
1589+ add_column_type ( type , opts ) ,
15841590 column_options ( type , opts )
15851591 ]
15861592 end
@@ -1594,11 +1600,11 @@ if Code.ensure_loaded?(Postgrex) do
15941600 quote_name ( name ) ,
15951601 " TYPE " ,
15961602 reference_column_type ( ref . type , opts ) ,
1603+ collation_expr ( collation ) ,
15971604 ", ADD " ,
15981605 reference_expr ( ref , table , name ) ,
15991606 modify_null ( name , opts ) ,
1600- modify_default ( name , ref . type , opts ) ,
1601- collation_expr ( collation )
1607+ modify_default ( name , ref . type , opts )
16021608 ]
16031609 end
16041610
@@ -1611,9 +1617,9 @@ if Code.ensure_loaded?(Postgrex) do
16111617 quote_name ( name ) ,
16121618 " TYPE " ,
16131619 modify_column_type ( type , opts ) ,
1620+ collation_expr ( collation ) ,
16141621 modify_null ( name , opts ) ,
16151622 modify_default ( name , type , opts ) ,
1616- collation_expr ( collation ) ,
16171623 modify_identity ( name , type , opts )
16181624 ]
16191625 end
@@ -1675,18 +1681,23 @@ if Code.ensure_loaded?(Postgrex) do
16751681 defp column_options ( type , opts ) do
16761682 default = Keyword . fetch ( opts , :default )
16771683 null = Keyword . get ( opts , :null )
1678- collation = Keyword . fetch ( opts , :collation )
16791684
1680- [ default_expr ( default , type ) , null_expr ( null ) , collation_expr ( collation ) ]
1685+ [ default_expr ( default , type ) , null_expr ( null ) ]
16811686 end
16821687
16831688 defp null_expr ( false ) , do: " NOT NULL"
16841689 defp null_expr ( true ) , do: " NULL"
16851690 defp null_expr ( _ ) , do: [ ]
16861691
1687- defp collation_expr ( { :ok , collation_name } ) , do: " COLLATE \" #{ collation_name } \" "
1692+ defp collation_expr ( { :ok , name } ) , do: [ " COLLATE " , quote_collation_name ( name ) ]
16881693 defp collation_expr ( _ ) , do: [ ]
16891694
1695+ defp quote_collation_name ( name ) when is_atom ( name ) ,
1696+ do: quote_collation_name ( Atom . to_string ( name ) )
1697+
1698+ defp quote_collation_name ( name ) when is_binary ( name ) ,
1699+ do: [ ?" , String . replace ( name , "\" " , "\" \" " ) , ?" ]
1700+
16901701 defp new_constraint_expr ( % Constraint { check: check } = constraint ) when is_binary ( check ) do
16911702 [
16921703 "CONSTRAINT " ,
@@ -1815,18 +1826,28 @@ if Code.ensure_loaded?(Postgrex) do
18151826 defp options_expr ( options ) ,
18161827 do: [ ?\s , options ]
18171828
1818- defp column_type ( type , opts ) do
1829+ defp add_column_type ( type , opts ) do
1830+ column_type ( type , opts , collation_expr ( Keyword . fetch ( opts , :collation ) ) )
1831+ end
1832+
1833+ defp add_reference_column_type ( type , opts ) when type in [ :serial , :bigserial , :identity ] do
1834+ [ reference_column_type ( type , opts ) , collation_expr ( Keyword . fetch ( opts , :collation ) ) ]
1835+ end
1836+
1837+ defp add_reference_column_type ( type , opts ) , do: add_column_type ( type , opts )
1838+
1839+ defp column_type ( type , opts , collation \\ [ ] ) do
18191840 type_name = column_type_name ( type , opts )
18201841
18211842 case Keyword . get ( opts , :generated ) do
18221843 nil when type == :identity ->
1823- [ type_name , identity_generated_expr ( opts ) ]
1844+ [ type_name , collation , identity_generated_expr ( opts ) ]
18241845
18251846 nil ->
1826- type_name
1847+ [ type_name , collation ]
18271848
18281849 expr when is_binary ( expr ) ->
1829- [ type_name , " GENERATED " , expr ]
1850+ [ type_name , collation , " GENERATED " , expr ]
18301851
18311852 other ->
18321853 raise ArgumentError ,
0 commit comments