add migration to cleanup participations

* remove author_signature column
* delete all self-participating participations
* delete all participations where author and post-author are remote
* delete all duplicate participations
* delete all participations without post
* create unique index
This commit is contained in:
Benjamin Neff 2016-06-18 07:03:03 +02:00
parent 70f378a1c1
commit b3a9bf3925
2 changed files with 53 additions and 10 deletions

View file

@ -0,0 +1,44 @@
class CleanupParticipations < ActiveRecord::Migration
class Participation < ActiveRecord::Base
end
def up
remove_column :participations, :author_signature
cleanup
remove_index :participations, name: :index_participations_on_target_id_and_target_type_and_author_id
add_index :participations, %i(target_id target_type author_id), unique: true
end
def down
remove_index :participations, name: :index_participations_on_target_id_and_target_type_and_author_id
add_index :participations, %i(target_id target_type author_id)
add_column :participations, :author_signature, :text
end
private
def cleanup
self_where = "WHERE participations.target_type = 'Post' AND participations.target_id = posts.id AND " \
"posts.author_id = participations.author_id"
remote_where = "WHERE participations.target_type = 'Post' AND participations.target_id = posts.id AND " \
"posts.author_id = post_author.id AND participations.author_id = author.id AND " \
"author.owner_id is NULL AND post_author.owner_id is NULL"
duplicate_where = "WHERE p1.author_id = p2.author_id AND p1.target_id = p2.target_id " \
"AND p1.target_type = p2.target_type AND p1.id > p2.id"
if AppConfig.postgres?
execute "DELETE FROM participations USING posts #{self_where}"
execute "DELETE FROM participations USING posts, people AS author, people AS post_author #{remote_where}"
execute "DELETE FROM participations AS p1 USING participations AS p2 #{duplicate_where}"
else
execute "DELETE participations FROM participations, posts #{self_where}"
execute "DELETE participations FROM participations, posts, people author, people post_author #{remote_where}"
execute "DELETE p1 FROM participations p1, participations p2 #{duplicate_where}"
end
Participation.joins("LEFT OUTER JOIN posts ON posts.id = participations.target_id")
.where(target_type: "Post").delete_all("posts.id is NULL")
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160531170531) do
ActiveRecord::Schema.define(version: 20160618033455) do
create_table "account_deletions", force: :cascade do |t|
t.string "diaspora_handle", limit: 255
@ -316,7 +316,6 @@ ActiveRecord::Schema.define(version: 20160531170531) do
t.integer "target_id", limit: 4
t.string "target_type", limit: 60, null: false
t.integer "author_id", limit: 4
t.text "author_signature", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "count", limit: 4, default: 1, null: false
@ -324,7 +323,7 @@ ActiveRecord::Schema.define(version: 20160531170531) do
add_index "participations", ["author_id"], name: "index_participations_on_author_id", using: :btree
add_index "participations", ["guid"], name: "index_participations_on_guid", length: {"guid"=>191}, using: :btree
add_index "participations", ["target_id", "target_type", "author_id"], name: "index_participations_on_target_id_and_target_type_and_author_id", using: :btree
add_index "participations", ["target_id", "target_type", "author_id"], name: "index_participations_on_target_id_and_target_type_and_author_id", unique: true, using: :btree
create_table "people", force: :cascade do |t|
t.string "guid", limit: 255, null: false