Skip to content

Commit a19bd1e

Browse files
committed
chore: add allow_download field to existing Document records
--bug=1060848 --user=刘瑞斌 【知识库】文档设置[允许知识库来源中下载 ]应该默认开启 https://www.tapd.cn/62980211/s/1762365
1 parent 4063fef commit a19bd1e

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

apps/knowledge/migrations/0002_alter_file_source_type.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
# Generated by Django 5.2.4 on 2025-08-11 09:45
22

3-
from django.db import migrations, models
3+
from django.db import migrations, models, connection
4+
45

5-
from django.db.models import Q
66

77

88
def add_allow_download_to_existing_documents(apps, schema_editor):
9-
Document = apps.get_model('knowledge', 'Document')
10-
11-
# 为所有现有的Document记录添加allow_download=True
12-
documents = Document.objects.filter(
13-
Q(meta__isnull=True) |
14-
~Q(meta__has_key='allow_download')
15-
)
16-
17-
for doc in documents:
18-
if doc.meta is None:
19-
doc.meta = {}
20-
doc.meta['allow_download'] = True
21-
doc.save(update_fields=['meta'])
9+
10+
# 使用原生SQL进行批量更新,避免加载大量对象到内存
11+
with connection.cursor() as cursor:
12+
# 为meta为null的记录设置初始值
13+
cursor.execute("""
14+
UPDATE document
15+
SET meta = '{"allow_download": true}'::jsonb
16+
WHERE meta IS NULL
17+
""")
18+
19+
# 为meta不包含allow_download键的记录添加该字段
20+
cursor.execute("""
21+
UPDATE document
22+
SET meta = meta || '{"allow_download": true}'::jsonb
23+
WHERE meta IS NOT NULL
24+
AND NOT (meta ? 'allow_download')
25+
""")
26+
2227

2328
class Migration(migrations.Migration):
2429

0 commit comments

Comments
 (0)