From b0d64c47355baa5939b585cccae74fd9b1aa3960 Mon Sep 17 00:00:00 2001 From: ms32035 Date: Tue, 11 Mar 2014 22:55:32 +0100 Subject: [PATCH] Postgresql compatibility for agent model --- app/models/agent.rb | 4 +++- lib/rdbms_functions.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 lib/rdbms_functions.rb diff --git a/app/models/agent.rb b/app/models/agent.rb index 6a950a34..d5a0ed92 100644 --- a/app/models/agent.rb +++ b/app/models/agent.rb @@ -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 diff --git a/lib/rdbms_functions.rb b/lib/rdbms_functions.rb new file mode 100644 index 00000000..a2b1c6e3 --- /dev/null +++ b/lib/rdbms_functions.rb @@ -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