fix: docker-compose 持久化 SQLite 数据库; 迁移 DB 路径到 data/ 子目录

- docker-compose.yml: bind mount → named volume (auv_data:/app/data)
- backend/database.py: DB_PATH 改为 data/stock_data.db,自动迁移旧数据库
- .gitignore: 忽略 *.db 文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Sakurasan
2026-07-06 23:59:25 +08:00
co-authored by Claude Opus 4.7
parent 6390545e61
commit 5153fa28de
4 changed files with 19 additions and 1 deletions
+5
View File
@@ -7,6 +7,11 @@
*.bak *.bak
*.orig *.orig
# 数据库文件
*.db
*.sqlite
*.sqlite3
# 环境变量与密钥 # 环境变量与密钥
.env .env
.env.local .env.local
+9 -1
View File
@@ -1,7 +1,7 @@
import sqlite3 import sqlite3
import os 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 = """ SCHEMA_SQL = """
CREATE TABLE IF NOT EXISTS stock_collections ( CREATE TABLE IF NOT EXISTS stock_collections (
@@ -41,6 +41,7 @@ CREATE TABLE IF NOT EXISTS cache (
def get_connection() -> sqlite3.Connection: def get_connection() -> sqlite3.Connection:
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
conn = sqlite3.connect(DB_PATH) conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA foreign_keys = ON")
@@ -48,6 +49,13 @@ def get_connection() -> sqlite3.Connection:
def init_db(): def init_db():
# 迁移旧版 DBbackend/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 = get_connection()
conn.executescript(SCHEMA_SQL) conn.executescript(SCHEMA_SQL)
Binary file not shown.
+5
View File
@@ -7,3 +7,8 @@ services:
env_file: env_file:
- .env - .env
restart: unless-stopped restart: unless-stopped
volumes:
- auv_data:/app/data
volumes:
auv_data: