From b7e2ab759ffa8cb79716c1ca725c347ca04bb006 Mon Sep 17 00:00:00 2001 From: yutasb Date: Wed, 13 Mar 2024 21:27:51 +0900 Subject: [PATCH] Add index_predicate to README --- README.markdown | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.markdown b/README.markdown index 86b62f3b..4fa8fdec 100644 --- a/README.markdown +++ b/README.markdown @@ -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