Skip to content

add the ability to use a schema other than pg_temp for installing catalog functions #210

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

Merged
merged 1 commit into from
Mar 4, 2025
Merged
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
25 changes: 25 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
* update `tipg.collections.pg_get_collection_index` to return a list of PgCollection instead of a Catalog
* update `tipg.collections.register_collection_catalog` to pass `db_settings` to `pg_get_collection_index` function
* add `tilesets` and `viewer` links in `/collections` and `/collections/{collectionId}` response links
* add the ability to use a schema other than pg_temp for installing catalog functions (using `TIPG_DB_APPLICATION_SCHEMA` environment variable)
* change `database.connect_to_db` input order

```python
# Before
async def connect_to_db(
app: FastAPI,
settings: Optional[PostgresSettings] = None,
schemas: Optional[List[str]] = None,
user_sql_files: Optional[List[pathlib.Path]] = None,
**kwargs,
) -> None:

# Now
async def connect_to_db(
app: FastAPI,
*,
schemas: List[str],
tipg_schema: str = "pg_temp",
user_sql_files: Optional[List[pathlib.Path]] = None,
settings: Optional[PostgresSettings] = None,
**kwargs,
) -> None:
```


## [0.11.0] - TBD

Expand Down
16 changes: 14 additions & 2 deletions docs/src/advanced/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ async def lifespan(app: FastAPI):
"""
await connect_to_db(
app,
settings=PostgresSettings(database_url="postgres://...."),
schemas=["public"],
tipg_schema="pg_temp",
settings=PostgresSettings(database_url="postgres://...."),
)
await register_collection_catalog(
app,
db_settings=DatabaseSettings(schemas=["public"]),
db_settings=DatabaseSettings(schemas=["public"], application_schema="pg_temp"),
)

yield
Expand Down Expand Up @@ -133,3 +134,14 @@ pg_temp.hexagons
pg_temp.squares
pg_temp.landsat
```

### Custom schema for `tipg` catalog method

By default, when users start the `tipg` application, we will register some SQL function to the `pg_temp` schema. This schema might not always be available to the user deploying the application (e.g in AWS Aurora).

Starting with `tipg>=0.12`, users can use environment variable `TIPG_DB_APPLICATION_SCHEMA` to change the schema where `tipg` will register the catalog function.

!!! important

This schema must already exist, and the logged in user must have full permissions to the schema!).

6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ async def lifespan(app: FastAPI):
"""FastAPI Lifespan."""
await connect_to_db(
app,
settings=postgres_settings,
schemas=db_settings.schemas,
tipg_schema=db_settings.tipg_schema,
user_sql_files=sql_settings.sql_files,
settings=postgres_settings,
)
await register_collection_catalog(app, db_settings=db_settings)
yield
Expand Down Expand Up @@ -496,8 +497,9 @@ async def lifespan(app: FastAPI):
"""FastAPI Lifespan."""
await connect_to_db(
app,
settings=postgres_settings,
schemas=db_settings.schemas,
tipg_schema=db_settings.tipg_schema,
settings=postgres_settings,
)
await register_collection_catalog(app, db_settings=db_settings)
yield
Expand Down
6 changes: 3 additions & 3 deletions tipg/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,8 +931,8 @@ async def pg_get_collection_index( # noqa: C901

schemas = settings.schemas or ["public"]

query = """
SELECT pg_temp.tipg_catalog(
query = f"""
SELECT {settings.tipg_schema}.tipg_catalog(
:schemas,
:tables,
:exclude_tables,
Expand Down Expand Up @@ -971,7 +971,7 @@ async def pg_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 == f"{settings.tipg_schema}.tipg_catalog":
continue

table_conf = table_confs.get(confid, TableConfig())
Expand Down
29 changes: 18 additions & 11 deletions tipg/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ class connection_factory:
"""Connection creation."""

schemas: List[str]
tipg_schema: str
user_sql_files: List[pathlib.Path]

def __init__(
self,
schemas: Optional[List[str]] = None,
schemas: List[str],
tipg_schema: str,
user_sql_files: Optional[List[pathlib.Path]] = None,
) -> None:
"""Init."""
self.schemas = schemas or []
self.schemas = schemas
self.tipg_schema = tipg_schema
self.user_sql_files = user_sql_files or []

async def __call__(self, conn: asyncpg.Connection):
Expand All @@ -39,9 +42,9 @@ async def __call__(self, conn: asyncpg.Connection):
"jsonb", encoder=orjson.dumps, decoder=orjson.loads, schema="pg_catalog"
)

# Note: we add `pg_temp as the first element of the schemas list to make sure
# Note: we add `{tipg_schema}` 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([self.tipg_schema, *self.schemas])
logger.debug(f"Looking for Tables and Functions in {schemas} schemas")

await conn.execute(
Expand All @@ -54,27 +57,31 @@ async def __call__(self, conn: asyncpg.Connection):
"""
)

# Register custom SQL functions/table/views in pg_temp
# Register custom SQL functions/table/views in `{tipg_schema}`
for sqlfile in self.user_sql_files:
await conn.execute(sqlfile.read_text())

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


async def connect_to_db(
app: FastAPI,
settings: Optional[PostgresSettings] = None,
schemas: Optional[List[str]] = None,
*,
schemas: List[str],
tipg_schema: str = "pg_temp",
user_sql_files: Optional[List[pathlib.Path]] = None,
settings: Optional[PostgresSettings] = None,
**kwargs,
) -> None:
"""Connect."""
con_init = connection_factory(schemas, tipg_schema, user_sql_files)

if not settings:
settings = PostgresSettings()

con_init = connection_factory(schemas, user_sql_files)

app.state.pool = await asyncpg.create_pool_b(
str(settings.database_url),
min_size=settings.db_min_conn_size,
Expand Down
3 changes: 2 additions & 1 deletion tipg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ async def lifespan(app: FastAPI):
# Create Connection Pool
await connect_to_db(
app,
settings=postgres_settings,
schemas=db_settings.schemas,
tipg_schema=db_settings.tipg_schema,
user_sql_files=custom_sql_settings.sql_files,
settings=postgres_settings,
)

# Register Collection Catalog
Expand Down
1 change: 1 addition & 0 deletions tipg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class DatabaseSettings(BaseSettings):
"""TiPg Database settings."""

schemas: List[str] = ["public"]
tipg_schema: str = Field("pg_temp", alias="application_schema")
tables: Optional[List[str]] = None
exclude_tables: Optional[List[str]] = None
exclude_table_schemas: Optional[List[str]] = None
Expand Down
Loading