Skip to content

Commit

Permalink
Cache deletion times on Post
Browse files Browse the repository at this point in the history
Closes #96
  • Loading branch information
Undo1 committed Feb 17, 2017
1 parent 8956222 commit f77c98d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/models/deletion_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ class DeletionLog < ApplicationRecord
ActionCable.server.broadcast "api_deletion_logs", { deletion_log: JSON.parse(DeletionLogsController.render(locals: {deletion_log: self}, partial: 'deletion_log.json')) }
end
end

after_create do
if is_deleted and post.deleted_at.nil?
post.update(:deleted_at => created_at)
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170217051750_add_deleted_at_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDeletedAtToPosts < ActiveRecord::Migration[5.0]
def change
add_column :posts, :deleted_at, :datetime
end
end
12 changes: 12 additions & 0 deletions db/migrate/20170217064146_migrate_to_cached_deletion_times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MigrateToCachedDeletionTimes < ActiveRecord::Migration[5.0]
def change
sql = <<-SQL
UPDATE `posts`
INNER JOIN
(SELECT `deletion_logs`.`post_id`, min(`deletion_logs`.`created_at`) as min_dl FROM `deletion_logs` WHERE `is_deleted` = true GROUP BY `deletion_logs`.`post_id`) dl on dl.post_id = posts.id
SET `posts`.`deleted_at` = dl.min_dl
SQL

ActiveRecord::Base.connection.execute(sql)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170215003244) do
ActiveRecord::Schema.define(version: 20170217064146) do

create_table "api_keys", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci" do |t|
t.datetime "created_at", null: false
Expand Down Expand Up @@ -188,6 +188,7 @@
t.boolean "is_fp", default: false
t.boolean "is_naa", default: false
t.integer "revision_count"
t.datetime "deleted_at"
t.index ["created_at"], name: "index_posts_on_created_at", using: :btree
t.index ["link"], name: "index_posts_on_link", length: { link: 191 }, using: :btree
end
Expand Down

0 comments on commit f77c98d

Please sign in to comment.