From c34a4b2e54ad091e0e98d195c0a9bf70da47b91f Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Mon, 30 Jan 2012 12:27:05 +0000 Subject: Added possibility of using postgres databases. --- lib/mauve/alert.rb | 22 +++++++++++++++------- lib/mauve/datamapper.rb | 8 +++++++- lib/mauve/server.rb | 5 ++--- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 37a8f2e..3c9fbdc 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -14,7 +14,6 @@ module Mauve include DataMapper::Resource - property :id, Serial property :alert_id, Integer property :earliest, EpochTime belongs_to :alert, :model => "Alert" @@ -29,6 +28,16 @@ module Mauve # def self.create_view! the_distant_future = (Time.now + 2000.days).to_i # it is the year 2000 - the humans are dead + + case DataMapper.repository(:default).adapter.class.to_s + when "DataMapper::Adapters::PostgresAdapter" + ifnull = "COALESCE" + min = "LEAST" + else + ifnull = "IFNULL" + min = "MIN" + end + ["BEGIN TRANSACTION", "DROP VIEW IF EXISTS mauve_alert_earliest_dates", "CREATE VIEW @@ -37,12 +46,12 @@ module Mauve SELECT id AS alert_id, NULLIF( - MIN( - IFNULL(will_clear_at, '#{the_distant_future}'), - IFNULL(will_raise_at, '#{the_distant_future}'), - IFNULL(will_unacknowledge_at, '#{the_distant_future}') + #{min}( + #{ifnull}(will_clear_at, #{the_distant_future}), + #{ifnull}(will_raise_at, #{the_distant_future}), + #{ifnull}(will_unacknowledge_at, #{the_distant_future}) ), - '#{the_distant_future}' + #{the_distant_future} ) AS earliest FROM mauve_alerts WHERE @@ -54,7 +63,6 @@ module Mauve repository(:default).adapter.execute(statement.gsub(/\s+/, " ")) end end - end # diff --git a/lib/mauve/datamapper.rb b/lib/mauve/datamapper.rb index abf56c7..e7334da 100644 --- a/lib/mauve/datamapper.rb +++ b/lib/mauve/datamapper.rb @@ -6,7 +6,13 @@ require 'dm-core' require 'dm-migrations' require 'dm-serializer' -require 'dm-sqlite-adapter-with-mutex' +%w(dm-sqlite-adapter-with-mutex dm-postgres-adapter).each do |req| + begin + require req + rescue LoadError => err + # do not a lot. + end +end require 'dm-types' require 'dm-validations' diff --git a/lib/mauve/server.rb b/lib/mauve/server.rb index 233695d..f07b7ad 100644 --- a/lib/mauve/server.rb +++ b/lib/mauve/server.rb @@ -1,7 +1,7 @@ # encoding: UTF-8 require 'yaml' require 'socket' -# require 'mauve/datamapper' +require 'mauve/datamapper' require 'mauve/proto' require 'mauve/alert' require 'mauve/history' @@ -118,7 +118,6 @@ module Mauve # # m.auto_migrate! if m.respond_to?("auto_migrate!") # - # m.properties.each do |prop| next unless prop.is_a?(DataMapper::Property::EpochTime) logger.info("Updating #{c}.#{prop.name}") @@ -126,7 +125,7 @@ module Mauve DataMapper.repository(:default).adapter.execute("BEGIN TRANSACTION;") DataMapper.repository(:default).adapter.execute(statement) DataMapper.repository(:default).adapter.execute("COMMIT TRANSACTION;") - end + end if DataMapper.repository(:default).adapter.class.to_s == "DataMapper::Adapters::SqliteAdapter" end AlertHistory.migrate! -- cgit v1.2.1