- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathport.rake
293 lines (259 loc) · 9.74 KB
/
port.rake
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
namespace:dbdo
taskport: [
'db:port:users',
'db:port:badges',
'db:port:pictures',
'db:port:protips',
'db:port:comments',
'db:port:teams',
'db:port:likes',
'db:port:counters',
'db:clean:spam',
'cache:score:recalculate']
namespace:portdo
defport_data_since
days=ENV['since'].to_i
days=7ifdays == 0
since=days.days.ago
puts"Porting #{days} days ago"
["created_at > ? OR updated_at > ?",since,since]
end
task:connect=>:environmentdo
ifhide_sql_out=Rails.env.development?
ActiveRecord::Base.logger.level=Logger::INFO
end
LegacyRedis=Redis.new(url: ENV['LEGACY_REDIS_URL'])
Legacy=Sequel.connect(ENV['LEGACY_DB_URL'])
# Monkeypatch methods just for porting needs
ActiveRecord::Base.class_evaldo
defself.find_or_initialize_by_id(id)
where(id: id).first || new
end
defself.reset_pk_sequence
caseActiveRecord::Base.connection.adapter_name
when'PostgreSQL'
ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
else
raise"Task not implemented for this DB adapter"
end
end
end
end
namespace:jobsdo
task:clear=>:environmentdo
Job.delete_all
end
end
task:jobs=>:connectdo
puts"Sourcing jobs: #{ENV['source']}"
response=Faraday.get(ENV['source'])
results=JSON.parse(response.body)
results.eachdo |data|
nextifdata['company_logo'].blank? || ENV['COMPANY_BLACKLIST'].split(',').include?(data['company'])
data['company_logo'].sub!('http:','')
data['created_at']=Time.parse(data['created_at'])
data['role_type']=data.delete('type')
desc=data.delete("description")
url=data.delete('url')
found=URI.extract(data.delete("how_to_apply"),/http(s)?/).first
data['source']=found || url
data['source']=data['source'].chomp("apply")
data['expires_at']=1.month.from_now
data['author_name']='Seed Script'
data['author_email']='support@coderwall.com'
begin
job=Job.create!(data)
puts"Created: #{job.title}"
rescueException=>ex
puts"Failed: #{data['title']} - #{ex.message}"
end
end
end
task:check=>:connectdo
puts"legacy => ported"
puts"Likes: #{Legacy[:likes].count} => #{Like.count}"
puts"Comments: #{Legacy[:comments].count} => #{Comment.count}"
puts"Protips: #{Legacy[:protips].count} => #{Protip.count}"
puts"Badges: #{Legacy[:badges].count} => #{Badge.count}"
puts"Users: #{Legacy[:users].count} => #{User.count}"
end
task:karma=>:environmentdo
User.find_eachdo |user|
karma=user.badges.size * 10
karma += Like.where(likable: user.comments).size - user.comments.size
karma += Like.where(likable: user.protips).size - user.protips.size
karma += user.protips.sum(:views_count) / 50
karma=1ifkarma <= 0
user.update_column(:karma,karma)
puts"#{user.username}: #{karma}"
end
end
task:pictures=>:connectdo
Legacy[:pictures].eachdo |row|
ifrow[:user_id]
picture=Picture.find_or_initialize_by_id(row[:id])
picture.attributes.keys.eachdo |key|
picture[key]=row[key.to_sym]
end
picture.save!
else
puts"Skipped #{row[:id]} -> #{row.inspect}"
end
end
Picture.reset_pk_sequence
end
task:comments=>:connectdo
Comment.reset_pk_sequence
not_ported=[]
Legacy[:comments].where(port_data_since).eachdo |row|
ifrow[:comment].to_s.size >= 2
comment=Comment.find_or_initialize_by_id(row[:id])
comment.attributes.except(:comment).keys.eachdo |key|
comment[key]=row[key.to_sym]
end
comment.body=row[:comment]
if !comment.save
not_ported << comment
end
end
end
Comment.reset_pk_sequence
puts"Failed to port #{not_ported.size}"
end
task:teams=>:connectdo
Team.reset_pk_sequence
not_ported=[]
# .where(port_data_since)
Legacy[:teams].eachdo |row|
team=Team.find_or_initialize_by_id(row[:id])
team.attributes.keys.eachdo |key|
team[key]=row[key.to_sym]
end
team.color=row[:branding]
ifrow[:github_organization_name].present?
team.github=row[:github_organization_name]
end
legacy_impressions_key="team:#{row[:id]}:impressions"
team.views_count=LegacyRedis.get(legacy_impressions_key).to_i
ifteam.save
puts"#{team.name} (#{team.views_count})"
else
not_ported << team
puts"#{row[:name]} skipped #{team.errors.inspect}"
end
end
Team.reset_pk_sequence
puts"Failed to port #{not_ported.size}"
end
task:likes=>:connectdo
Like.reset_pk_sequence
Legacy[:likes].where(port_data_since).eachdo |row|
like=Like.find_or_initialize_by_id(row[:id])
like.attributes.keys.eachdo |key|
# puts "#{key} #{row[key.to_sym]}"
like[key]=row[key.to_sym]
end
iflike.save
putslike.id
else
puts"#{row[:id]} skipped #{like.errors.inspect}"
end
end
Like.reset_pk_sequence
end
task:badges=>:connectdo
Badge.reset_pk_sequence
Legacy[:badges].where(port_data_since).eachdo |row|
unlessrow[:badge_class_name].nil?
ifLEGACY_BADGES[row[:badge_class_name]].nil?
raiserow[:badge_class_name].inspect
end
puts"Importing #{row[:id]}"
badge=Badge.find_or_initialize_by_id(row[:id])
badge.user_id=row[:user_id]
badge.created_at=row[:created_at]
badge.updated_at=row[:updated_at]
legacy_badge=LEGACY_BADGES[row[:badge_class_name]]
badge.name=legacy_badge[0]
badge.image_name=legacy_badge[1]
badge.description=legacy_badge[2]
badge.why=legacy_badge[3]
badge.provider='Coderwall'
badge.save!
end
end
Badge.reset_pk_sequence
end
task:users=>:connectdo
User.reset_pk_sequence
Legacy[:users].where(port_data_since).eachdo |row|
putsrow[:username]
begin
user=User.find_or_initialize_by_id(row[:id])
user.attributes.keys.eachdo |key|
user[key]=row[key.to_sym]
end
social_links=[]
social_links << "[LinkedIn](#{row[:linkedin_public_url]})"unlessrow[:linkedin_public_url].blank?
social_links << "[Blog](#{row[:blog]})"unlessrow[:blog].blank?
social_links << "[Bitbucket](https://bitbucket.org/#{row[:bitbucket]})"unlessrow[:bitbucket].blank?
social_links << "[Codeplex](http://www.codeplex.com/site/users/view/#{row[:codeplex]})"unlessrow[:codeplex].blank?
social_links << "[Dribbble](http://dribbble.com/#{row[:dribbble]})"unlessrow[:dribbble].blank?
social_links << "[StackOverflow](http://stackoverflow.com/users/#{row[:stackoverflow]})"unlessrow[:stackoverflow].blank?
social_links << "[Speakerdeck](http://speakerdeck.com/u/#{row[:speakerdeck]})"unlessrow[:speakerdeck].blank?
social_links << "[Slideshare](http://www.slideshare.net/#{row[:slideshare]})"unlessrow[:slideshare].blank?
if !social_links.empty?
user.about=''ifuser.about.nil?
user.about << "\n\n\n#{social_links.join(' ')}\n\n"
end
user.karma=(Legacy[:endorsements].where(endorsed_user_id: row[:id]).count + 1)
user.password=SecureRandom.hex
user.skills=Legacy[:skills].select(:name,:tokenized).where(
deleted: false,
user_id: row[:id]).collect{|row| row[:name]}
ifteam=Legacy[:teams].where(id: row[:team_id]).collect.first
user.company=team[:name]
end
Rails.logger.info"#{row[:username]} => #{row[:email]}"
user.save!
end
end
User.reset_pk_sequence
end
task:protips=>:connectdo
Protip.reset_pk_sequence
not_ported=[]
Legacy[:protips].where(port_data_since).eachdo |row|
puts"#{row[:id]} : #{row[:public_id]} : #{row[:slug]}"
protip=Protip.find_or_initialize_by_id(row[:id])
protip.attributes.keys.eachdo |key|
protip[key]=row[key.to_sym]
end
protip.public_id=row[:public_id]
protip.likes_count=(Legacy[:likes].where(likable_id: row[:id],likable_type: 'Protip').count + 1)
protip.tags=Legacy[:tags].select(:name).join(:taggings,:tag_id=>:id).where(
taggable_id: row[:id],
taggable_type: 'Protip'
).collect{|row| row[:name]}
legacy_impressions_key="protip:#{protip.public_id}:impressions"
protip.views_count=LegacyRedis.get(legacy_impressions_key).to_i
protip.bad_content=row[:inappropriate].to_i > 0
ifprotip.user.blank? || !protip.save
not_ported << protip
end
end
Protip.reset_pk_sequence
puts"Failed to port #{not_ported.size}"
end
task:counters=>:environmentdo
Comment.where(port_data_since).find_eachdo |comment|
putscomment.id
Comment.reset_counters(comment.id,:likes)
end
Protip.where(port_data_since).find_eachdo |protip|
putsprotip.id
Protip.reset_counters(protip.id,:likes)
end
end
end
end