Skip to content

Commit

Permalink
Pass validate(_check)_constraint through change_table
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer committed May 1, 2024
1 parent 20444dd commit 5c5e8d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
* `validate_constraint` can be called in a `change_table` block.

ex:
```ruby
change_table :products do |t|
t.check_constraint "price > discounted_price", name: "price_check", validate: false
t.validate_check_constraint "price_check"
end
```

*Cody Cutrer*

* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string.

Ex:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,26 @@ def unique_constraint(*args)
def remove_unique_constraint(*args)
@base.remove_unique_constraint(name, *args)
end

# Validates the given constraint on the table.
#
# t.check_constraint("price > 0", name: "price_check", validate: false)
# t.validate_constraint "price_check"
#
# See {connection.validate_constraint}[rdoc-ref:SchemaStatements#validate_constraint]
def validate_constraint(*args)
@base.validate_constraint(name, *args)
end

# Validates the given check constraint on the table
#
# t.check_constraint("price > 0", name: "price_check", validate: false)
# t.validate_check_constraint name: "price_check"
#
# See {connection.validate_check_constraint}[rdoc-ref:SchemaStatements#validate_check_constraint]
def validate_check_constraint(*args)
@base.validate_check_constraint(name, *args)
end
end

# = Active Record PostgreSQL Adapter Alter \Table
Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/migration/change_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ def test_remove_check_constraint_removes_check_constraint
end
end

if current_adapter?(:PostgreSQLAdapter)
def test_validate_check_constraint
with_change_table do |t|
expect :add_check_constraint, nil, [:delete_me, "price > discounted_price"], name: "price_check", validate: false
t.check_constraint "price > discounted_price", name: "price_check", validate: false
expect :validate_check_constraint, :nil, [:delete_me, "price_check"]
t.validate_check_constraint "price_check"
end
end

def test_validate_constraint
with_change_table do |t|
expect :add_check_constraint, nil, [:delete_me, "price > discounted_price"], name: "price_check", validate: false
t.check_constraint "price > discounted_price", name: "price_check", validate: false
expect :validate_constraint, :nil, [:delete_me, "price_check"]
t.validate_constraint "price_check"
end
end
end

def test_remove_column_with_if_exists_raises_error
assert_raises(ArgumentError) do
with_change_table do |t|
Expand Down

0 comments on commit 5c5e8d2

Please sign in to comment.