-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
48 lines (33 loc) · 1.47 KB
/
model.py
File metadata and controls
48 lines (33 loc) · 1.47 KB
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
import os
from dotenv import load_dotenv
from datetime import datetime, UTC
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
load_dotenv() # load env
DATA_DIR = os.getenv("DATA_DIR")
DB_URL = f"sqlite:////{DATA_DIR.strip('/')}/pdf_server.db"
Base = declarative_base()
class pdfs(Base):
__tablename__ = "pdfs"
id = Column(Integer, primary_key=True, index = True)
path = Column(String, unique = True, nullable = False)
created_at = Column(DateTime, default = datetime.now(UTC))
# to access logs
logs = relationship("changeLogs", back_populates="pdf", cascade="all, delete-orphan")
class changeLogs(Base):
__tablename__ = "change_logs"
id = Column(Integer, primary_key=True, index = True)
pdf_id = Column(Integer, ForeignKey("pdfs.id"), nullable= False)
temp_path = Column(String)
timestamp = Column(DateTime, default = datetime.now(UTC))
pdf = relationship("pdfs", back_populates="logs")
##### Engine and Table Creation #####
engine = create_engine(DB_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def init_db():
# This command checks if tables exist; if not, it creates them.
Base.metadata.create_all(bind=engine)
if __name__ == "__main__":
init_db()
print("Database initialized and tables created.")