diaspora/db/migrate/20160302025129_cleanup_aspect_visibility.rb
Benjamin Neff 54ff7ee278 migration to cleanup aspect_visibilities
* remove all for public posts
* remove all for deleted posts
2016-03-04 20:13:52 +01:00

27 lines
1.2 KiB
Ruby

class CleanupAspectVisibility < ActiveRecord::Migration
class AspectVisibility < ActiveRecord::Base
end
def up
AspectVisibility.joins("LEFT OUTER JOIN posts ON posts.id = aspect_visibilities.shareable_id")
.where(shareable_type: "Post").delete_all("posts.id is NULL")
AspectVisibility.joins("LEFT OUTER JOIN photos ON photos.id = aspect_visibilities.shareable_id")
.where(shareable_type: "Photo").delete_all("photos.id is NULL")
AspectVisibility.joins("INNER JOIN posts ON posts.id = aspect_visibilities.shareable_id")
.where(shareable_type: "Post").delete_all(posts: {public: true})
AspectVisibility.joins("INNER JOIN photos ON photos.id = aspect_visibilities.shareable_id")
.where(shareable_type: "Photo").delete_all(photos: {public: true})
remove_columns :aspect_visibilities, :created_at, :updated_at
end
def down
add_column :aspect_visibilities, :created_at, :datetime
add_column :aspect_visibilities, :updated_at, :datetime
User.all.each do |user|
user.posts.where(public: true).each {|post| user.add_to_streams(post, user.aspects) }
user.photos.where(public: true).each {|photo| user.add_to_streams(photo, user.aspects) }
end
end
end