def migrate_scan_adjustments(self): adjs = KarmaAdjustment._query(data=True) for adj in adjs: sr = Subreddit._byID(adj.sr_id) gravity = 'ups' if adj.amount >= 0 else 'downs' key = 'karma_{0}_adjustment_{1}'.format(gravity, sr.name) self.new_values[adj.account_id][key] += abs(adj.amount)
def migrate_scan_adjustments(self): # These should hopefully all fit in memory at once because this feature # is relatively new, but dividing up the work is still necessary in # order to run this script more than once. STEP = 100 max_id = self.max_thing_id(KarmaAdjustment) id_start = int(self.state.kvstore.get('karmaadjustment.cur_read_id', '0')) print('Scanning {0}. Max id is {1}, starting at {2}'.format( 'adjustments', max_id, id_start)) for id_low in xrange(id_start, max_id + 1, STEP): adjs = list(KarmaAdjustment._query( KarmaAdjustment.c._id >= id_low, KarmaAdjustment.c._id < id_low + STEP, data=True)) print('{0}: {1}, {2} of {3}'.format( datetime.now().isoformat(' '), 'adjustments', id_low, max_id)) for adj in adjs: # adj.amount can be either positive or negative self.state.tally_karma(adj.account_id, adj.sr_id, 'adjustment', adj.amount) if adjs: max_id = max(a._id for a in adjs) self.state.kvstore['karmaadjustment.cur_read_id'] = str(max_id + 1) self.state.commit()