async def flush_to_db(ctx): redis = await create_pool(RedisSettings.from_url(REDIS_URL)) while 1: post_id = await redis.spop(RK_VISITED_POST_IDS) if post_id is None: break post = await Post.get(Q(id=post_id)) if post: post._pageview = int(await redis.get(RK_PAGEVIEW.format(post_id)) or 0) await post.save()
async def flush_to_db(ctx): redis = await create_pool(RedisSettings.from_url(REDIS_URL)) while 1: if (post_id := await redis.spop(RK_VISITED_POST_IDS)) is None: break post = await Post.get(Q(id=post_id)) if post: post._pageview = int( await redis.hget(RK_PAGEVIEW.format(post_id), PAGEVIEW_FIELD) or 0) await post.save() print(f'Flush Post(id={post_id}) pageview')
async def flush_to_db(ctx): redis = await create_pool(RedisSettings.from_url(REDIS_URL)) while 1: if (post_id := await redis.spop(RK_VISITED_POST_IDS)) is None: break post = await Post.filter(id=post_id).first() if post: post._pageview = int(await redis.hget( RK_PAGEVIEW.format(post_id), PAGEVIEW_FIELD) or 0) await post.save() logger.info(f'Flush Post(id={post_id}) pageview') else: logger.warning(f'Post(id={post_id}) have deleted!')
async def _migrate_for_v27() -> None: redis = await get_redis() keys = await redis.keys('lyanna:pageview:*') ids = [] for k in keys: id = k.split(b':')[-1] if id.isdigit(): await redis.hset(RK_PAGEVIEW.format(id.decode()), PAGEVIEW_FIELD, int(await redis.get(k))) ids.append(id) await redis.sadd(RK_ALL_POST_IDS, *ids) await init_db(create_db=False) client = Tortoise.get_connection('default') await client.execute_script( '''CREATE TABLE `special_item` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `post_id` int(11) NOT NULL, `index` smallint(6) NOT NULL, `special_id` smallint(6) NOT NULL, `created_at` datetime(6) NOT NULL, PRIMARY KEY (`id`), KEY `idx_special_post` (`special_id`,`post_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8''') await client.execute_script( '''CREATE TABLE `special_topic` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `intro` varchar(2000) NOT NULL, `title` varchar(100) NOT NULL, `created_at` datetime(6) NOT NULL, `status` smallint(6) NOT NULL DEFAULT '0', `slug` varchar(100) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `title` (`title`), KEY `idx_slug` (`slug`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8''') await client.execute_script( ('alter table post_tags add column `updated_at` datetime(6) ' 'DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)')) await client.execute_script('alter table users add column `avatar` varchar(100) DEFAULT ""') # noqa