Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20110527135552_photo_status_message_association_on_guid.rb « migrate « db - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19f7105e32c62f70e8cbbf15306d9f8a8987e1a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class PhotoStatusMessageAssociationOnGuid < ActiveRecord::Migration
  class Post < ActiveRecord::Base
    attr_accessible :id, :guid, :status_message_id, :status_message_guid
    self.inheritance_column = :_type_disabled
  end

  def self.up
    add_column :posts, :status_message_guid, :string

    ActiveRecord::Base.record_timestamps = false
    photos = Post.where(Post.arel_table[:status_message_id].not_eq(nil).and(Post.arel_table[:type].eq('Photo')))
    photos.each do |photo|
      if Post.where(:id => photo.status_message_id).exists?
        status_message = Post.find(photo.status_message_id)
        photo.update_attributes(:status_message_guid => status_message.guid)
      end
    end

    remove_index :posts, [:status_message_id]
    remove_index :posts, [:status_message_id, :pending]
    add_index :posts, :status_message_guid
    add_index :posts, [:status_message_guid, :pending]

    remove_column :posts, :status_message_id
  end

  def self.down
    add_column :posts, :status_message_id, :integer

    ActiveRecord::Base.record_timestamps = false
    photos = Post.where(Post.arel_table[:status_message_guid].not_eq(nil).and(Post.arel_table[:type].eq('Photo')))
    photos.each do |photo|
      if Post.where(:guid => photo.status_message_guid).exists?
        status_message = Post.where(:guid => photo.status_message_guid).first
        photo.update_attributes(:status_message_id => status_message.id)
      end
    end

    remove_index :posts, [:status_message_guid, :pending]
    remove_index :posts, :status_message_guid
    add_index :posts, :status_message_id
    add_index :posts, [:status_message_id, :pending]

    remove_column :posts, :status_message_guid
  end
end