-
Notifications
You must be signed in to change notification settings - Fork 2
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
Tifeatures exits with asyncpg.exceptions.UndefinedTableError #49
Comments
To be honest I'm not sure why users would like to have tifeatures up without any tables in the database. At least we should return a better error message (or allow empty db) 🤷 For now you can do this:
"""tifeatures app."""
from typing import Any, List
import jinja2
from tifeatures import __version__ as tifeatures_version
from tifeatures.db import close_db_connection, connect_to_db, register_table_catalog
from tifeatures.errors import DEFAULT_STATUS_CODES, add_exception_handlers
from tifeatures.factory import Endpoints
from tifeatures.layer import FunctionRegistry
from tifeatures.middleware import CacheControlMiddleware
from tifeatures.settings import APISettings
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.templating import Jinja2Templates
from starlette_cramjam.middleware import CompressionMiddleware
from starlette.requests import Request
settings = APISettings()
app = FastAPI(
title=settings.name,
version=tifeatures_version,
openapi_url="/api",
docs_url="/api.html",
)
# custom template directory
templates_location: List[Any] = (
[jinja2.FileSystemLoader(settings.template_directory)]
if settings.template_directory
else []
)
# default template directory
templates_location.append(jinja2.PackageLoader(__package__, "templates"))
templates = Jinja2Templates(
directory="", # we need to set a dummy directory variable, see https://github.com/encode/starlette/issues/1214
loader=jinja2.ChoiceLoader(templates_location),
)
# Register endpoints.
endpoints = Endpoints(title=settings.name, templates=templates)
app.include_router(endpoints.router)
# We add the function registry to the application state
app.state.tifeatures_function_catalog = FunctionRegistry()
# Set all CORS enabled origins
if settings.cors_origins:
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["GET"],
allow_headers=["*"],
)
app.add_middleware(CacheControlMiddleware, cachecontrol=settings.cachecontrol)
app.add_middleware(CompressionMiddleware)
add_exception_handlers(app, DEFAULT_STATUS_CODES)
@app.on_event("startup")
async def startup_event() -> None:
"""Connect to database on startup."""
await connect_to_db(app)
try:
await register_table_catalog(app)
except:
app.state.table_catalog = {}
@app.get("/register", include_in_schema=False)
async def register_table(request: Request):
await register_table_catalog(request.app)
@app.on_event("shutdown")
async def shutdown_event() -> None:
"""Close database connection."""
await close_db_connection(app)
@app.get("/healthz", description="Health Check", tags=["Health Check"])
def ping():
"""Health check."""
return {"ping": "pong!"} |
@rodrigoalmeida94 I'm not sure about the statement
I'm not able to recreate the issue when pointing the application to 🤷 |
Interesting! That was my original assumption, which is wrong. @vincentsarago could it be it's about a database that doesn't have postgis installed (not that it is empty?) |
When connecting to an empty database (i.e. no tables) tifeatures returns the following exception:
In a lot of deployment cases, the DB is deployed and is empty (and could be that the tifeatures deployment occurs at the same time) so I would think it makes sense to fail silently in these cases.
The text was updated successfully, but these errors were encountered: