Skip to content

Commit 86c882c

Browse files
committed
Add session management utilities for database operations
Introduces `get_session_factory` and `session_scope` context manager for handling database connections and transactions.
1 parent b5d7b72 commit 86c882c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/open_cinema_index/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
from contextlib import contextmanager
2+
13
import typer
4+
from sqlalchemy import create_engine
5+
from sqlalchemy.orm import sessionmaker
26

37
app = typer.Typer(
48
name="oci",
59
help="Open Cinema Index — film ingestion and indexing toolkit",
610
)
711

812

13+
def get_session_factory():
14+
engine = create_engine("sqlite:///open_cinema_index.db")
15+
return sessionmaker(bind=engine)
16+
17+
18+
@contextmanager
19+
def session_scope():
20+
session_factory = get_session_factory()
21+
session = session_factory()
22+
try:
23+
yield session
24+
session.commit()
25+
except Exception:
26+
session.rollback()
27+
raise
28+
finally:
29+
session.close()
30+
31+
932
@app.command()
1033
def fetch(
1134
source: str = typer.Argument(..., help="Source name (e.g. tmdb, imdb)"),

0 commit comments

Comments
 (0)