Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to use a schema other than pg_temp for installing catalog functions. #191

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 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"]

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

catalog: Dict[str, Collection] = {}
table_settings = TableSettings()
table_confs = table_settings.table_config
Expand All @@ -945,7 +950,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.split(".").pop() == "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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to think about the use case for the custom function to have access the the pg_temp 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", pattern=r"[a-zA-Z][a-zA-Z0-9_-]*")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • can you move this up, before the model_config
  • what do you think about TIPG_SCHEMA_NAME ?


# 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
Loading