Skip to content

Commit 5c5e8d2

Browse files
committed
Pass validate(_check)_constraint through change_table
1 parent 20444dd commit 5c5e8d2

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
* `validate_constraint` can be called in a `change_table` block.
2+
3+
ex:
4+
```ruby
5+
change_table :products do |t|
6+
t.check_constraint "price > discounted_price", name: "price_check", validate: false
7+
t.validate_check_constraint "price_check"
8+
end
9+
```
10+
11+
*Cody Cutrer*
12+
113
* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string.
214

315
Ex:

activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,26 @@ def unique_constraint(*args)
332332
def remove_unique_constraint(*args)
333333
@base.remove_unique_constraint(name, *args)
334334
end
335+
336+
# Validates the given constraint on the table.
337+
#
338+
# t.check_constraint("price > 0", name: "price_check", validate: false)
339+
# t.validate_constraint "price_check"
340+
#
341+
# See {connection.validate_constraint}[rdoc-ref:SchemaStatements#validate_constraint]
342+
def validate_constraint(*args)
343+
@base.validate_constraint(name, *args)
344+
end
345+
346+
# Validates the given check constraint on the table
347+
#
348+
# t.check_constraint("price > 0", name: "price_check", validate: false)
349+
# t.validate_check_constraint name: "price_check"
350+
#
351+
# See {connection.validate_check_constraint}[rdoc-ref:SchemaStatements#validate_check_constraint]
352+
def validate_check_constraint(*args)
353+
@base.validate_check_constraint(name, *args)
354+
end
335355
end
336356

337357
# = Active Record PostgreSQL Adapter Alter \Table

activerecord/test/cases/migration/change_table_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ def test_remove_check_constraint_removes_check_constraint
339339
end
340340
end
341341

342+
if current_adapter?(:PostgreSQLAdapter)
343+
def test_validate_check_constraint
344+
with_change_table do |t|
345+
expect :add_check_constraint, nil, [:delete_me, "price > discounted_price"], name: "price_check", validate: false
346+
t.check_constraint "price > discounted_price", name: "price_check", validate: false
347+
expect :validate_check_constraint, :nil, [:delete_me, "price_check"]
348+
t.validate_check_constraint "price_check"
349+
end
350+
end
351+
352+
def test_validate_constraint
353+
with_change_table do |t|
354+
expect :add_check_constraint, nil, [:delete_me, "price > discounted_price"], name: "price_check", validate: false
355+
t.check_constraint "price > discounted_price", name: "price_check", validate: false
356+
expect :validate_constraint, :nil, [:delete_me, "price_check"]
357+
t.validate_constraint "price_check"
358+
end
359+
end
360+
end
361+
342362
def test_remove_column_with_if_exists_raises_error
343363
assert_raises(ArgumentError) do
344364
with_change_table do |t|

0 commit comments

Comments
 (0)