feat: add database module
- Add SQLAlchemy database module with DeclarativeBase - Implement engine and session factory management - Add context manager for database sessions - Add database initialization script - Update models/__init__.py to import Base from database - Fix Python 3.8 compatibility issues (use Optional instead of |) - Ensure SQLite database file is created on init_db Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
40
backend/init_db.py
Normal file
40
backend/init_db.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
数据库初始化脚本.
|
||||
创建所有表和必要的目录.
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
|
||||
from app.config import get_settings
|
||||
from app.database import init_db
|
||||
from app.models import Base
|
||||
|
||||
|
||||
def main():
|
||||
"""初始化数据库."""
|
||||
settings = get_settings()
|
||||
print(f"初始化数据库: {settings.db_path}")
|
||||
|
||||
# 创建目录
|
||||
settings.data_dir.mkdir(parents=True, exist_ok=True)
|
||||
settings.ssh_keys_dir.mkdir(parents=True, exist_ok=True)
|
||||
settings.repos_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 初始化数据库
|
||||
init_db(settings.db_path)
|
||||
|
||||
# 创建所有表
|
||||
from app.database import get_engine
|
||||
Base.metadata.create_all(get_engine())
|
||||
|
||||
print("数据库初始化成功!")
|
||||
print(f" - 数据库: {settings.db_path}")
|
||||
print(f" - SSH 密钥: {settings.ssh_keys_dir}")
|
||||
print(f" - 仓库: {settings.repos_dir}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user