def forwards(self, orm): if db.dry_run: return try: from utils.redis_utils import default_connection from statistic import get_fetch_subtitles_key except ImportError: if settings.DEBUG: return raise Exception('Some redis utilits is unavailable, maybe this migration was not updated after refactoring. You can ignore this migration with: python manage.py migrate statistic 0002 --fake, but all statistic data will be lost.') try: default_connection.ping() except ConnectionError: if settings.DEBUG: return raise Exception('Redis server is unavailable. You can ignore this migration with: python manage.py migrate statistic 0002 --fake, but all statistic data will be lost.') print 'Total count of rows: ', orm.SubtitleFetchStatistic.objects.count() keys = [] for item in orm.SubtitleFetchStatistic.objects.order_by('id'): if not item.pk % 1000: print item.pk key = get_fetch_subtitles_key(item.video, item.language, item.created) if not key in keys: default_connection.delete(key) keys.append(key) default_connection.incr(key)
def handle(self, *args, **kwargs): if default_connection.ping() is None: print 'Redis is unavailable.' return start = time.time() qs = Video.objects.all()[:1000] print '"View" videos' i = 0 for video in qs: i += 1 if i % 100 == 0: print 'Viewed %s' % i st_widget_view_statistic.update(video=video) print '' print 'Migrate to DB' st_widget_view_statistic.migrate(verbosity=2) print 'Test migrated data' for video in qs: views_st = st_widget_view_statistic.get_views(video=video) assert video.widget_views_count >= views_st['month'], video.pk print time.time() - start
def _test_redis(self): print '=== REDIS ===' assert default_connection.ping(), u'Redis is unavailable' val = str(random.random()) key = 'test-redis-%s' % base64.b64encode(str(random.random())) default_connection.set(key, val) assert val == default_connection.get(key), u'Redis is unavailable. Can\'t get value' default_connection.delete(key) assert default_connection.get(key) is None, u'Redis is unavailable. Can\'t delete value' print 'OK' print