File size: 1,016 Bytes
2685f20
77aa3fc
2685f20
6ca330f
2685f20
6ca330f
2685f20
 
7890908
 
77aa3fc
 
 
7890908
77aa3fc
 
 
 
 
7890908
 
 
 
77aa3fc
 
2685f20
 
 
 
 
 
 
 
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
import os
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker, scoped_session
from .config import DATA_DIR

DB_PATH = os.path.join(DATA_DIR, "sougouwiki.db")
DATABASE_URL = f"sqlite:///{DB_PATH}"

# Now that we run on local ephemeral disk (NOT tmpfs and NOT FUSE),
# we can safely use WAL mode for high performance.
engine = create_engine(
    DATABASE_URL, 
    echo=False,
    connect_args={"check_same_thread": False, "timeout": 30}
)

@event.listens_for(engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
    cursor = dbapi_connection.cursor()
    # WAL mode is perfect for "Crawler writing, API reading"
    cursor.execute("PRAGMA journal_mode=WAL")
    cursor.execute("PRAGMA synchronous=NORMAL")
    cursor.execute("PRAGMA busy_timeout=30000")
    cursor.close()

SessionLocal = scoped_session(sessionmaker(bind=engine))

def init_db():
    from .models import Base
    Base.metadata.create_all(engine)

def get_session():
    return SessionLocal()