Skip to content

A platform for community discussion. Free, open, simple.

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

Vinoth Kannan2c7cc40be3c9
diff --git a/lib/discourse_dev/record.rb b/lib/discourse_dev/record.rb
index 6a57189bf12..71ff5af9cda 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 c64f8dc3466..8b9fa8fe108 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)