chore: add allow_download field to existing Document records

--bug=1060848 --user=刘瑞斌 【知识库】文档设置[允许知识库来源中下载 ]应该默认开启 https://www.tapd.cn/62980211/s/1762365
This commit is contained in:
CaptainB 2025-08-27 15:36:54 +08:00
parent 4063fef48a
commit a19bd1eef1

View File

@ -1,24 +1,29 @@
# Generated by Django 5.2.4 on 2025-08-11 09:45
from django.db import migrations, models
from django.db import migrations, models, connection
from django.db.models import Q
def add_allow_download_to_existing_documents(apps, schema_editor):
Document = apps.get_model('knowledge', 'Document')
# 为所有现有的Document记录添加allow_download=True
documents = Document.objects.filter(
Q(meta__isnull=True) |
~Q(meta__has_key='allow_download')
)
# 使用原生SQL进行批量更新避免加载大量对象到内存
with connection.cursor() as cursor:
# 为meta为null的记录设置初始值
cursor.execute("""
UPDATE document
SET meta = '{"allow_download": true}'::jsonb
WHERE meta IS NULL
""")
# 为meta不包含allow_download键的记录添加该字段
cursor.execute("""
UPDATE document
SET meta = meta || '{"allow_download": true}'::jsonb
WHERE meta IS NOT NULL
AND NOT (meta ? 'allow_download')
""")
for doc in documents:
if doc.meta is None:
doc.meta = {}
doc.meta['allow_download'] = True
doc.save(update_fields=['meta'])
class Migration(migrations.Migration):