Skip to content

Commit

Permalink
Merge pull request huginn#184 from ms32035/master
Browse files Browse the repository at this point in the history
Postgresql compatibility for agent model
  • Loading branch information
cantino committed Mar 23, 2014
2 parents 691f3b7 + 686694c commit 0e7c5c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Agent < ActiveRecord::Base
include AssignableTypes
include MarkdownClassAttributes
include JSONSerializedField
include RDBMSFunctions

markdown_class_attributes :description, :event_description

Expand Down Expand Up @@ -127,7 +128,7 @@ def update_event_expirations!
if keep_events_for == 0
events.update_all :expires_at => nil
else
events.update_all "expires_at = DATE_ADD(`created_at`, INTERVAL #{keep_events_for.to_i} DAY)"
events.update_all "expires_at = " + rdbms_date_add("created_at", "DAY", keep_events_for.to_i)
end
end

Expand Down Expand Up @@ -265,8 +266,8 @@ def receive!(options={})

agents_to_events = {}
Agent.connection.select_rows(sql).each do |receiver_agent_id, source_agent_id, event_id|
agents_to_events[receiver_agent_id] ||= []
agents_to_events[receiver_agent_id] << event_id
agents_to_events[receiver_agent_id.to_i] ||= []
agents_to_events[receiver_agent_id.to_i] << event_id
end

event_ids = agents_to_events.values.flatten.uniq.compact
Expand Down
1 change: 1 addition & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test:
socket: <%= ENV['DATABASE_SOCKET'] || ["/var/run/mysqld/mysqld.sock", "/opt/local/var/run/mysql5/mysqld.sock", "/tmp/mysql.sock"].find{ |path| File.exist? path } %>
encoding: <%= ENV['DATABASE_ENCODING'] || "utf8" %>
reconnect: <%= ENV['DATABASE_RECONNECT'] || "true" %>
port: <%= ENV['DATABASE_PORT'] || "" %>
pool: <%= ENV['DATABASE_POOL'] || "5" %>

production:
Expand Down
13 changes: 13 additions & 0 deletions lib/rdbms_functions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module RDBMSFunctions
def rdbms_date_add(source, unit, amount)
adapter_type = connection.adapter_name.downcase.to_sym
case adapter_type
when :mysql, :mysql2
"DATE_ADD(`#{source}`, INTERVAL #{amount} #{unit})"
when :postgresql
"(#{source} + INTERVAL '#{amount} #{unit}')"
else
raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
end
end
end

0 comments on commit 0e7c5c5

Please sign in to comment.