From b3a9bf392591294f5d957e2da2162f2be3527dea Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 18 Jun 2016 07:03:03 +0200 Subject: [PATCH] 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 --- .../20160618033455_cleanup_participations.rb | 44 +++++++++++++++++++ db/schema.rb | 19 ++++---- 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20160618033455_cleanup_participations.rb diff --git a/db/migrate/20160618033455_cleanup_participations.rb b/db/migrate/20160618033455_cleanup_participations.rb new file mode 100644 index 000000000..ffe857885 --- /dev/null +++ b/db/migrate/20160618033455_cleanup_participations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 41c0ddf5c..88ef2d80f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -312,19 +312,18 @@ ActiveRecord::Schema.define(version: 20160531170531) do end create_table "participations", force: :cascade do |t| - t.string "guid", limit: 255 - 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 + t.string "guid", limit: 255 + t.integer "target_id", limit: 4 + t.string "target_type", limit: 60, null: false + t.integer "author_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "count", limit: 4, default: 1, null: false end 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