Permalink
Browse files

fixes #2264 - replace fulltext sources.value index with digest

  • Loading branch information...
1 parent 5f4a100 commit bf38fd07f855b2018916a4d1cbe850d65dca7ba3 Dominic Cleal committed with ohadlevy Feb 28, 2013
Showing with 29 additions and 2 deletions.
  1. +1 −1 app/models/report.rb
  2. +6 −1 app/models/source.rb
  3. +22 −0 db/migrate/20130228145456_add_digest_to_sources.rb
View
@@ -209,7 +209,7 @@ def import_log_messages report
# skipping catalog summary run messages, we dont want them in our db too
next if r.message =~ /^Finished catalog run in \d+.\d+ seconds$/
message = Message.find_or_create r.message
- source = Source.find_or_create_by_value r.source
+ source = Source.find_or_create r.source
log = Log.create :message_id => message.id, :source_id => source.id, :report_id => self.id, :level => r.level
log.errors.empty?
end
View
@@ -1,7 +1,7 @@
class Source < ActiveRecord::Base
has_many :reports, :through => :logs
has_many :logs
- validates_presence_of :value
+ validates_presence_of :value, :digest
def to_s
value
@@ -10,4 +10,9 @@ def to_s
def as_json(options={})
{:source => value }
end
+
+ def self.find_or_create val
+ digest = Digest::SHA1.hexdigest(val)
+ Source.where(:digest => digest).first || Source.create(:value => val, :digest => digest)
+ end
end
@@ -0,0 +1,22 @@
+class AddDigestToSources < ActiveRecord::Migration
+ def self.up
+ if ["mysql", "mysql2"].include? ActiveRecord::Base.connection.instance_values["config"][:adapter]
+ execute "DROP INDEX value ON sources" if index_exists?(:sources, :value, :name => 'value')
+ else
+ remove_index(:sources, :value) if index_exists?(:sources, :value)
+ end
+ add_column :sources, :digest, :string
+ Source.find_each {|m| m.update_attribute(:digest, Digest::SHA1.hexdigest(m.value)) }
+ add_index :sources, :digest
+ end
+
+ def self.down
+ remove_index :sources, :digest
+ remove_column :sources, :digest
+ if ["mysql", "mysql2"].include? ActiveRecord::Base.connection.instance_values["config"][:adapter]
+ execute "ALTER TABLE sources ADD FULLTEXT (value)"
+ else
+ add_index :sources, :value
+ end
+ end
+end

0 comments on commit bf38fd0

Please sign in to comment.