v0.4 Go back to simpler use of MySQL2

This commit is contained in:
David Sainty 2024-09-09 14:47:06 +10:00
parent 931d8cf8c0
commit d60cdafbfe

View File

@ -1,38 +1,31 @@
# Federated Computer, Inc. # Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D. # David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- CleanUp Script # Gossamer Threads to Discourse -- CleanUp Script
# v0.3 Other necessary fixes and tidyups # v0.4 Go back to simpler use of MySQL2
require 'mysql2' require 'mysql2'
require 'active_record' require 'active_record'
# require 'concurrent-ruby' # require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__) require File.expand_path("../../../../config/environment", __FILE__)
require File.expand_path("../../../../script/import_scripts/base", __FILE__)
class LegacyPost < ActiveRecord::Base class GossamerForumsDestroyDeletedPosts < ImportScripts::Base
self.table_name = 'gforum_Post' def initialize
end super
begin
class LegacyUser < ActiveRecord::Base # Initialize MySQL client to connect to Gossamer Forums database
self.table_name = 'gforum_User' @mysql_client = Mysql2::Client.new(
end host: "slowtwitch.northend.network",
username: "admin",
class GossamerForumsDestroyDeletedPosts password: "yxnh93Ybbz2Nm8#mp28zCVv",
# def initialize database: "slowtwitch"
# super )
# begin rescue Mysql2::Error => e
# # Initialize MySQL client to connect to Gossamer Forums database puts "Error connecting to MySQL: #{e.message}"
# @mysql_client = Mysql2::Client.new( exit 1
# host: "slowtwitch.northend.network", end
# username: "admin", end
# password: "yxnh93Ybbz2Nm8#mp28zCVv",
# database: "slowtwitch"
# )
# rescue Mysql2::Error => e
# puts "Error connecting to MySQL: #{e.message}"
# exit 1
# end
# end
# Define a method to find a post by custom field # Define a method to find a post by custom field
def find_post_by_custom_field(post_id) def find_post_by_custom_field(post_id)
@ -50,49 +43,24 @@ class GossamerForumsDestroyDeletedPosts
end end
end end
# Define the method to delete posts based on the Gossamer Forums flag
def destroy_deleted_posts_from_gossamer
# Connect to the legacy MySQL database
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: 'slowtwitch.northend.network',
database: 'slowtwitch',
username: 'admin',
password: 'yxnh93Ybbz2Nm8#mp28zCVv'
)
# Find all posts marked as deleted in the legacy database
LegacyPost.where(post_deleted: 1).find_each do |legacy_post|
# Look for the post in Discourse by custom field
post = find_post_by_custom_field(legacy_post.post_id)
if post
puts "DestroyDeletedPosts: Deleting post with id: #{post.id}"
# post.destroy
end
end
end
# Define the method to delete posts based on the Gossamer Forums flag # Define the method to delete posts based on the Gossamer Forums flag
def destroy_deleted_posts_from_gossamer_with_user(username) def destroy_deleted_posts_from_gossamer_with_user(username)
# Connect to the legacy MySQL database
ActiveRecord::Base.establish_connection( # Query the user ID from the legacy MySQL database
adapter: 'mysql2', user_result = @mysql_client.query("SELECT user_id FROM gforums_User WHERE user_username = '#{username}' LIMIT 1")
host: 'slowtwitch.northend.network', user_id_row = user_result.first
database: 'slowtwitch', if user_id_row.nil?
username: 'admin', puts "DiscourseDeletedPosts: No user found with username: #{username}"
password: 'yxnh93Ybbz2Nm8#mp28zCVv'
)
# Find the user ID for the given username
user = LegacyUser.find_by(user_username: username)
if user.nil?
puts "DestroyDeletedPosts: No user found with username: #{username}"
return return
end end
user_id = user_id_row['user_id']
# Find all posts marked as deleted by the given user
posts_result = @mysql_client.query("SELECT post_id FROM gforums_Post WHERE post_deleted = 1 AND user_id_fk = #{user_id}")
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
# Find all posts marked as deleted in the legacy database
LegacyPost.where(post_deleted: 1, user_id_fk: user.user_id).find_each do |legacy_post|
# Look for the post in Discourse by custom field # Look for the post in Discourse by custom field
post = find_post_by_custom_field(legacy_post.post_id) post = find_post_by_custom_field(legacy_post.post_id)