First Build alpha 0.1
This commit is contained in:
57
app/database.py
Normal file
57
app/database.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""Database setup and session management for NetBird MSP Appliance."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from typing import Generator
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.orm import Session, sessionmaker, declarative_base
|
||||
|
||||
DATABASE_PATH = os.environ.get("DATABASE_PATH", "/app/data/netbird_msp.db")
|
||||
DATABASE_URL = f"sqlite:///{DATABASE_PATH}"
|
||||
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
echo=False,
|
||||
)
|
||||
|
||||
# Enable WAL mode and foreign keys for SQLite
|
||||
@event.listens_for(engine, "connect")
|
||||
def _set_sqlite_pragma(dbapi_connection, connection_record) -> None:
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA journal_mode=WAL")
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
"""Yield a database session, ensuring it is closed after use."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
"""Create all database tables."""
|
||||
from app.models import ( # noqa: F401
|
||||
Customer,
|
||||
Deployment,
|
||||
DeploymentLog,
|
||||
SystemConfig,
|
||||
User,
|
||||
)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "init":
|
||||
init_db()
|
||||
print("Database initialized successfully.")
|
||||
Reference in New Issue
Block a user