Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Discourse generator of a test database
# can be used to generate a mock db for profiling purposes
# we want our script to generate a consistent output, to do so
# we monkey patch array sample so it always uses the same rng
class Array
RNG = Random.new(1098109928029800)
def sample
self[RNG.rand(size)]
end
end
# based on https://gist.github.com/zaius/2643079
def unbundled_require(gem)
if defined?(::Bundler)
spec_path = Dir.glob("#{Gem.dir}/specifications/#{gem}-*.gemspec").last
if spec_path.nil?
raise LoadError
end
spec = Gem::Specification.load spec_path
spec.activate
end
begin
require gem
end
end
def sentence
@gabbler ||= Gabbler.new.tap do |gabbler|
story = File.read(File.dirname(__FILE__) + "/alice.txt")
gabbler.learn(story)
end
sentence = ""
until sentence.length > 800 do
sentence << @gabbler.sentence
sentence << "\n"
end
sentence
end
def create_admin(seq)
User.new.tap { |admin|
admin.email = "admin@localhost#{seq}.fake"
admin.username = "admin#{seq}"
admin.password = "password12345abc"
admin.save!
admin.grant_admin!
admin.change_trust_level!(TrustLevel[4])
admin.activate
}
end
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
SiteSetting.queue_jobs = false
unless Rails.env == "production"
puts "This script should only be used in the profile environment"
exit
end
# by default, Discourse has a "system" and `discobot` account
# and in my setup I have an admin account configured
if User.count > 3
puts "Only run this script against an empty DB"
exit
end
require 'optparse'
begin
unbundled_require 'gabbler'
rescue LoadError
puts "installing gabbler gem"
puts `gem install gabbler`
unbundled_require 'gabbler'
end
puts "Creating 10 users"
users = 10.times.map do |i|
putc "."
create_admin(i)
end
puts
puts "Creating 10 categories"
categories = 10.times.map do |i|
putc "."
Category.create(name: "category#{i}", text_color: "ffffff", color: "000000", user: users.first)
end
puts
puts "Creating 50 topics"
topic_ids = 50.times.map do
post = PostCreator.create(users.sample, raw: sentence, title: sentence[0..50].strip, category: categories.sample.name, skip_validations: true)
putc "."
post.topic_id
end
puts
puts "creating 200 replies"
200.times do
putc "."
PostCreator.create(users.sample, raw: sentence, topic_id: topic_ids.sample, skip_validations: true)
end
# no sidekiq so update some stuff
Category.update_stats
Jobs::PeriodicalUpdates.new.execute(nil)
@tpokorra

This comment has been minimized.

Copy link
Owner Author

commented Oct 3, 2018

this is from https://github.com/discourse/discourse/blob/master/script/profile_db_generator.rb

plus my modifications:

  • to run it deliberately inside a production environment because it is easy to set up
  • to work with my admin user installed
  • to have less users and less posts and less replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.