56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
"""
|
||
一次性迁移脚本:为已存在的 forms 表补充 announcement 列 + 创建公告附件表。
|
||
用法(在服务器项目根目录执行):
|
||
python scripts/migrate_announcement.py
|
||
脚本是幂等的,可重复运行。
|
||
"""
|
||
import os
|
||
import sys
|
||
|
||
# 确保能导入项目根目录的 db.py / database.py
|
||
_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
if _PROJECT_DIR not in sys.path:
|
||
sys.path.insert(0, _PROJECT_DIR)
|
||
|
||
from db import get_db_connection, is_sqlite, get_table_columns, table_exists
|
||
|
||
|
||
def main():
|
||
with get_db_connection() as conn:
|
||
cursor = conn.cursor()
|
||
|
||
# 1. 检查并补充 announcement 列
|
||
cols = get_table_columns(conn, 'forms')
|
||
names = [c['name'] for c in cols]
|
||
if 'announcement' not in names:
|
||
cursor.execute('ALTER TABLE forms ADD COLUMN announcement TEXT')
|
||
print("✅ 已为 forms 表添加 announcement 列")
|
||
else:
|
||
print("ℹ️ forms 表已有 announcement 列,跳过")
|
||
|
||
# 2. 检查并创建公告附件表
|
||
if not table_exists(conn, 'form_announcement_files'):
|
||
cursor.execute('''
|
||
CREATE TABLE IF NOT EXISTS form_announcement_files (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
form_id TEXT NOT NULL,
|
||
filename TEXT NOT NULL,
|
||
original_name TEXT,
|
||
file_size INTEGER,
|
||
mime_type TEXT,
|
||
file_data BLOB NOT NULL,
|
||
uploaded_at TEXT NOT NULL,
|
||
FOREIGN KEY (form_id) REFERENCES forms(id) ON DELETE CASCADE
|
||
)
|
||
''')
|
||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_announcement_files_form ON form_announcement_files(form_id)')
|
||
print("✅ 已创建 form_announcement_files 表")
|
||
else:
|
||
print("ℹ️ form_announcement_files 表已存在,跳过")
|
||
|
||
print("🎉 迁移完成")
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|