Skip to content

Commit

Permalink
add setting to be able to install tipg functions outside of the pg_te…
Browse files Browse the repository at this point in the history
…mp schema
  • Loading branch information
bitner committed Sep 16, 2024
1 parent b617fa9 commit f30ba2d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
16 changes: 11 additions & 5 deletions tipg/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
from tipg.filter.filters import bbox_to_wkt
from tipg.logger import logger
from tipg.model import Extent
from tipg.settings import FeaturesSettings, MVTSettings, TableConfig, TableSettings
from tipg.settings import (
FeaturesSettings,
MVTSettings,
PostgresSettings,
TableConfig,
TableSettings,
)

from fastapi import FastAPI

Expand Down Expand Up @@ -904,9 +910,9 @@ async def get_collection_index( # noqa: C901
) -> Catalog:
"""Fetch Table and Functions index."""
schemas = schemas or ["public"]

PostgresSettings()
query = """
SELECT pg_temp.tipg_catalog(
SELECT {pg_settings.tipg_schema}.tipg_catalog(
:schemas,
:tables,
:exclude_tables,
Expand Down Expand Up @@ -934,7 +940,7 @@ async def get_collection_index( # noqa: C901
spatial_extent=spatial_extent,
datetime_extent=datetime_extent,
)

PostgresSettings()
catalog: Dict[str, Collection] = {}
table_settings = TableSettings()
table_confs = table_settings.table_config
Expand All @@ -945,7 +951,7 @@ async def get_collection_index( # noqa: C901
table_id = table["schema"] + "." + table["name"]
confid = table["schema"] + "_" + table["name"]

if table_id == "pg_temp.tipg_catalog":
if table_id == "{pg_settings.tipg_schema}.tipg_catalog":
continue

table_conf = table_confs.get(confid, TableConfig())
Expand Down
11 changes: 8 additions & 3 deletions tipg/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(

async def __call__(self, conn: asyncpg.Connection):
"""Create connection."""
settings = PostgresSettings()
await conn.set_type_codec(
"json", encoder=orjson.dumps, decoder=orjson.loads, schema="pg_catalog"
)
Expand All @@ -46,7 +47,7 @@ async def __call__(self, conn: asyncpg.Connection):

# Note: we add `pg_temp as the first element of the schemas list to make sure
# we register the custom functions and `dbcatalog` in it.
schemas = ",".join(["pg_temp", *self.schemas])
schemas = ",".join([settings.tipg_schema, *self.schemas])
logger.debug(f"Looking for Tables and Functions in {schemas} schemas")

await conn.execute(
Expand All @@ -61,10 +62,14 @@ async def __call__(self, conn: asyncpg.Connection):

# Register custom SQL functions/table/views in pg_temp
for sqlfile in self.user_sql_files:
await conn.execute(sqlfile.read_text())
await conn.execute(
sqlfile.read_text().replace("pg_temp", settings.tipg_schema)
)

# Register TiPG functions in `pg_temp`
await conn.execute(DB_CATALOG_FILE.read_text())
await conn.execute(
DB_CATALOG_FILE.read_text().replace("pg_temp", settings.tipg_schema)
)


async def connect_to_db(
Expand Down
2 changes: 2 additions & 0 deletions tipg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class PostgresSettings(BaseSettings):

model_config = {"env_file": ".env", "extra": "ignore"}

tipg_schema: str = Field("pg_temp", regex=r"[a-zA-Z][a-zA-Z0-9_-]*")

# https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/core/config.py#L42
@field_validator("database_url", mode="before")
def assemble_db_connection(
Expand Down

0 comments on commit f30ba2d

Please sign in to comment.