@@ -1010,6 +1010,76 @@ def change_column_null_for_alter(table_name, column_name, null, default = nil)
1010
1010
end
1011
1011
end
1012
1012
1013
+ def add_foreign_key_for_alter ( from_table , to_table , **options )
1014
+ assert_valid_deferrable ( options [ :deferrable ] )
1015
+ return unless use_foreign_keys?
1016
+
1017
+ return if options [ :if_not_exists ] == true && foreign_key_exists? ( from_table , to_table , **options . slice ( :column ) )
1018
+
1019
+ options = foreign_key_options ( from_table , to_table , options )
1020
+ td = create_table_definition from_table
1021
+ fk = td . new_foreign_key_definition to_table , options
1022
+
1023
+ "ADD #{ schema_creation . accept ( fk ) } "
1024
+ end
1025
+
1026
+ def remove_foreign_key_for_alter ( from_table , to_table = nil , **options )
1027
+ return unless use_foreign_keys?
1028
+ return if options . delete ( :if_exists ) == true && !foreign_key_exists? ( from_table , to_table )
1029
+
1030
+ fk_name_to_delete = foreign_key_for! ( from_table , to_table : to_table , **options ) . name
1031
+ remove_constraint_for_alter ( from_table , fk_name_to_delete , **options )
1032
+ end
1033
+
1034
+ def add_check_constraint_for_alter ( table_name , expression , if_not_exists : false , **options )
1035
+ options = check_constraint_options ( table_name , expression , options )
1036
+ return if if_not_exists && check_constraint_exists? ( table_name , **options )
1037
+
1038
+ td = create_table_definition ( table_name )
1039
+ constraint = td . new_check_constraint_definition ( expression , options )
1040
+
1041
+ "ADD #{ schema_creation . accept ( constraint ) } "
1042
+ end
1043
+
1044
+ def remove_constraint_for_alter ( table_name , constraint , if_exists : false , **options )
1045
+ "DROP CONSTRAINT#{ ' IF EXISTS' if if_exists } #{ quote_column_name ( constraint ) } "
1046
+ end
1047
+
1048
+ def remove_check_constraint_for_alter ( table_name , expression = nil , **options )
1049
+ chk_name_to_delete = check_constraint_name ( table_name , expression : expression , **options )
1050
+
1051
+ remove_constraint_for_alter ( table_name , chk_name_to_delete , **options )
1052
+ end
1053
+
1054
+ def add_exclusion_constraint_for_alter ( table_name , expression , options )
1055
+ options = exclusion_constraint_options ( table_name , expression , options )
1056
+ td = create_table_definition ( table_name )
1057
+ constraint = td . new_exclusion_constraint_definition ( expression , **options )
1058
+
1059
+ "ADD #{ schema_creation . accept ( constraint ) } "
1060
+ end
1061
+
1062
+ def remove_exclusion_constraint_for_alter ( table_name , expression = nil , **options )
1063
+ excl_name_to_delete = exclusion_constraint_name ( table_name , expression : expression , **options )
1064
+
1065
+ remove_constraint_for_alter ( table_name , excl_name_to_delete , **options )
1066
+ end
1067
+
1068
+ def add_unique_constraint_for_alter ( table_name , column_name , options = { } )
1069
+ options = unique_constraint_options ( table_name , column_name , options )
1070
+
1071
+ td = create_table_definition ( table_name )
1072
+ constraint = td . new_unique_constraint_definition ( column_name , options )
1073
+
1074
+ "ADD #{ schema_creation . accept ( constraint ) } "
1075
+ end
1076
+
1077
+ def remove_unique_constraint_for_alter ( table_name , column_name = nil , **options )
1078
+ unique_name_to_delete = unique_constraint_name ( table_name , column : column_name , **options )
1079
+
1080
+ remove_constraint_for_alter ( table_name , unique_name_to_delete , **options )
1081
+ end
1082
+
1013
1083
def add_index_opclass ( quoted_columns , **options )
1014
1084
opclasses = options_for_index_columns ( options [ :opclass ] )
1015
1085
quoted_columns . each do |name , column |
0 commit comments