From 0e1ae663b137494a1aab5eadb30b5679d96cfc0e Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 19 Aug 2025 13:55:10 +0800 Subject: [PATCH] fix: add migration to refresh collation and reindex database --- .../0011_refresh_collation_reindex.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 apps/setting/migrations/0011_refresh_collation_reindex.py diff --git a/apps/setting/migrations/0011_refresh_collation_reindex.py b/apps/setting/migrations/0011_refresh_collation_reindex.py new file mode 100644 index 000000000..cfef5f113 --- /dev/null +++ b/apps/setting/migrations/0011_refresh_collation_reindex.py @@ -0,0 +1,26 @@ +from django.db import migrations + + +def refresh_collation_and_reindex(apps, schema_editor): + # 获取当前数据库名 + db_name = schema_editor.connection.settings_dict["NAME"] + with schema_editor.connection.cursor() as cursor: + cursor.execute(f'ALTER DATABASE "{db_name}" REFRESH COLLATION VERSION;') + cursor.execute(f'REINDEX DATABASE "{db_name}";') + + +def noop(apps, schema_editor): + # 不可逆操作,留空 + pass + + +class Migration(migrations.Migration): + atomic = False # ALTER DATABASE/REINDEX 需在事务外执行 + + dependencies = [ + ('setting', '0010_log'), + ] + + operations = [ + migrations.RunPython(refresh_collation_and_reindex, reverse_code=noop), + ] \ No newline at end of file