Skip to content

Commit bc48843

Browse files
authored
Merge pull request rails#41788 from pbstriker38/check_table_name_constraint_mariadb
Add support for check_constraints with the same name on different tables
2 parents 564a26d + 010a4a6 commit bc48843

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def check_constraints(table_name)
427427
if supports_check_constraints?
428428
scope = quoted_scope(table_name)
429429

430-
chk_info = exec_query(<<~SQL, "SCHEMA")
430+
sql = <<~SQL
431431
SELECT cc.constraint_name AS 'name',
432432
cc.check_clause AS 'expression'
433433
FROM information_schema.check_constraints cc
@@ -437,6 +437,9 @@ def check_constraints(table_name)
437437
AND tc.table_name = #{scope[:name]}
438438
AND cc.constraint_schema = #{scope[:schema]}
439439
SQL
440+
sql += " AND cc.table_name = #{scope[:name]}" if mariadb?
441+
442+
chk_info = exec_query(sql, "SCHEMA")
440443

441444
chk_info.map do |row|
442445
options = {

activerecord/test/cases/helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def supports_default_expression?
5454
end
5555
end
5656

57+
def supports_non_unique_constraint_name?
58+
if current_adapter?(:Mysql2Adapter)
59+
conn = ActiveRecord::Base.connection
60+
conn.mariadb?
61+
else
62+
false
63+
end
64+
end
65+
5766
%w[
5867
supports_savepoints?
5968
supports_partial_index?

activerecord/test/cases/migration/check_constraint_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ class Trade < ActiveRecord::Base
1818
t.integer :price
1919
t.integer :quantity
2020
end
21+
22+
@connection.create_table "purchases", force: true do |t|
23+
t.integer :price
24+
t.integer :quantity
25+
end
2126
end
2227

2328
teardown do
2429
@connection.drop_table "trades", if_exists: true rescue nil
30+
@connection.drop_table "purchases", if_exists: true rescue nil
2531
end
2632

2733
def test_check_constraints
@@ -56,6 +62,25 @@ def test_add_check_constraint
5662
end
5763
end
5864

65+
if supports_non_unique_constraint_name?
66+
def test_add_constraint_with_same_name_to_different_table
67+
@connection.add_check_constraint :trades, "quantity > 0", name: "greater_than_zero"
68+
@connection.add_check_constraint :purchases, "quantity > 0", name: "greater_than_zero"
69+
70+
trades_check_constraints = @connection.check_constraints("trades")
71+
assert_equal 1, trades_check_constraints.size
72+
trade_constraint = trades_check_constraints.first
73+
assert_equal "trades", trade_constraint.table_name
74+
assert_equal "greater_than_zero", trade_constraint.name
75+
76+
purchases_check_constraints = @connection.check_constraints("purchases")
77+
assert_equal 1, purchases_check_constraints.size
78+
purchase_constraint = purchases_check_constraints.first
79+
assert_equal "purchases", purchase_constraint.table_name
80+
assert_equal "greater_than_zero", purchase_constraint.name
81+
end
82+
end
83+
5984
def test_add_check_constraint_with_non_existent_table_raises
6085
e = assert_raises(ActiveRecord::StatementInvalid) do
6186
@connection.add_check_constraint :refunds, "quantity > 0", name: "quantity_check"

0 commit comments

Comments
 (0)