Skip to content

Commit

Permalink
Merge pull request #832 from yutasb/readme-index-predicate
Browse files Browse the repository at this point in the history
Add index_predicate example to README
  • Loading branch information
jkowens authored Mar 13, 2024
2 parents cfab0ae + b7e2ab7 commit 35c335b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ book.reload.title # => "Book1" (stayed the same)
book.reload.author # => "Bob Barker" (changed)
```

PostgreSQL Using partial indexes

```ruby
book = Book.create! title: "Book1", author: "George Orwell", published_at: Time.now
book.author = "Bob Barker"

# in migration
execute <<-SQL
CREATE INDEX books_published_at_index ON books (published_at) WHERE published_at IS NOT NULL;
SQL

# PostgreSQL version
Book.import [book], on_duplicate_key_update: {
conflict_target: [:id],
index_predicate: "published_at IS NOT NULL",
columns: [:author]
}

book.reload.title # => "Book1" (stayed the same)
book.reload.author # => "Bob Barker" (changed)
book.reload.published_at # => 2017-10-09 (stayed the same)
```

PostgreSQL Using constraints

```ruby
Expand Down

0 comments on commit 35c335b

Please sign in to comment.