Skip to content

Commit

Permalink
Merge pull request #6 from VaquitApp/add-postgres
Browse files Browse the repository at this point in the history
feat: add postgres integration
  • Loading branch information
MegaRedHand authored May 13, 2024
2 parents 5d97771 + 1cf5d57 commit da569a6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,32 @@ on:
pull_request:
branches: ["main", "develop"]

permissions:
contents: read
env:
DB_NAME: db
DB_USER: user
DB_PASSWORD: postgres
DB_HOST: localhost
DB_PORT: 5432

jobs:
ci:
runs-on: ubuntu-latest

services:
postgres:
image: postgres
env:
POSTGRES_DB: ${{ env.DB_NAME }}
POSTGRES_USER: ${{ env.DB_USER }}
POSTGRES_PASSWORD: ${{ env.DB_PASSWORD }}
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -38,4 +57,4 @@ jobs:
run: poetry install --no-interaction --no-root

- name: Run tests
run: make test
run: poetry run pytest -v .
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ lint:

test:
rm test.db 2> /dev/null || true
poetry run pytest -sv .
DB_NAME="./test.db" poetry run pytest -sv .

run: install
poetry run uvicorn src.main:app --host localhost --port 8000 --reload
25 changes: 24 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fastapi = "^0.111.0"
sqlalchemy = "^2.0.30"
uvicorn = "^0.29.0"
flake8 = "^7.0.0"
psycopg2 = "^2.9.9"


[tool.poetry.group.dev.dependencies]
Expand Down
33 changes: 27 additions & 6 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
from sqlalchemy import create_engine
from logging import info
import os
from sqlalchemy import create_engine, URL
from sqlalchemy.orm import DeclarativeBase, sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# SQLALCHEMY_DATABASE_URL = "postgresql://user:password@postgresserver/db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
DB_NAME = os.environ.get("DB_NAME", "./sql_app.db")
DB_USER = os.environ.get("DB_USER")
DB_PASS = os.environ.get("DB_PASSWORD")

# set automatically by kubernetes
# resolves to postgres service's host/port
DB_HOST = os.environ.get("DB_HOST")
DB_PORT = os.environ.get("DB_PORT")


SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_NAME}"

if DB_HOST is not None:
info("Using PostgreSQL database")
SQLALCHEMY_DATABASE_URL = URL.create(
"postgresql",
username=DB_USER,
password=DB_PASS,
host=DB_HOST,
port=DB_PORT,
database=DB_NAME,
)

engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


Expand Down
10 changes: 2 additions & 8 deletions src/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from .database import Base
from .database import Base, SQLALCHEMY_DATABASE_URL
from .main import app, get_db


SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"

engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
engine = create_engine(SQLALCHEMY_DATABASE_URL, poolclass=StaticPool)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


Expand Down

0 comments on commit da569a6

Please sign in to comment.