Skip to content
Snippets Groups Projects
Verified Commit f2af88d5 authored by Daniel Schultz's avatar Daniel Schultz :tm:
Browse files

Add PostgreSQL

We're going to need to use a database to store the state (and results)
of pdf conversions.
parent b16888ea
No related branches found
No related tags found
1 merge request!3Add `POST /conversions` endpoint
......@@ -4,3 +4,14 @@ S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}
S3_ENDPOINT=${S3_ENDPOINT:-"https://nyc3.digitaloceanspaces.com"}
S3_REGION=${S3_REGION:-"us-east-1"}
S3_BUCKET=${S3_BUCKET}
# Database setup
PGHOST=${PGHOST:-"localhost"}
PGUSER=${PGUSER:-"user"}
PGPASSWORD=${PGPASSWORD:-"password"}
PGDATABASE=${PGDATABASE:-"database"}
PGPORT=${PGPORT:-:"5432"}
## The schema being used for the application database.
## Migrations will be run against this schema.
APP_SCHEMA=${APP_SCHEMA:-"public"}
......@@ -15,6 +15,8 @@ lint:
- make lint
test:
services:
- postgres
stage: test
script:
- make test
......@@ -24,3 +26,8 @@ test:
S3_ENDPOINT: "https://example.com"
S3_REGION: "us-east-1"
S3_BUCKET: "example"
DATABASE_URL: "postgresql://test_user:password@localhost/test_database"
POSTGRES_DB: "test_database"
POSTGRES_USER: "test_user"
POSTGRES_PASSWORD: "password"
POSTGRES_HOST_AUTH_METHOD: trust
......@@ -6,6 +6,7 @@ This project requires:
- Python 3.10.x
- make
- PostgreSQL
## Dev setup
......
......@@ -3,3 +3,4 @@ pyright==1.1.368
pytest==8.2.2
pytest-mock==3.14.0
starlette==0.37.2
sqlalchemy-stubs==0.4
boto3==1.34.131
fastapi==0.111.0
psycopg2-binary==2.9.9
pydantic==2.7.4
pydantic-settings==2.3.3
SQLAlchemy==2.0.31
uvicorn==0.30.1
from sqlalchemy import Column, DateTime, create_engine, func, text
from sqlalchemy.engine.url import URL
from sqlalchemy.ext.declarative import declared_attr
# We need to ignore type checks on this line due to https://github.com/dropbox/sqlalchemy-stubs/issues/250
from sqlalchemy.orm import as_declarative, sessionmaker # type: ignore
from service.core.settings import settings
database_url = URL(
"postgresql",
host=settings.PGHOST,
username=settings.PGUSER,
password=settings.PGPASSWORD,
database=settings.PGDATABASE,
port=settings.PGPORT,
query={},
)
engine = create_engine(database_url)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@as_declarative()
class Base:
@declared_attr
def created_at(cls):
return Column(
DateTime(timezone=True), nullable=False, server_default=func.now()
)
@declared_attr
def updated_at(cls):
return Column(
DateTime(timezone=True), nullable=False, server_default=func.now()
)
def get_db():
db = SessionLocal()
db.execute(text(f"set search_path to {settings.APP_SCHEMA}"))
try:
yield db
finally:
db.close()
......@@ -7,6 +7,12 @@ class Settings(BaseSettings):
extra="allow",
)
APP_NAME: str = "PDF to Markdown Service"
PGHOST: str
PGUSER: str
PGPASSWORD: str
PGDATABASE: str
PGPORT: int = 5432
APP_SCHEMA: str = "public"
S3_BUCKET: str
S3_ACCESS_KEY_ID: str
S3_SECRET_ACCESS_KEY: str
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment