Some checks failed
CD Pipeline / deploy (push) Failing after 59s
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml) - 部署模式: rsync Python 檔案至 188 → docker restart (volume mount) - Dockerfile/requirements 變動時自動重建 Docker image - 部署通知: Telegram (開始/成功/失敗) - 健康檢查: https://mo.wooo.work/health (最多 5 次重試) - 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
import os
|
||
import sys
|
||
from sqlalchemy import inspect, text
|
||
|
||
# 將專案根目錄加入 sys.path 以便讀取模組
|
||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||
sys.path.insert(0, BASE_DIR)
|
||
|
||
try:
|
||
from database.manager import DatabaseManager
|
||
except ImportError:
|
||
print("❌ 無法導入 DatabaseManager,請確認您在專案根目錄下執行此腳本。")
|
||
sys.exit(1)
|
||
|
||
def clear_table():
|
||
"""清空指定的資料表"""
|
||
table_name = 'realtime_sales_monthly'
|
||
|
||
# V-Fix: 直接使用當前腳本路徑計算資料庫位置,確保與 app.py 一致 (修正路徑問題)
|
||
db_path = os.path.join(BASE_DIR, 'data', 'momo_database.db')
|
||
print(f"🚀 準備清空資料表: {table_name}")
|
||
print(f"📂 目標資料庫路徑: {db_path}")
|
||
|
||
db = DatabaseManager(db_path=db_path)
|
||
engine = db.engine
|
||
|
||
try:
|
||
inspector = inspect(engine)
|
||
if not inspector.has_table(table_name):
|
||
print(f"ℹ️ 資料表 '{table_name}' 不存在,無需清空。")
|
||
return
|
||
|
||
with engine.connect() as conn:
|
||
# 1. 查詢目前筆數
|
||
count = conn.execute(text(f"SELECT COUNT(*) FROM {table_name}")).scalar()
|
||
conn.commit() # 提交隱式交易,避免鎖定
|
||
|
||
if count == 0:
|
||
print("⚠️ 資料表已空,無需執行。")
|
||
return
|
||
|
||
print(f"📊 目前共有 {count} 筆資料。")
|
||
|
||
# 2. 確認刪除
|
||
confirm = input("⚠️ 警告:確定要【永久刪除 (DROP TABLE)】所有資料嗎?(y/n): ")
|
||
if confirm.lower() == 'y':
|
||
print("⏳ 正在執行刪除...")
|
||
|
||
# V-Fix: 安全修復邏輯
|
||
try:
|
||
# 1. 嘗試強制寫入 WAL 暫存檔,解決鎖定問題
|
||
conn.execute(text("PRAGMA wal_checkpoint(TRUNCATE)"))
|
||
|
||
# 2. 僅刪除指定的業績報表
|
||
with conn.begin():
|
||
conn.execute(text(f"DROP TABLE IF EXISTS {table_name}"))
|
||
|
||
# 3. 重整資料庫檔案 (釋放空間並修復可能的頁面錯誤)
|
||
conn.execute(text("VACUUM"))
|
||
|
||
print(f"✅ 成功刪除資料表 '{table_name}' 並完成資料庫重整。")
|
||
print("✅ 其他資料 (商品、價格紀錄) 均已保留。")
|
||
except Exception as db_err:
|
||
print(f"❌ 資料庫操作失敗: {db_err}")
|
||
print("👉 請務必先【關閉 app.py】再執行此腳本!")
|
||
return
|
||
|
||
print("==========================================")
|
||
print("⚠️ 重要提示:請務必【重新啟動 app.py】以清除網頁快取,否則網頁上可能仍會顯示舊資料!")
|
||
print("==========================================")
|
||
else:
|
||
print("已取消操作。")
|
||
|
||
except Exception as e:
|
||
print(f"❌ 發生錯誤: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
clear_table()
|