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

allow defining schemas from environment variable #50

Merged
merged 1 commit into from
Aug 4, 2022
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
28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ jobs:
- name: Run Benchmark
run: python -m pytest tests/benchmarks.py --benchmark-only --benchmark-columns 'min, max, mean, median' --benchmark-json output.json --asyncio-mode=strict

- name: Store and benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
name: TiFeatures Benchmarks
tool: 'pytest'
output-file-path: output.json
alert-threshold: '130%'
comment-on-alert: true
fail-on-alert: true
# GitHub API token to make a commit comment
github-token: ${{ secrets.GITHUB_TOKEN }}
# Make a commit on `gh-pages` only if master
auto-push: ${{ github.ref == 'refs/heads/master' }}
benchmark-data-dir-path: benchmarks
# - name: Store and benchmark result
# uses: benchmark-action/github-action-benchmark@v1
# with:
# name: TiFeatures Benchmarks
# tool: 'pytest'
# output-file-path: output.json
# alert-threshold: '130%'
# comment-on-alert: true
# fail-on-alert: true
# # GitHub API token to make a commit comment
# github-token: ${{ secrets.GITHUB_TOKEN }}
# # Make a commit on `gh-pages` only if master
# auto-push: ${{ github.ref == 'refs/heads/master' }}
# benchmark-data-dir-path: benchmarks

publish:
needs: [tests]
Expand Down
6 changes: 3 additions & 3 deletions tifeatures/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""tifeatures.db: database events."""

from typing import Optional
from typing import Any, Optional

from buildpg import asyncpg

Expand Down Expand Up @@ -46,9 +46,9 @@ async def connect_to_db(
)


async def register_table_catalog(app: FastAPI) -> None:
async def register_table_catalog(app: FastAPI, **kwargs: Any) -> None:
"""Register Table catalog."""
app.state.table_catalog = await get_table_index(app.state.pool)
app.state.table_catalog = await get_table_index(app.state.pool, **kwargs)


async def close_db_connection(app: FastAPI) -> None:
Expand Down
11 changes: 8 additions & 3 deletions tifeatures/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tifeatures.factory import Endpoints
from tifeatures.layer import FunctionRegistry
from tifeatures.middleware import CacheControlMiddleware
from tifeatures.settings import APISettings
from tifeatures.settings import APISettings, PostgresSettings

from fastapi import FastAPI

Expand All @@ -19,6 +19,7 @@
from starlette_cramjam.middleware import CompressionMiddleware

settings = APISettings()
postgres_settings = PostgresSettings()

app = FastAPI(
title=settings.name,
Expand Down Expand Up @@ -66,8 +67,12 @@
@app.on_event("startup")
async def startup_event() -> None:
"""Connect to database on startup."""
await connect_to_db(app)
await register_table_catalog(app)
await connect_to_db(app, settings=postgres_settings)
await register_table_catalog(
app,
schemas=postgres_settings.db_schemas,
tables=postgres_settings.db_tables,
)


@app.on_event("shutdown")
Expand Down
5 changes: 4 additions & 1 deletion tifeatures/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""tifeatures config."""

from functools import lru_cache
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional

import pydantic

Expand Down Expand Up @@ -56,6 +56,9 @@ class PostgresSettings(pydantic.BaseSettings):
db_max_queries: int = 50000
db_max_inactive_conn_lifetime: float = 300

db_schemas: List[str] = ["public"]
db_tables: Optional[List[str]]

class Config:
"""model config"""

Expand Down