Skip to content

Commit e3215fd

Browse files
committed
fixing some issues migrating public ids
1 parent b57e83f commit e3215fd

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

app/models/protip.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Protip < ActiveRecord::Base
66

77
BIG_BANG = Time.parse("05/07/2012").to_i #date protips were launched
88
before_update :cache_cacluated_score!
9-
before_create :generate_public_id
9+
before_create :generate_public_id, if: :public_id_blank?
1010

1111
belongs_to :user, autosave: true
1212
has_many :comments, dependent: :destroy
@@ -83,12 +83,15 @@ def cacluate_score
8383
end
8484

8585
def generate_public_id
86-
return self.public_id if self.public_id.present?
8786
self.public_id = SecureRandom.urlsafe_base64(4).downcase
8887
#retry if not unique
8988
generate_public_id unless Protip.where(public_id: self.public_id).blank?
9089
end
9190

91+
def public_id_blank?
92+
public_id.blank?
93+
end
94+
9295
def cache_cacluated_score!
9396
self.score = cacluate_score
9497
end

lib/tasks/db.rake

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace :db do
1111
'cache:score:recalculate']
1212

1313
task :fix_counters => :environment do
14-
Comment.find_each do |comment|
14+
Comment.where(port_data_since).find_each do |comment|
1515
puts comment.id
1616
Comment.reset_counters(comment.id, :likes)
1717
end
18-
Protip.find_each do |protip|
18+
Protip.where(port_data_since).find_each do |protip|
1919
puts protip.id
2020
Protip.reset_counters(protip.id, :likes)
2121
end
@@ -165,13 +165,15 @@ namespace :db do
165165
end
166166

167167
def port_data_since
168-
# OR updated_at > ?
169-
["created_at > ?", 7.days.ago]
168+
days = ENV['since'].to_i
169+
days = 7 if days == 0
170+
since = days.days.ago
171+
puts "Porting #{days} days ago"
172+
["created_at > ?", since]
170173
end
171174

172175
task :users => :connect do
173176
User.reset_pk_sequence
174-
# puts Legacy[:users].where("created_at >= ?", 2.days.ago).count
175177
Legacy[:users].where(port_data_since).each do |row|
176178
puts row[:username]
177179
begin
@@ -220,7 +222,7 @@ namespace :db do
220222
protip.attributes.keys.each do |key|
221223
protip[key] = row[key.to_sym]
222224
end
223-
225+
protip.public_id = row[:public_id]
224226
protip.likes_count = (Legacy[:likes].where( likable_id: row[:id], likable_type: 'Protip').count + 1)
225227
protip.tags = Legacy[:tags].select(:name).join(:taggings, :tag_id => :id).where(
226228
taggable_id: row[:id],

0 commit comments

Comments
 (0)