補強 5.5 自癒安全回看
All checks were successful
CD Pipeline / deploy (push) Successful in 1m11s

This commit is contained in:
OoO
2026-04-29 22:48:24 +08:00
parent 779b27f676
commit 0875dd8fda
10 changed files with 217 additions and 66 deletions

View File

@@ -2,6 +2,7 @@ import os
import re
import threading
from sqlalchemy import create_engine, desc, select, text, literal
from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from .models import Base, Category, Product, PriceRecord, MonthlySummaryAnalysis
@@ -112,8 +113,14 @@ class DatabaseManager:
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
db_path = os.path.join(base_dir, 'data', 'momo_database.db')
os.makedirs(os.path.dirname(db_path), exist_ok=True)
self.engine = create_engine(f'sqlite:///{db_path}', echo=False)
if str(db_path).startswith('sqlite://'):
sqlite_db_file = make_url(db_path).database
if sqlite_db_file:
os.makedirs(os.path.dirname(sqlite_db_file), exist_ok=True)
self.engine = create_engine(db_path, echo=False)
else:
os.makedirs(os.path.dirname(db_path), exist_ok=True)
self.engine = create_engine(f'sqlite:///{db_path}', echo=False)
Base.metadata.create_all(self.engine)
self.Session = sessionmaker(bind=self.engine)
self._check_and_fix_schema()