A platform for community discussion. Free, open, simple.
DEV: Add bookmark import support to bulk import scripts (#37677)
Ruben Oussoren07cd68db6d06
diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rbindex 94c71510df6..c6567777f12 100644--- a/script/bulk_import/base.rb+++ b/script/bulk_import/base.rb@@ -287,6 +287,9 @@ class BulkImport::Baseputs "Loading post actions indexes..."@last_post_action_id = last_id(PostAction)+ puts "Loading bookmark indexes..."+ @last_bookmark_id = last_id(Bookmark)+puts "Loading upload indexes..."@uploads_mapping = load_index(MAPPING_TYPES[:upload])@uploads_by_sha1 = Upload.pluck(:sha1, :id).to_h@@ -370,6 +373,11 @@ class BulkImport::Baseif @last_post_action_id > 0@raw_connection.exec("SELECT setval('#{PostAction.sequence_name}', #{@last_post_action_id})")end+ if @last_bookmark_id > 0+ @raw_connection.exec(+ "SELECT setval(pg_get_serial_sequence('bookmarks', 'id'), #{@last_bookmark_id})",+ )+ endif @last_user_avatar_id > 0@raw_connection.exec("SELECT setval('#{UserAvatar.sequence_name}', #{@last_user_avatar_id})")end@@ -753,6 +761,21 @@ class BulkImport::Basetotal_msecs_viewed]+ BOOKMARK_COLUMNS = %i[+ id+ user_id+ bookmarkable_id+ bookmarkable_type+ name+ reminder_at+ reminder_set_at+ reminder_last_sent_at+ auto_delete_preference+ pinned+ created_at+ updated_at+ ]+TAG_USER_COLUMNS = %i[tag_id user_id notification_level created_at updated_at]UPLOAD_COLUMNS = %i[@@ -1098,6 +1121,10 @@ class BulkImport::Basecreate_records(rows, "topic_user", TOPIC_USER_COLUMNS, &block)end+ def create_bookmarks(rows, &block)+ create_records(rows, "bookmark", BOOKMARK_COLUMNS, &block)+ end+def create_tag_users(rows, &block)create_records(rows, "tag_user", TAG_USER_COLUMNS, &block)end@@ -1643,6 +1670,14 @@ class BulkImport::Basetopic_userend+ def process_bookmark(bookmark)+ bookmark[:id] ||= @last_bookmark_id += 1+ bookmark[:auto_delete_preference] ||= 0+ bookmark[:created_at] ||= NOW+ bookmark[:updated_at] ||= bookmark[:created_at]+ bookmark+ end+def process_tag_user(tag_user)tag_user[:created_at] = NOWtag_user[:updated_at] = NOWdiff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rbindex 55bd4604698..e41768af0e0 100644--- a/script/bulk_import/generic_bulk.rb+++ b/script/bulk_import/generic_bulk.rb@@ -38,6 +38,11 @@ class BulkImport::Generic < BulkImport::Baseputs "running 'import:ensure_consistency' rake task."Rake::Task["import:ensure_consistency"].invoke++ # Force refresh directory items to sync user stats+ # This is needed even when enable_user_directory is false+ puts "", "Refreshing directory items and syncing user stats..."+ refresh_directory_itemsenddef execute@@ -109,6 +114,7 @@ class BulkImport::Generic < BulkImport::Baseimport_topic_usersupdate_topic_users+ import_bookmarksimport_user_stats@@ -144,6 +150,23 @@ class BulkImport::Generic < BulkImport::Base@uploads_db.close if @uploads_dbend+ def refresh_directory_items+ start_time = Time.now++ # Force refresh all directory periods - this works even when+ # enable_user_directory site setting is disabled+ DirectoryItem.period_types.each_key do |period|+ puts " Refreshing directory items for period: #{period}"+ DirectoryItem.refresh_period!(period, force: true)+ end++ UserStat.ensure_consistency!+ Discourse.cache.clear+ Site.clear_cache++ puts " Refreshed directory items in #{(Time.now - start_time).to_i} seconds."+ end+def enable_required_pluginsputs "", "Enabling required plugins..."@@ -1498,6 +1521,85 @@ class BulkImport::Generic < BulkImport::Baseputs " Updated topic users in #{(Time.now - start_time).to_i} seconds."end+ def import_bookmarks+ unless @source_db.get_first_value(+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='bookmarks'",+ )+ return+ end++ puts "", "Importing bookmarks..."++ bookmarks = query(<<~SQL)+ SELECT id, user_id, bookmarkable_id, bookmarkable_type, name,+ reminder_at, reminder_set_at, reminder_last_sent_at,+ auto_delete_preference, pinned, created_at, updated_at+ FROM bookmarks+ ORDER BY user_id, bookmarkable_id+ SQL++ existing_bookmarks = Bookmark.pluck(:user_id, :bookmarkable_type, :bookmarkable_id).to_set++ create_bookmarks(bookmarks) do |row|+ user_id = user_id_from_imported_id(row["user_id"])+ next unless user_id++ bookmarkable_type = row["bookmarkable_type"].to_s+ bookmarkable_id = row["bookmarkable_id"]++ if bookmarkable_type == "Topic"+ topic_id = topic_id_from_imported_id(bookmarkable_id)+ next unless topic_id+ next unless existing_bookmarks.add?([user_id, bookmarkable_type, topic_id])++ {+ user_id: user_id,+ bookmarkable_id: topic_id,+ bookmarkable_type: bookmarkable_type,+ name: row["name"],+ reminder_at: to_datetime(row["reminder_at"]),+ reminder_set_at: to_datetime(row["reminder_set_at"]),+ reminder_last_sent_at: to_datetime(row["reminder_last_sent_at"]),+ auto_delete_preference: row["auto_delete_preference"]&.to_i,+ pinned: row["pinned"].present? ? to_boolean(row["pinned"]) : false,+ created_at: to_datetime(row["created_at"]),+ updated_at: to_datetime(row["updated_at"]),+ }+ elsif bookmarkable_type == "Post"+ post_id = post_id_from_imported_id(bookmarkable_id)+ next unless post_id+ next unless existing_bookmarks.add?([user_id, bookmarkable_type, post_id])++ {+ user_id: user_id,+ bookmarkable_id: post_id,+ bookmarkable_type: bookmarkable_type,+ name: row["name"],+ reminder_at: to_datetime(row["reminder_at"]),+ reminder_set_at: to_datetime(row["reminder_set_at"]),+ reminder_last_sent_at: to_datetime(row["reminder_last_sent_at"]),+ auto_delete_preference: row["auto_delete_preference"]&.to_i,+ pinned: row["pinned"].present? ? to_boolean(row["pinned"]) : false,+ created_at: to_datetime(row["created_at"]),+ updated_at: to_datetime(row["updated_at"]),+ }+ else+ next+ end+ end++ bookmarks.close++ puts " Updating topic_users.bookmarked from bookmarks..."+ DB.exec(<<~SQL)+ INSERT INTO topic_users (user_id, topic_id, bookmarked)+ SELECT b.user_id, b.bookmarkable_id, TRUE+ FROM bookmarks b+ WHERE b.bookmarkable_type = 'Topic'+ ON CONFLICT (user_id, topic_id) DO UPDATE SET bookmarked = TRUE+ SQL+ end+def import_user_statsputs "", "Importing user stats..."