diff --git a/.gitignore b/.gitignore index 4e7ca7c..bf76f76 100755 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,11 @@ *.bak *.orig +# 数据库文件 +*.db +*.sqlite +*.sqlite3 + # 环境变量与密钥 .env .env.local diff --git a/backend/database.py b/backend/database.py index ae68e31..fa58cf2 100644 --- a/backend/database.py +++ b/backend/database.py @@ -1,7 +1,7 @@ import sqlite3 import os -DB_PATH = os.path.join(os.path.dirname(__file__), "stock_data.db") +DB_PATH = os.path.join(os.path.dirname(__file__), "data", "stock_data.db") SCHEMA_SQL = """ CREATE TABLE IF NOT EXISTS stock_collections ( @@ -41,6 +41,7 @@ CREATE TABLE IF NOT EXISTS cache ( def get_connection() -> sqlite3.Connection: + os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") @@ -48,6 +49,13 @@ def get_connection() -> sqlite3.Connection: def init_db(): + # 迁移旧版 DB(backend/stock_data.db → backend/data/stock_data.db) + old_path = os.path.join(os.path.dirname(__file__), "stock_data.db") + if os.path.exists(old_path) and os.path.isfile(old_path): + import shutil + shutil.move(old_path, DB_PATH) + print(f"[database] 已迁移 stock_data.db → data/stock_data.db") + conn = get_connection() conn.executescript(SCHEMA_SQL) diff --git a/backend/stock_data.db b/backend/stock_data.db deleted file mode 100644 index 156f1ff..0000000 Binary files a/backend/stock_data.db and /dev/null differ diff --git a/docker-compose.yml b/docker-compose.yml index 15655da..77af30a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,3 +7,8 @@ services: env_file: - .env restart: unless-stopped + volumes: + - auv_data:/app/data + +volumes: + auv_data: