@@ -78,6 +78,18 @@ if Code.ensure_loaded?(MyXQL) do
78
78
end
79
79
end
80
80
81
+ def to_constraints (
82
+ % MyXQL.Error { mysql: % { name: :ER_CHECK_CONSTRAINT_VIOLATED } , message: message } ,
83
+ _opts
84
+ ) do
85
+ with [ _ , quoted ] <- :binary . split ( message , [ "Check constraint " ] ) ,
86
+ [ _ , constraint | _ ] <- :binary . split ( quoted , @ quotes , [ :global ] ) do
87
+ [ check: constraint ]
88
+ else
89
+ _ -> [ ]
90
+ end
91
+ end
92
+
81
93
def to_constraints ( _ , _ ) ,
82
94
do: [ ]
83
95
@@ -1026,12 +1038,31 @@ if Code.ensure_loaded?(MyXQL) do
1026
1038
def execute_ddl ( { :create_if_not_exists , % Index { } } ) ,
1027
1039
do: error! ( nil , "MySQL adapter does not support create if not exists for index" )
1028
1040
1029
- def execute_ddl ( { :create , % Constraint { check: check } } ) when is_binary ( check ) ,
1030
- do: error! ( nil , "MySQL adapter does not support check constraints" )
1041
+ def execute_ddl ( { :create , % Constraint { check: check } = constraint } ) when is_binary ( check ) do
1042
+ table_name = quote_name ( constraint . prefix , constraint . table )
1043
+ [ [ "ALTER TABLE " , table_name , " ADD " , new_constraint_expr ( constraint ) ] ]
1044
+ end
1031
1045
1032
1046
def execute_ddl ( { :create , % Constraint { exclude: exclude } } ) when is_binary ( exclude ) ,
1033
1047
do: error! ( nil , "MySQL adapter does not support exclusion constraints" )
1034
1048
1049
+ def execute_ddl ( { :drop , % Constraint { } , :cascade } ) ,
1050
+ do: error! ( nil , "MySQL does not support `CASCADE` in `DROP CONSTRAINT` commands" )
1051
+
1052
+ def execute_ddl ( { :drop , % Constraint { } = constraint , _ } ) do
1053
+ [
1054
+ [
1055
+ "ALTER TABLE " ,
1056
+ quote_name ( constraint . prefix , constraint . table ) ,
1057
+ " DROP CONSTRAINT " ,
1058
+ quote_name ( constraint . name )
1059
+ ]
1060
+ ]
1061
+ end
1062
+
1063
+ def execute_ddl ( { :drop_if_exists , % Constraint { } , _ } ) ,
1064
+ do: error! ( nil , "MySQL adapter does not support `drop_if_exists` for constraints" )
1065
+
1035
1066
def execute_ddl ( { :drop , % Index { } , :cascade } ) ,
1036
1067
do: error! ( nil , "MySQL adapter does not support cascade in drop index" )
1037
1068
@@ -1047,12 +1078,6 @@ if Code.ensure_loaded?(MyXQL) do
1047
1078
]
1048
1079
end
1049
1080
1050
- def execute_ddl ( { :drop , % Constraint { } , _ } ) ,
1051
- do: error! ( nil , "MySQL adapter does not support constraints" )
1052
-
1053
- def execute_ddl ( { :drop_if_exists , % Constraint { } , _ } ) ,
1054
- do: error! ( nil , "MySQL adapter does not support constraints" )
1055
-
1056
1081
def execute_ddl ( { :drop_if_exists , % Index { } , _ } ) ,
1057
1082
do: error! ( nil , "MySQL adapter does not support drop if exists for index" )
1058
1083
@@ -1244,6 +1269,17 @@ if Code.ensure_loaded?(MyXQL) do
1244
1269
defp null_expr ( true ) , do: " NULL"
1245
1270
defp null_expr ( _ ) , do: [ ]
1246
1271
1272
+ defp new_constraint_expr ( % Constraint { check: check } = constraint ) when is_binary ( check ) do
1273
+ [
1274
+ "CONSTRAINT " ,
1275
+ quote_name ( constraint . name ) ,
1276
+ " CHECK (" ,
1277
+ check ,
1278
+ ")" ,
1279
+ validate ( constraint . validate )
1280
+ ]
1281
+ end
1282
+
1247
1283
defp default_expr ( { :ok , nil } ) ,
1248
1284
do: " DEFAULT NULL"
1249
1285
@@ -1401,6 +1437,9 @@ if Code.ensure_loaded?(MyXQL) do
1401
1437
defp reference_on_update ( :restrict ) , do: " ON UPDATE RESTRICT"
1402
1438
defp reference_on_update ( _ ) , do: [ ]
1403
1439
1440
+ defp validate ( false ) , do: " NOT ENFORCED"
1441
+ defp validate ( _ ) , do: [ ]
1442
+
1404
1443
## Helpers
1405
1444
1406
1445
defp get_source ( query , sources , ix , source ) do
@@ -1423,6 +1462,10 @@ if Code.ensure_loaded?(MyXQL) do
1423
1462
1424
1463
defp maybe_add_column_names ( _ , name ) , do: name
1425
1464
1465
+ defp quote_name ( nil , name ) , do: quote_name ( name )
1466
+
1467
+ defp quote_name ( prefix , name ) , do: [ quote_name ( prefix ) , ?. , quote_name ( name ) ]
1468
+
1426
1469
defp quote_name ( name ) when is_atom ( name ) do
1427
1470
quote_name ( Atom . to_string ( name ) )
1428
1471
end
0 commit comments