20100804112053_merge_wiki_versions_with_journals.rb 2.79 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2012 the ChiliProject Team
6
#
7 8 9 10
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
11
#
12 13 14
# See doc/COPYRIGHT.rdoc for more details.
#++

15
class MergeWikiVersionsWithJournals < ActiveRecord::Migration
16 17 18
  # This is provided here for migrating up after the WikiContent::Version class has been removed
  class WikiContent < ActiveRecord::Base
    class Version < ActiveRecord::Base
19
    end
20
  end
21

22
  def self.up
23 24 25 26 27 28
    # avoid touching WikiContent on journal creation
    WikiContentJournal.class_exec {
      def touch_journaled_after_creation
      end
    }

29 30 31 32 33
    # assign all wiki_contents w/o author to the anonymous user - they used to
    # work w/o author but don't any more.
    WikiContent.update_all({:author_id => User.anonymous.id}, :author_id => nil)
    WikiContent::Version.update_all({:author_id => User.anonymous.id}, :author_id => nil)

34 35
    WikiContent::Version.find_by_sql("SELECT * FROM wiki_content_versions").each do |wv|
      journal = WikiContentJournal.create!(:journaled_id => wv.wiki_content_id, :user_id => wv.author_id,
36
        :notes => wv.comments, :created_at => wv.updated_on, :activity_type => "wiki_edits")
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      changes = {}
      changes["compression"] = wv.compression
      changes["data"] = wv.data
      journal.update_attribute(:changes, changes.to_yaml)
      journal.update_attribute(:version, wv.version)
    end
    # drop_table :wiki_content_versions

    change_table :wiki_contents do |t|
      t.rename :version, :lock_version
    end
  end

  def self.down
    change_table :wiki_contents do |t|
      t.rename :lock_version, :version
    end

    # create_table :wiki_content_versions do |t|
    #   t.column :wiki_content_id, :integer, :null => false
    #   t.column :page_id, :integer, :null => false
    #   t.column :author_id, :integer
    #   t.column :data, :binary
    #   t.column :compression, :string, :limit => 6, :default => ""
    #   t.column :comments, :string, :limit => 255, :default => ""
    #   t.column :updated_on, :datetime, :null => false
    #   t.column :version, :integer, :null => false
    # end
    # add_index :wiki_content_versions, :wiki_content_id, :name => :wiki_content_versions_wcid
    #
    # WikiContentJournal.all.each do |j|
    #   WikiContent::Version.create(:wiki_content_id => j.journaled_id, :page_id => j.journaled.page_id,
    #     :author_id => j.user_id, :data => j.changes["data"], :compression => j.changes["compression"],
    #     :comments => j.notes, :updated_on => j.created_at, :version => j.version)
    # end

    WikiContentJournal.destroy_all
  end
end