added vector_engine to fix name postgres name space issue
treefmt / nix fmt (pull_request) Successful in 6s
pytest / pytest (pull_request) Successful in 31s
build_systems / build-bob (pull_request) Successful in 47s
build_systems / build-brain (pull_request) Successful in 48s
build_systems / build-leviathan (pull_request) Successful in 56s
build_systems / build-rhapsody-in-green (pull_request) Successful in 1m0s
build_systems / build-jeeves (pull_request) Successful in 2m36s
treefmt / nix fmt (pull_request) Successful in 6s
pytest / pytest (pull_request) Successful in 31s
build_systems / build-bob (pull_request) Successful in 47s
build_systems / build-brain (pull_request) Successful in 48s
build_systems / build-leviathan (pull_request) Successful in 56s
build_systems / build-rhapsody-in-green (pull_request) Successful in 1m0s
build_systems / build-jeeves (pull_request) Successful in 2m36s
This commit is contained in:
@@ -31,7 +31,7 @@ logger = logging.getLogger(__name__)
|
|||||||
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
||||||
"""Manage application startup and shutdown resources."""
|
"""Manage application startup and shutdown resources."""
|
||||||
logger.info("ebook_search_startup")
|
logger.info("ebook_search_startup")
|
||||||
app.state.engine = get_postgres_engine(name="RICHIE")
|
app.state.engine = get_postgres_engine(name="RICHIE", vector_engine=True)
|
||||||
with Session(app.state.engine) as session:
|
with Session(app.state.engine) as session:
|
||||||
ensure_bm25_corpus(session, app.state.config)
|
ensure_bm25_corpus(session, app.state.config)
|
||||||
try:
|
try:
|
||||||
|
|||||||
+24
-2
@@ -31,8 +31,24 @@ def get_connection_info(name: str) -> tuple[str, str, str, str, str | None]:
|
|||||||
return cast("tuple[str, str, str, str, str | None]", (database, host, port, username, password))
|
return cast("tuple[str, str, str, str, str | None]", (database, host, port, username, password))
|
||||||
|
|
||||||
|
|
||||||
def get_postgres_engine(*, name: str = "POSTGRES", pool_pre_ping: bool = True) -> Engine:
|
def get_postgres_engine(
|
||||||
"""Create a SQLAlchemy engine from environment variables."""
|
*,
|
||||||
|
name: str = "POSTGRES",
|
||||||
|
pool_pre_ping: bool = True,
|
||||||
|
vector_engine: bool = False,
|
||||||
|
) -> Engine:
|
||||||
|
"""Create a SQLAlchemy engine from environment variables.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str, optional): The name of the environment variable prefix. Defaults to "POSTGRES".
|
||||||
|
pool_pre_ping (bool, optional): Whether to ping the database before each connection. Defaults to True.
|
||||||
|
This fixes the issue of trying to use a conection that has timed out on the database side.
|
||||||
|
vector_engine (bool, optional): Whether to use the vector search schema. Defaults to False.
|
||||||
|
This updates the search path the incldued the vecore types and operators.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Engine: The SQLAlchemy engine.
|
||||||
|
"""
|
||||||
database, host, port, username, password = get_connection_info(name)
|
database, host, port, username, password = get_connection_info(name)
|
||||||
|
|
||||||
url = URL.create(
|
url = URL.create(
|
||||||
@@ -44,8 +60,14 @@ def get_postgres_engine(*, name: str = "POSTGRES", pool_pre_ping: bool = True) -
|
|||||||
database=database,
|
database=database,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
connect_args = {}
|
||||||
|
# There more better way to do this is with separate PG account and a dedicated vector schema for the vector types
|
||||||
|
if vector_engine:
|
||||||
|
connect_args["options"] = "-csearch_path=main,public"
|
||||||
|
|
||||||
return create_engine(
|
return create_engine(
|
||||||
url=url,
|
url=url,
|
||||||
pool_pre_ping=pool_pre_ping,
|
pool_pre_ping=pool_pre_ping,
|
||||||
pool_recycle=1800,
|
pool_recycle=1800,
|
||||||
|
connect_args=connect_args,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user