Add support for partial indices to PostgreSQL adapter #4956

Merged
merged 3 commits into from Feb 10, 2012

9 participants

@mhfs

Add a where option to add_index in order to support PostgreSQL partial indices.

add_index(:accounts, :code, :where => "active")

... generates ...

CREATE INDEX index_accounts_on_code ON accounts(code) WHERE active

Also made SchemaDumper recognize the where statements in the indices and dump them to schema.rb.

This is my first contribution to AR so please let me know if I missed something.

mhfs added some commits Feb 9, 2012
@mhfs mhfs Added where option to add_index to support postgresql partial indices
The `add_index` method now supports a `where` option that receives a
string with the partial index criteria.

    add_index(:accounts, :code, :where => "active")

    Generates

    CREATE INDEX index_accounts_on_code ON accounts(code) WHERE active
d70e023
@mhfs mhfs Made schema dumper recognize partial indices' where statements 7ef22fc
@thiagopradi

+1, I have been writing raw SQL migrations to use this feature. Would be great to have in the core.

@gustavowt

+1, really useful feature.

@tenderlove tenderlove commented on an outdated diff Feb 9, 2012
activerecord/test/cases/migration/index_test.rb
@@ -169,6 +169,12 @@ def test_add_index
connection.add_index("testings", ["last_name", "first_name"], :order => :desc)
connection.remove_index("testings", ["last_name", "first_name"])
end
+
+ # Selected adapters support partial indices
+ if current_adapter?(:PostgreSQLAdapter)
+ connection.add_index("testings", ["last_name"], :where => "first_name = 'john doe'")
+ connection.remove_index("testings", ["last_name"])
@tenderlove
Ruby on Rails member

Can you add an assertion that the index actually exists? The test will pass with empty implementations of add_index and remove_index.

Also please put it in it's own test method with a skip 'only on pg' unless current_adapter?(:PostgreSQLAdapter).

@tenderlove
Ruby on Rails member

Also make sure to assert that the index does not exist after the call to remove_index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tenderlove
Ruby on Rails member

Looks good! Can you fix my one commented section? Then I'll merge. Thanks!

@mhfs

You want it you got it. =D

Thanks for the feedback.

@tenderlove tenderlove merged commit 3092324 into rails:master Feb 10, 2012
@mhfs

@tenderlove would you allow a backport of this into 3.2?

@baburdick

+1 for the backport to Rails 3!

@will

Great work, thanks @mhfs!

@goosetav

FYI - we just started using TMXCredit/pg_power to provide backported partial index support. The partial index code is based on this pull request

It's working as expected so far.

@nberger nberger added a commit to nberger/rails that referenced this pull request Jul 15, 2013
@mhfs mhfs Backport #4956 to 3-2-stable. Postgresql partial indices
Added where option to add_index to support postgresql partial indices

The `add_index` method now supports a `where` option that receives a
string with the partial index criteria.

    add_index(:accounts, :code, :where => "active")

    Generates

    CREATE INDEX index_accounts_on_code ON accounts(code) WHERE active

Made schema dumper recognize partial indices' where statements
b36461b
@tute

Is this in 3.2.14?

@dmathieu

No. This is only in 4.0.0. This is a feature, so it won't be backported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment