Skip to content

Commit 0e1ae66

Browse files
committed
fix: add migration to refresh collation and reindex database
1 parent a10991a commit 0e1ae66

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.db import migrations
2+
3+
4+
def refresh_collation_and_reindex(apps, schema_editor):
5+
# 获取当前数据库名
6+
db_name = schema_editor.connection.settings_dict["NAME"]
7+
with schema_editor.connection.cursor() as cursor:
8+
cursor.execute(f'ALTER DATABASE "{db_name}" REFRESH COLLATION VERSION;')
9+
cursor.execute(f'REINDEX DATABASE "{db_name}";')
10+
11+
12+
def noop(apps, schema_editor):
13+
# 不可逆操作,留空
14+
pass
15+
16+
17+
class Migration(migrations.Migration):
18+
atomic = False # ALTER DATABASE/REINDEX 需在事务外执行
19+
20+
dependencies = [
21+
('setting', '0010_log'),
22+
]
23+
24+
operations = [
25+
migrations.RunPython(refresh_collation_and_reindex, reverse_code=noop),
26+
]

0 commit comments

Comments
 (0)