问题描述
由于一个bug,导致不同卡片可能会出现相同的五个单词
问题原因
自动生成卡片的触发时机,是访问/cardList/today时,当满足条件触发。
如果两次请求访问间隔很短,前一次生成的卡片还未存入数据库时,则会触发第二次生成。
解决办法
加锁
冗余数据的解决办法
- 找出今天的所有卡片,按用户分组
SELECT card.openid,card_id,GROUP_CONCAT(word_name)FROM card_word INNER JOIN card ON card.id = card_id WHERE DATEDIFF(card.CREATE_DATE,NOW())=0 GROUP BY card_id
=> repeatCard.json
2. 找出这些卡片的所有单词,筛选拥有重复单词的卡片ID
3. 删除这些卡片
# 转为openid,card_id,wordlist格式
with open("repeatCard.json") as f:
cardDict = {}
lines = f.readlines()
for card in lines:
item = card.split(" ")
cardDict[item[1]] = {"openid": item[0], "wordlist": item[2]}
# print(cardDict)
# 找出openid和wordlist相同的卡片
for card in cardDict:
for card2 in cardDict:
if card != card2:
if cardDict[card]["openid"] == cardDict[card2]["openid"] and cardDict[card]["wordlist"] == cardDict[card2]["wordlist"]:
print("delete from card where id="+ card +";")
print("delete from card_word where card_id="+card+";")