chore: add allow_download field to existing Document records
--bug=1060848 --user=刘瑞斌 【知识库】文档设置[允许知识库来源中下载 ]应该默认开启 https://www.tapd.cn/62980211/s/1762365v3.2
parent
4063fef48a
commit
a19bd1eef1
|
|
@ -1,24 +1,29 @@
|
||||||
# Generated by Django 5.2.4 on 2025-08-11 09:45
|
# 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):
|
def add_allow_download_to_existing_documents(apps, schema_editor):
|
||||||
Document = apps.get_model('knowledge', 'Document')
|
|
||||||
|
|
||||||
# 为所有现有的Document记录添加allow_download=True
|
# 使用原生SQL进行批量更新,避免加载大量对象到内存
|
||||||
documents = Document.objects.filter(
|
with connection.cursor() as cursor:
|
||||||
Q(meta__isnull=True) |
|
# 为meta为null的记录设置初始值
|
||||||
~Q(meta__has_key='allow_download')
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue