Postgresql compatibility for agent model

This commit is contained in:
ms32035 2014-03-11 22:55:32 +01:00
parent 68437b9e0a
commit b0d64c4735
2 changed files with 16 additions and 1 deletions

View file

@ -10,6 +10,7 @@ class Agent < ActiveRecord::Base
include AssignableTypes
include MarkdownClassAttributes
include JSONSerializedField
include RDBMSFunctions
markdown_class_attributes :description, :event_description
@ -127,7 +128,8 @@ class Agent < ActiveRecord::Base
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 = 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

13
lib/rdbms_functions.rb Normal file
View file

@ -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
"DATE_ADD(`#{source}`, INTERVAL #{unit} #{AMOUNT})"
when :postgresql
"(#{source} + INTERVAL '#{amount} #{unit}')"
else
raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
end
end
end