DEV: only use the records that are auto populated by the task. (#14360)

DEV: only use the records that are auto populated by the task. (#14360)

Previously, it was using existing user and topic records to generate random posts.

diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb
index 6a57189..71ff5af 100644
--- a/lib/discourse_dev/record.rb
+++ b/lib/discourse_dev/record.rb
@@ -7,6 +7,7 @@ require 'faker'
 module DiscourseDev
   class Record
     DEFAULT_COUNT = 30.freeze
+    AUTO_POPULATED = "auto_populated"
 
     attr_reader :model, :type
 
@@ -24,6 +25,7 @@ module DiscourseDev
 
     def create!
       record = model.create!(data)
+      record.custom_fields[AUTO_POPULATED] = true if record.respond_to?(:custom_fields)
       yield(record) if block_given?
       DiscourseEvent.trigger(:after_create_dev_record, record, type)
       record
@@ -71,8 +73,12 @@ module DiscourseDev
       self.new.populate!
     end
 
-    def self.random(model)
-      offset = Faker::Number.between(from: 0, to: model.count - 1)
+    def self.random(model, use_existing_records: true)
+      model.joins(:_custom_fields).where("#{:type}_custom_fields.name = '#{AUTO_POPULATED}'") if !use_existing_records && model.new.respond_to?(:custom_fields)
+      count = model.count
+      raise "#{:type} records are not yet populated" if count == 0
+
+      offset = Faker::Number.between(from: 0, to: count - 1)
       model.offset(offset).first
     end
   end
diff --git a/lib/discourse_dev/user.rb b/lib/discourse_dev/user.rb
index c64f8dc..8b9fa8f 100644
--- a/lib/discourse_dev/user.rb
+++ b/lib/discourse_dev/user.rb
@@ -44,7 +44,7 @@ module DiscourseDev
     end
 
     def self.random
-      super(::User)
+      super(::User, use_existing_records: false)
     end
 
     def set_random_avatar(user)

GitHub sha: 2c7cc40be3c9625e68c67e513aeb57dfe6cfe09d

This commit appears in #14360 which was approved by eviltrout. It was merged by vinothkannans.