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:
co-authored by
Claude Opus 4.7
parent
6390545e61
commit
5153fa28de
@@ -7,6 +7,11 @@
|
||||
*.bak
|
||||
*.orig
|
||||
|
||||
# 数据库文件
|
||||
*.db
|
||||
*.sqlite
|
||||
*.sqlite3
|
||||
|
||||
# 环境变量与密钥
|
||||
.env
|
||||
.env.local
|
||||
|
||||
+9
-1
@@ -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)
|
||||
|
||||
|
||||
Binary file not shown.
@@ -7,3 +7,8 @@ services:
|
||||
env_file:
|
||||
- .env
|
||||
restart: unless-stopped
|
||||
volumes:
|
||||
- auv_data:/app/data
|
||||
|
||||
volumes:
|
||||
auv_data:
|
||||
|
||||
Reference in New Issue
Block a user