forked from SamSaffron/discourse_public_import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_db.rb
244 lines (218 loc) · 5.95 KB
/
import_db.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# todo: port schema from .sql file to table creation and insertion in Ruby
require "sqlite3"
require "mini_sql"
sqlite_conn = SQLite3::Database.new("dump.db")
conn = MiniSql::Connection.get(sqlite_conn)
Dir.chdir("/home/sam/Source/discourse")
require "/home/sam/Source/discourse/config/environment"
RateLimiter.disable
def import_users(conn)
puts "Importing users..."
created = 0
conn
.query("SELECT * FROM users")
.each_slice(5000) do |slice|
slice.each do |row|
User.transaction do
if !User.exists?(row.id)
User.create(
id: row.id,
username: row.username,
name: row.username,
password: SecureRandom.hex,
email: "#{SecureRandom.hex}@email.com"
)
end
print "."
created += 1
puts "#{created} users created" if created % 500 == 0
end
end
end
end
def import_topics(conn)
categories =
Category.pluck(:name, :id).map { |name, id| [name.downcase, id] }.to_h
puts "ensuring categories exist..."
# ensuring categories exist
conn
.query("SELECT DISTINCT category FROM topics")
.each do |row|
if !categories[row.category.downcase]
category =
Category.create!(
name: row.category,
user_id: -1,
skip_category_definition: true
)
categories[row.category.downcase] = category.id
puts "created #{row.category}"
end
end
puts "creating topics..."
created = 0
conn
.query("SELECT id FROM topics")
.each_slice(100) do |slice|
Topic.transaction do
slice.each do |row|
if !Topic.exists?(row.id)
topic =
conn.query("SELECT * FROM topics WHERE id = ?", row.id).first
t =
Topic.new(
id: topic.id,
title: topic.title,
category_id: categories[topic.category],
user_id: topic.user_id,
created_at: topic.created_at,
updated_at: topic.created_at
)
t.save!(validate: false)
print "."
end
created += 1
puts "#{created} topics created" if created % 500 == 0
end
end
end
end
def import_posts(conn)
puts "creating posts..."
created = 0
conn
.query("SELECT id,topic_id,post_number FROM posts order by id asc")
.each_slice(100) do |slice|
Post.transaction do
slice.each do |row|
if DB.query(
"SELECT 1 FROM posts where (topic_id = ? and post_number = ?)",
row.topic_id,
row.post_number
).blank?
post = conn.query("SELECT * FROM posts WHERE id = ?", row.id).first
p =
Post.new(
id: post.id,
raw: post.raw,
cooked: PrettyText.cook(post.raw),
user_id: post.user_id,
created_at: post.created_at,
updated_at: post.created_at,
post_number: post.post_number,
topic_id: post.topic_id
)
p.save!(validate: false)
print "."
end
created += 1
puts "#{created} posts created" if created % 500 == 0
end
end
end
end
def import_likes(conn)
puts "creating likes..."
created = 0
conn
.query(
"SELECT post_id,user_id,created_at FROM likes order by post_id, user_id asc"
)
.each_slice(100) do |slice|
PostAction.transaction do
slice.each do |row|
if DB.query(
"SELECT 1 FROM post_actions where (post_id = ? and user_id = ? and post_action_type_id = ?)",
row.post_id,
row.user_id,
2
).blank?
DB.exec(
"INSERT INTO post_actions (post_id, user_id, post_action_type_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
row.post_id,
row.user_id,
2,
row.created_at,
row.created_at
)
print "."
end
created += 1
puts "#{created} likes created" if created % 500 == 0
end
end
end
end
def fix_likes
DB.exec <<~SQL
INSERT INTO user_actions (
user_id,
action_type,
target_topic_id,
target_post_id,
acting_user_id,
created_at,
updated_at
)
SELECT pa.user_id, 1, p.topic_id, pa.post_id, pa.user_id, pa.created_at, pa.created_at
FROM post_actions pa
JOIN posts p ON p.id = pa.post_id
WHERE post_action_type_id = 2
UNION ALL
SELECT p.user_id, 2, p.topic_id, pa.post_id, pa.user_id, pa.created_at, pa.created_at
FROM post_actions pa
JOIN posts p ON p.id = pa.post_id
WHERE post_action_type_id = 2
ON CONFLICT DO NOTHING
SQL
end
def fix_sequences
%w[users topics posts post_actions].each { |table| DB.exec <<~SQL }
SELECT setval('public."#{table}_id_seq"',
(SELECT MAX(id) FROM public.#{table})
);
SQL
end
def fix_counts
DB.exec <<~SQL
UPDATE posts
SET like_count = (
SELECT COUNT(*)
FROM post_actions
WHERE post_actions.post_id = posts.id
AND post_action_type_id = 2
)
SQL
DB.exec <<~SQL
UPDATE topics
SET like_count = (
SELECT SUM(like_count)
FROM posts
WHERE posts.topic_id = topics.id
)
SQL
end
def fix_dates
DB.exec <<~SQL
UPDATE topics
SET created_at = (SELECT MIN(created_at) FROM posts WHERE posts.topic_id = topics.id)
SQL
DB.exec <<~SQL
UPDATE topics
SET updated_at = (SELECT MAX(created_at) FROM posts WHERE posts.topic_id = topics.id)
SQL
DB.exec <<~SQL
UPDATE topics
SET bumped_at = (SELECT MAX(created_at) FROM posts WHERE posts.topic_id = topics.id)
SQL
end
import_users(conn)
import_topics(conn)
import_posts(conn)
import_likes(conn)
fix_likes
fix_sequences
fix_counts
fix_dates
Jobs::EnsureDbConsistency.new.execute(nil)
Topic.reset_all_highest!