From a0f037baab282c0cb3f02e07e6ee1df85711e523 Mon Sep 17 00:00:00 2001 From: albert Date: Mon, 8 Apr 2013 14:33:58 -0400 Subject: [PATCH] add fix for deleted post parents --- script/fixes/012.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 script/fixes/012.rb diff --git a/script/fixes/012.rb b/script/fixes/012.rb new file mode 100644 index 000000000..9d6d00f2b --- /dev/null +++ b/script/fixes/012.rb @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) + +ActiveRecord::Base.connection.execute("set statement_timeout = 0") + +Post.where("is_deleted = true").find_each do |post| + parent_id = ActiveRecord::Base.connection.select_value("select parent_id from post_versions where post_id = #{post.id} and parent_id is not null order by updated_at desc limit 1") + if parent_id + post.update_column(:parent_id, parent_id) + ActiveRecord::Base.connection.execute("update posts set has_children = true where id = #{parent_id}") + end +end