-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathseed_db_data.rb
executable file
·117 lines (94 loc) · 2.72 KB
/
seed_db_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env ruby
# Based on Discourse's profile_db_generator
require 'optparse'
require 'gabbler'
random_seed = 2546769937
do_drop = false
OptionParser.new do |opts|
opts.banner = "Usage: RAILS_ENV=profile ruby seed_db_data.rb [options]"
opts.on("-r", "--random-seed NUMBER", "random seed") do |r|
random_seed = r
end
opts.on("-d", "--drop", "drop existing database and re-migrate first") do
do_drop = true
end
end.parse!
# Set the constant
RANDOM_SEED = random_seed
# we want our script to generate a consistent output, to do so
# we monkey patch array sample so it always uses the same rng.
# All randomization in this script uses .sample.
class Array
RNG = Random.new(RANDOM_SEED)
def sample
self[RNG.rand(size)]
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@fake#{seq}.appfolio.com"
admin.username = "admin#{seq}"
admin.password = "longpassword"
admin.save!
admin.grant_admin!
admin.change_trust_level!(TrustLevel[4])
admin.email_tokens.update_all(confirmed: true)
admin.activate # Added after activation seemed not to work
admin.approved = true
admin.active = true
admin.save!
}
end
require File.expand_path(File.join(File.dirname(__FILE__), "work/discourse/config/environment"))
unless Rails.env == "profile"
puts "This script should only be used in the profile environment"
exit
end
if do_drop
system "cd work/discourse && RAILS_ENV=profile rake db:drop db:create db:migrate"
end
SiteSetting.queue_jobs = false
# by default, Discourse has a "system" account
if User.count > 1
puts "Only run this script against an empty DB (and in RAILS_ENV=profile)"
exit
end
puts "Creating 100 users"
users = 100.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 100 topics"
topic_ids = 100.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" # PostCreator is just crazy slow. Why?
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)