diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-18 10:47:50 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-18 10:47:50 +0100 |
commit | c30daf56d4aab9e41a993d82406cba947c8efe32 (patch) | |
tree | ad24bf816ea1ed75fccee138475c51da3c78439b /lib/mauve | |
parent | 992aec20269d7154016b71edf6b01e02be3328ca (diff) |
Added migration to history table.
Diffstat (limited to 'lib/mauve')
-rw-r--r-- | lib/mauve/history.rb | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/lib/mauve/history.rb b/lib/mauve/history.rb index 096e648..29ee64c 100644 --- a/lib/mauve/history.rb +++ b/lib/mauve/history.rb @@ -11,6 +11,35 @@ module Mauve belongs_to :alert belongs_to :history + + def self.migrate! + # + # This copies the alert IDs from the old History table to the new AlertHistories thing, but only if there are no AertHistories + # and some Histories + # + if AlertHistory.last.nil? and not History.last.nil? + # + # This is horrid. FIXME! + # + history_schema = '"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "type" VARCHAR(50) DEFAULT \'unknown\' NOT NULL, "event" TEXT DEFAULT \'Nothing set\' NOT NULL, "created_at" TIMESTAMP NOT NULL' + history_cols = 'id, type, event, created_at' + ## + # Now adjust the Histories table to remove its alert_id col + # + ["BEGIN TRANSACTION;", + "INSERT INTO mauve_alert_histories (alert_id, history_id) SELECT alert_id, id FROM mauve_histories;", + "CREATE TEMPORARY TABLE mauve_histories_backup( #{history_schema} );", + "INSERT INTO mauve_histories_backup SELECT #{history_cols} FROM mauve_histories;", + "DROP TABLE mauve_histories;", + "CREATE TABLE mauve_histories( #{history_schema} );", + "INSERT INTO mauve_histories SELECT #{history_cols} FROM mauve_histories_backup;", + "DROP TABLE mauve_histories_backup;", + "COMMIT;"].each do |statement| + repository(:default).adapter.execute(statement) + end + end + end + end class History @@ -18,9 +47,11 @@ module Mauve # so .first always returns the most recent update default_scope(:default).update(:order => [:created_at.desc, :id.desc]) - + + # + # If these properties change.. then the migration above will break horribly. FIXME. + # property :id, Serial -# property :alert_id, String, :required => true property :type, String, :required => true, :default => "unknown" property :event, Text, :required => true, :default => "Nothing set" property :created_at, Time, :required => true @@ -29,6 +60,17 @@ module Mauve before :valid?, :set_created_at + def self.migrate! + ## + # + # FIXME this is dire. + # + schema = repository(:default).adapter.execute(".schema mauve_histories") + + + + end + def set_created_at(context = :default) self.created_at = Time.now unless self.created_at.is_a?(Time) or self.created_at.is_a?(DateTime) end |