Migrare un forum NodeBB con MongoDB a Discourse

Sono riuscito in qualche modo a far funzionare correttamente la funzione dello script.

Ecco le mie modifiche (con un piccolo aiuto da un Claude Code :slight_smile:)

 	>mongo.rb
   
   def posts(offset = 0, page_size = 1000)
      post_keys = mongo.find(_key: "posts:pid").skip(offset).limit(page_size).pluck(:value)
      post_keys
          .map { |pid| post(pid) }
          .compact  # <-- scarta qualsiasi risultato nil (pid orfani)
      post_keys.map { |post_key| post(post_key) }
    end

    def post(id)
    post = mongo.find(_key: "post:#{id}").first
    return nil if post.nil? # <-- controllo null
    post["timestamp"] = timestamp_to_date(post["timestamp"])
    if post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first
        post["upvoted_by"] = mongo.find(_key: "pid:#{id}:upvote").first[:members]
      else
        post["upvoted_by"] = []
      end

      post["pid"] = post["pid"].to_s
      post["deleted"] = post["deleted"].to_s

      post
    end
	
>nodebb.rb

 create_posts(posts, total: post_count, offset: offset) do |post|
        # salta se il post è nullo
		# salta se è un merged_post
        next if post.nil?
        next if @merged_posts_map[post["pid"]]

        # salta se è cancellato
        next if post["deleted"] == "1"

        raw = post["content"]
        post_id = "p#{post["pid"]}"

        next if raw.blank?
        topic = topic_lookup_from_imported_post_id("t#{post["tid"]}")

        unless topic
          puts "Topic with id #{post["tid"]} not found, skipping"
          next
        end	

Sembra che ora funzioni nel modo giusto.

Anche se non so quanto sia corretto dal punto di vista dell’architettura interna di Discourse, a prima vista sembra funzionare.

Qualsiasi suggerimento per miglioramenti e ottimizzazioni sono benvenuti.

1 Mi Piace