-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
74 lines (59 loc) · 2.19 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, Text
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
import os
Base = declarative_base()
class Repository(Base):
__tablename__ = "repositories"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
path = Column(String)
url = Column(String, nullable=True)
last_updated = Column(DateTime, default=datetime.utcnow)
last_analyzed = Column(DateTime)
commits = relationship("Commit", back_populates="repository", cascade="all, delete-orphan")
class Commit(Base):
__tablename__ = "commits"
id = Column(Integer, primary_key=True, index=True)
repository_id = Column(Integer, ForeignKey("repositories.id", ondelete="CASCADE"))
hash = Column(String, index=True)
author = Column(String)
date = Column(DateTime)
message = Column(Text)
files_changed = Column(Integer)
insertions = Column(Integer)
deletions = Column(Integer)
repository = relationship("Repository", back_populates="commits")
# 全局数据库引擎和会话工厂
engine = None
SessionLocal = None
def init_db():
global engine, SessionLocal
# 如果已经初始化,直接返回
if engine is not None:
return engine, SessionLocal
# 根据环境选择数据库 URL
if os.environ.get('VERCEL_ENV') == 'production':
db_url = 'sqlite:///:memory:'
else:
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'gitscan.db')
db_url = f'sqlite:///{db_path}'
# 创建数据库引擎
engine = create_engine(
db_url,
connect_args={'check_same_thread': False}
)
# 创建会话工厂
session_factory = sessionmaker(
bind=engine,
autocommit=False,
autoflush=False
)
# 使用 scoped_session 来确保线程安全
SessionLocal = scoped_session(session_factory)
# 创建所有表
Base.metadata.create_all(bind=engine)
return engine, SessionLocal
# 初始化数据库
engine, SessionLocal = init_db()