Skip to content

Commit

Permalink
If tidying, clear unchanged records.
Browse files Browse the repository at this point in the history
Anything that hasn’t been inserted or updated will have an ‘old’ timestamp - the record will be from a deleted model instance that wasn’t cleaned up correctly.
  • Loading branch information
pat committed Mar 13, 2021
1 parent ba3fd0f commit 08086ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/thinking_sphinx/real_time/populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.populate(index)

def initialize(index)
@index = index
@started_at = Time.current
end

def populate
Expand All @@ -17,12 +18,14 @@ def populate
instrument 'populated', :instances => instances
end

transcriber.clear_before(started_at) if configuration.settings["real_time_tidy"]

instrument 'finish_populating'
end

private

attr_reader :index
attr_reader :index, :started_at

delegate :controller, :batch_size, :to => :configuration
delegate :scope, :to => :index
Expand Down
6 changes: 6 additions & 0 deletions lib/thinking_sphinx/real_time/transcriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ def initialize(index)
@index = index
end

def clear_before(time)
execute <<~SQL.strip
DELETE FROM #{@index.name} WHERE sphinx_updated_at < #{time.to_i}
SQL
end

def copy(*instances)
items = instances.select { |instance|
instance.persisted? && copy?(instance)
Expand Down

0 comments on commit 08086ff

Please sign in to comment.