v0.2 Address where due to import issues there are some stray PostCustomField records for Posts that do not exist.
This commit is contained in:
parent
a85d33d1e3
commit
e18a7602a8
@ -1,7 +1,7 @@
|
||||
# Federated Computer, Inc.
|
||||
# David Sainty <saint@federated.computer> 2024 A.D.
|
||||
# Gossamer Threads to Discourse -- Soft Deletion of Marked-As-Deleted Posts
|
||||
# v0.1 Move from destroy to soft deletion.
|
||||
# v0.2 Address where due to import issues there are some stray PostCustomField records for Posts that do not exist.
|
||||
|
||||
require 'mysql2'
|
||||
require 'active_record'
|
||||
@ -46,20 +46,21 @@ class GossamerForumsSoftDelDeletedPosts < ImportScripts::Base
|
||||
# Soft delete the post, whether topic post (OP) or reply post
|
||||
def soft_delete_post(post, deleted_by_id)
|
||||
if post.deleted_at.nil?
|
||||
# Get the post's owner and topic information
|
||||
user = User.find_by(id: post.user_id) # Fetch user details
|
||||
topic = Topic.find_by(id: post.topic_id) # Fetch topic details
|
||||
# # Get the post's owner and topic information
|
||||
# user = User.find_by(id: post.user_id) # Fetch user details
|
||||
# topic = Topic.find_by(id: post.topic_id) # Fetch topic details
|
||||
|
||||
# Display post, user, and topic information
|
||||
puts "DELETING POST: SoftDelDeletedPosts: Soft deleting post with id: #{post.id}"
|
||||
puts " - Post owner: #{user.username} (ID: #{user.id})"
|
||||
puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
|
||||
# # Display post, user, and topic information
|
||||
# puts "DELETING POST: SoftDelDeletedPosts: Soft deleting post with id: #{post.id}"
|
||||
# puts " - Post owner: #{user.username} (ID: #{user.id})"
|
||||
# puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
|
||||
|
||||
# Perform the soft delete by updating the fields
|
||||
## post.deleted_at = Time.now
|
||||
## post.deleted_by_id = deleted_by_id
|
||||
## post.save
|
||||
### post.update(deleted_at: Time.now, deleted_by_id: deleted_by_id)
|
||||
## post.deleted_at = Time.now
|
||||
## post.deleted_by_id = deleted_by_id
|
||||
## post.save
|
||||
puts "... actual deleted_at step"
|
||||
### post.update(deleted_at: Time.now, deleted_by_id: deleted_by_id)
|
||||
else
|
||||
puts "SoftDelDeletedPosts: Post with id: #{post.id} is already soft deleted."
|
||||
end
|
||||
@ -96,42 +97,42 @@ class GossamerForumsSoftDelDeletedPosts < ImportScripts::Base
|
||||
end
|
||||
end
|
||||
|
||||
# Define the method to reverse (soft undelete) the soft deletion
|
||||
def soft_undelete_post(post)
|
||||
if post.deleted_at.present?
|
||||
# Get the post's owner and topic information
|
||||
user = User.find_by(id: post.user_id) # Fetch user details
|
||||
topic = Topic.find_by(id: post.topic_id) # Fetch topic details
|
||||
# Define the method to reverse (soft undelete) the soft deletion
|
||||
def soft_undelete_post(post)
|
||||
if post.deleted_at.present?
|
||||
# Get the post's owner and topic information
|
||||
user = User.find_by(id: post.user_id) # Fetch user details
|
||||
topic = Topic.find_by(id: post.topic_id) # Fetch topic details
|
||||
|
||||
# Display post, user, and topic information
|
||||
puts "RESTORING POST: SoftDelDeletedPosts: Soft undeleting post with id: #{post.id}"
|
||||
puts " - Post owner: #{user.username} (ID: #{user.id})"
|
||||
puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
|
||||
# Display post, user, and topic information
|
||||
puts "RESTORING POST: SoftDelDeletedPosts: Soft undeleting post with id: #{post.id}"
|
||||
puts " - Post owner: #{user.username} (ID: #{user.id})"
|
||||
puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
|
||||
|
||||
# Reverse the soft delete by setting deleted_at and deleted_by_id to nil
|
||||
### post.update(deleted_at: nil, deleted_by_id: nil)
|
||||
else
|
||||
puts "SoftDelDeletedPosts: Post with id: #{post.id} is not soft deleted."
|
||||
end
|
||||
end
|
||||
|
||||
# Define the method to reverse the soft deletion for all legacy posts
|
||||
def soft_undelete_all_deleted_posts
|
||||
# Query the legacy database for posts marked as deleted
|
||||
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1")
|
||||
|
||||
posts_result.each do |legacy_post|
|
||||
post_id = legacy_post['post_id']
|
||||
|
||||
# Look for the post in Discourse by custom field
|
||||
post = find_post_by_custom_field(post_id)
|
||||
|
||||
if post
|
||||
# Reverse the soft deletion (restore the post)
|
||||
soft_undelete_post(post)
|
||||
# Reverse the soft delete by setting deleted_at and deleted_by_id to nil
|
||||
### post.update(deleted_at: nil, deleted_by_id: nil)
|
||||
else
|
||||
puts "SoftDelDeletedPosts: Post with id: #{post.id} is not soft deleted."
|
||||
end
|
||||
end
|
||||
|
||||
# Define the method to reverse the soft deletion for all legacy posts
|
||||
def soft_undelete_all_deleted_posts
|
||||
# Query the legacy database for posts marked as deleted
|
||||
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1")
|
||||
|
||||
posts_result.each do |legacy_post|
|
||||
post_id = legacy_post['post_id']
|
||||
|
||||
# Look for the post in Discourse by custom field
|
||||
post = find_post_by_custom_field(post_id)
|
||||
|
||||
if post
|
||||
# Reverse the soft deletion (restore the post)
|
||||
soft_undelete_post(post)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Define the method to delete posts based on the Gossamer Forums flag
|
||||
def soft_del_all_deleted_posts
|
||||
@ -145,13 +146,46 @@ end
|
||||
posts_result.each do |legacy_post|
|
||||
post_id = legacy_post['post_id']
|
||||
|
||||
# Look for the post in Discourse by custom field
|
||||
post = find_post_by_custom_field(post_id)
|
||||
# # Look for the post in Discourse by custom field
|
||||
# post = find_post_by_custom_field(post_id)
|
||||
#
|
||||
# if post
|
||||
# # Soft delete only the individual post (whether topic post / OP or a reply post)
|
||||
# soft_delete_post(post, deleted_by_id)
|
||||
# end
|
||||
soft_del_all_deleted_posts_by_custom_field(post_id, deleted_by_id)
|
||||
end
|
||||
end
|
||||
|
||||
if post
|
||||
# Soft delete only the individual post (whether topic post / OP or a reply post)
|
||||
soft_delete_post(post, deleted_by_id)
|
||||
# Define a method to find all posts by the original Gossamer Forums post ID and soft delete them
|
||||
def soft_del_all_deleted_posts_by_custom_field(post_id, deleted_by_id)
|
||||
puts "SoftDelDeletedPosts: Searching for all posts with original_gossamer_id: #{post_id}"
|
||||
|
||||
# Find all PostCustomField records with the given Gossamer Forums post_id
|
||||
post_custom_fields = PostCustomField.where(name: 'original_gossamer_id', value: post_id.to_s)
|
||||
|
||||
if post_custom_fields.any?
|
||||
post_custom_fields.each do |post_custom_field|
|
||||
post = post_custom_field.post
|
||||
|
||||
if post
|
||||
# Get the post's owner and topic information
|
||||
user = User.find_by(id: post.user_id) # Fetch user details
|
||||
topic = Topic.find_by(id: post.topic_id) # Fetch topic details
|
||||
|
||||
# Display post, user, and topic information
|
||||
puts "DELETING POST: SoftDelDeletedPosts: Soft deleting post with id: #{post.id}"
|
||||
puts " - Post owner: #{user.username} (ID: #{user.id})"
|
||||
puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
|
||||
|
||||
# Soft delete the post if not already deleted
|
||||
soft_delete_post(post, deleted_by_id)
|
||||
else
|
||||
puts "SoftDelDeletedPosts: Found PostCustomField with no corresponding post (ID: #{post_custom_field.id})"
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "SoftDelDeletedPosts: No PostCustomField records found with original_gossamer_id: #{post_id}"
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user