Issues in Like Count for OSQA Migration to Discourse

Hi guys,

I am trying to migrate from OSQA to Discourse. I am doing bulk migration due to our platform having a relatively large number of users and posts. I have imported all users, posts and all the likes. I am facing two issues w.r.t. likes.

  • I am shown the number of likes on any topic or post only when I click like on it. Otherwise, by default, the number of likes stay hidden. Is there some setting to disable this behavior and show all the likes from the beginning.
  • The likes given count and the likes received count in the user profile are not updated. They are shown to be zero. They are only updated for new likes, but not for old likes. Do I have to manually add the like count to the users? I thought there should be some sort of job that will make sure to update the like_counts for the user.

Please help me with this. I would be really thankful for it.

3 Likes

Have you ever seen this before @pfaffman? Surely if likes were migrated correctly they should be visible.

The last time I needed to do a bulk import I couldn’t quite get it to work. The only thing that I can think of off-hand is to make sure to run

 rake import:ensure_consistency
4 Likes

Aha bulk migration maybe @zogstrip can comment then.

@pfaffman: Thanks for the suggestion.

I usually run it after each import. Here is the output of my usual run. I think the output is fine.

/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:17: warning: already initialized constant I18n::RESERVED_KEYS
/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:17: warning: previous definition of RESERVED_KEYS was here
/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:32: warning: already initialized constant I18n::RESERVED_KEYS_PATTERN
/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:32: warning: previous definition of RESERVED_KEYS_PATTERN was here
/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:33: warning: already initialized constant I18n::EMPTY_HASH
/usr/share/rvm/gems/ruby-2.3.4/gems/i18n-1.0.1/lib/i18n.rb:33: warning: previous definition of EMPTY_HASH was here
[2018-08-17 12:31:48] Starting...
[2018-08-17 12:31:48] Inserting post timings...
[2018-08-17 12:31:49] Inserting post replies...
[2018-08-17 12:31:49] Inserting topic users...
[2018-08-17 12:31:49] Inserting topic views...
[2018-08-17 12:31:50] Inserting user actions for NEW_TOPIC = 4...
[2018-08-17 12:31:50] Inserting user actions for REPLY = 5...
[2018-08-17 12:31:50] Inserting user actions for RESPONSE = 6...
[2018-08-17 12:31:51] Inserting user options...
[2018-08-17 12:31:51] Inserting user stats...
[2018-08-17 12:31:51] Inserting user visits...
[2018-08-17 12:31:52] Inserting draft sequences...
[2018-08-17 12:31:52] Updating user stats...
[2018-08-17 12:31:53] Updating posts...
[2018-08-17 12:31:53] Updating topics...
[2018-08-17 12:31:53] Updating categories...
[2018-08-17 12:31:53] Updating users...
[2018-08-17 12:31:53] Updating groups...
[2018-08-17 12:31:53] Done!
1 Like

It’s been a little while since we’ve done a bulk import. Maybe it’s gone out of sync a bit. I’ll have a look.

1 Like

Thanks for your reply, Jeff :slight_smile:

It seems that few things might be unclear regarding my post. I am trying to expand on that.

On the topic:

  • The like count is hidden in the beginning. When someone clicks on the like button on it, then the like count is shown and this count is correctly imported. It actually shows the total number of likes correctly. Added an example later for details.

On the user profile:

  • Here it only shows the new likes done after import. Please see the example below for more details.

Example

Suppose there is a topic T and user U. The actual number of likes on the T are, say 100, and the total number of likes by U are 10.

Then, when U likes the topic T, then the like count shown is 101 (which is correct). But on the user profile of U the total likes given are still 1, not 11.

In summary, I want to resolve both of these issues, i.e.

  • When some body opens the topic, just show the 100 likes without having a user to click like to show the like count. Is there some sort of setting for it?
  • The user profile should contain the like counts properly.
2 Likes

Can you share the code that imports the likes?

2 Likes

@zogstrip: Here is the relevant code for it.

def import_likes
    puts "Importing likes..."

    @last_imported_post_id = 0

    post_thanks = mysql_stream <<-SQL
      SELECT
        fa.user_id as user_id,
        fa.node_id as node_id,
        fa.action_date as action_date
        from forum_action fa
        INNER JOIN forum_node fn
        ON fa.node_id = fn.id
        where canceled = 0
        and (action_type = 'voteup' 
          || action_type = 'voteupcomment')
        and fa.node_id > #{@last_imported_post_id}
        ORDER BY fa.node_id ASC
    SQL

    @imported_likes = Set.new
    
    errors = 0
    create_post_actions(post_thanks) do |row|
      post_id = post_id_from_imported_id(row['node_id'])
      user_id = user_id_from_imported_id(row['user_id'])

      if post_id.nil? || user_id.nil?
        errors += 1
        next
      end

      if @imported_likes.add?([post_id, user_id]).nil?
        errors += 1
        next
      end

      # next if post_id.nil? || user_id.nil?
      # next if @imported_likes.add?([post_id, user_id]).nil?

      like = {
        post_id: post_id,
        user_id: user_id,
        post_action_type_id: 2,
        created_at: Time.zone.at(row['action_date'])
      }

      # puts like

      # script_exit
      like
    end

    puts "total errors in likes importing: ", errors

  end
1 Like

@zogstrip: Finally, I manually moved the likes_given_count and likes_received_count to user statistics (i.e. in the function import_user_stats). Now, everything is fine except when I see the user activity by clicking on the likes given, I receive “No liked posts.”. It seems that the table user_actions doesn’t contain any like related action, it contains only action id’s 4, 5 and 6.

2 Likes

I recently did a likes-import directly in SQL, and updated counts successfully. If you want, I can share queries here, but since it’s a “worst-practice” doing it through the database, it didn’t think of sharing it with the Community. I will, if there is interest.

A trick that is useful even if you’re writing a proper Ruby importer is to run the server with a command like this

bundle exec rails server --binding=0.0.0.0 | grep -i 'update\|insert'

And then try the action (in this case, the “Like”) from the UI. You will see the SQL that Discourse is using to change tables. This will help you figure out the several things involved in the action.

2 Likes

@pgr: Thanks for letting me know your approach.

Coincidentally, I was able to resolve the issue today. I didn’t need to make changes in the database directly. I made changes in the base.rb to handle the create_user_actions (very similar to create_post_actions) and used this create_user_actions function to bulk migrate the likes given and received by the user. This fixed all the like related issues for me.

4 Likes