9c37c9a574
Phase 4: org_id 注入 JWT/API 过滤/组织管理 CRUD/前端组织列 测试: tests/test_phase_upgrades.py 97项全覆盖 CSS: theme-modern.css 共享 mobile-card-list/status-dot/search-bar 等模式 修复: initial_data.py LLM配置 NOT NULL 约束, TopicResponse 含 org_id
74 lines
3.0 KiB
Python
74 lines
3.0 KiB
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
import os
|
|
from pathlib import Path
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent.parent
|
|
|
|
USE_POSTGRES = os.getenv('USE_POSTGRES', 'true').lower() == 'true'
|
|
|
|
if USE_POSTGRES:
|
|
POSTGRES_CONFIG = {
|
|
'host': os.getenv('PG_HOST', '127.0.0.1'),
|
|
'port': os.getenv('PG_PORT', '5432'),
|
|
'database': os.getenv('PG_DATABASE', 'yzr_nr'),
|
|
'user': os.getenv('PG_USER', 'yzr_nr'),
|
|
'password': os.getenv('PG_PASSWORD', 'aTX3WKKnPfRnM5PC')
|
|
}
|
|
SQLALCHEMY_DATABASE_URL = (
|
|
f"postgresql://{POSTGRES_CONFIG['user']}:{POSTGRES_CONFIG['password']}"
|
|
f"@{POSTGRES_CONFIG['host']}:{POSTGRES_CONFIG['port']}/{POSTGRES_CONFIG['database']}"
|
|
)
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
pool_pre_ping=True,
|
|
pool_size=10,
|
|
max_overflow=20
|
|
)
|
|
else:
|
|
DATA_DIR = os.getenv('DATA_DIR', str(PROJECT_ROOT / 'data'))
|
|
os.makedirs(DATA_DIR, exist_ok=True)
|
|
DB_PATH = os.path.join(DATA_DIR, 'yzr.db')
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
|
|
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
def init_db():
|
|
Base.metadata.create_all(bind=engine)
|
|
from sqlalchemy import text
|
|
try:
|
|
with engine.connect() as conn:
|
|
# 迁移:为已有表添加列
|
|
conn.execute(text("ALTER TABLE users ADD COLUMN IF NOT EXISTS last_login TIMESTAMP"))
|
|
conn.execute(text("ALTER TABLE llm_configs ADD COLUMN IF NOT EXISTS provider VARCHAR DEFAULT 'opencode-go'"))
|
|
conn.execute(text("ALTER TABLE llm_configs ADD COLUMN IF NOT EXISTS base_url VARCHAR"))
|
|
conn.execute(text("ALTER TABLE llm_configs ADD COLUMN IF NOT EXISTS api_key VARCHAR"))
|
|
try:
|
|
conn.execute(text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS images JSON DEFAULT '{}'::json"))
|
|
except Exception:
|
|
conn.execute(text("ALTER TABLE articles ADD COLUMN IF NOT EXISTS images TEXT DEFAULT '{}'"))
|
|
for table, col, typ in [
|
|
("users", "org_id", "VARCHAR DEFAULT 'default'"),
|
|
("topics", "org_id", "VARCHAR DEFAULT 'default'"),
|
|
]:
|
|
try:
|
|
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS {col} {typ}"))
|
|
except Exception:
|
|
try:
|
|
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN {col} {typ}"))
|
|
except Exception:
|
|
pass
|
|
conn.commit()
|
|
except Exception:
|
|
pass # SQLite 不支持 IF NOT EXISTS,但 create_all 对 SQLite 够用,这里仅为 PostgreSQL 迁移
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|