def update_to_supergroup(bot, update): old_id = update.message.migrate_from_chat_id new_id = update.message.chat_id user_id = update.message.from_user.id if old_id: UserStat.update(user_id, old_id, {'cid': new_id}) Entity.update_all(old_id, {'cid': new_id}) Chat.update(old_id, {'cid': new_id}) # Update all rows in chat_stats for c in db.query(ChatStat)\ .filter(ChatStat.cid == old_id)\ .all(): c.cid = new_id db.commit() bot.sendMessage(new_id, 'Group was updated to supergroup') cache.delete('last_{}'.format(old_id)) logger.info('Group {} was updated to supergroup {}'.format( old_id, new_id))