Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Timestamps are assigned to pg_search_documents in multisearch rebuild #29

Closed
wants to merge 1 commit into from

2 participants

@bartonmcguire

Rails' timestamps migration helper now defaults to :null => false. This previously caused the multisearch rebuild rake tast to throw a not-null violation as it hasn't been assigning timestamps.

This simply adds the assignment of these timestamps, as the current time, to the sql injection statement in the rake task.

Alternative solution could be to amend the migration to create null-friendly timestamp columns, however assigning timestamps from the beginning seems the better way to go.

Relates to this issue:
#27

@nertzy

Better to use Time.now.to_s(:db) here, I think?

@nertzy

It would be better if the Time.now stub returned a Time object instead of a string.

@nertzy
Owner

Thanks! I left a couple comments on your commit. Could you update it?

@nertzy
Owner

I merged your solution in: d0c8949
and made some edits: 56fe0fb

Thanks!

@nertzy nertzy closed this
@bartonmcguire

Edits look good. Thanks for the merge.

Overall kudos for what's been a very useful gem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 3 deletions.
  1. +5 −1 lib/pg_search/multisearch.rb
  2. +10 −2 spec/pg_search/multisearch_spec.rb
View
6 lib/pg_search/multisearch.rb
@@ -1,9 +1,11 @@
module PgSearch
module Multisearch
REBUILD_SQL_TEMPLATE = <<-SQL
-INSERT INTO :documents_table (searchable_type, searchable_id, content)
+INSERT INTO :documents_table (searchable_type, searchable_id, created_at, updated_at, content)
SELECT :model_name AS searchable_type,
:model_table.id AS searchable_id,
+ ':current_time' AS created_at,
+ ':current_time' AS updated_at,
(
:content_expressions
) AS content
@@ -41,6 +43,8 @@ def rebuild_sql(model)
":model_table", model.quoted_table_name
).gsub(
":documents_table", PgSearch::Document.quoted_table_name
+ ).gsub(
+ ":current_time", Time.now.to_s
)
end
end
View
12 spec/pg_search/multisearch_spec.rb
@@ -20,6 +20,10 @@
end
describe ".rebuild_sql" do
+ before do
+ @time = "2012-03-30 12:52:03 -0700"
+ Time.stub(:now){@time}
+ end
context "with one attribute" do
it "should generate the proper SQL code" do
model = MultisearchableModel
@@ -28,9 +32,11 @@
model.multisearchable :against => :title
expected_sql = <<-SQL
-INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content)
+INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, created_at, updated_at, content)
SELECT #{connection.quote(model.name)} AS searchable_type,
#{model.quoted_table_name}.id AS searchable_id,
+ '#{@time}' AS created_at,
+ '#{@time}' AS updated_at,
(
coalesce(#{model.quoted_table_name}.title, '')
) AS content
@@ -49,9 +55,11 @@
model.multisearchable :against => [:title, :content]
expected_sql = <<-SQL
-INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, content)
+INSERT INTO #{PgSearch::Document.quoted_table_name} (searchable_type, searchable_id, created_at, updated_at, content)
SELECT #{connection.quote(model.name)} AS searchable_type,
#{model.quoted_table_name}.id AS searchable_id,
+ '#{@time}' AS created_at,
+ '#{@time}' AS updated_at,
(
coalesce(#{model.quoted_table_name}.title, '') || ' ' || coalesce(#{model.quoted_table_name}.content, '')
) AS content
Something went wrong with that request. Please try again.