Skip to content

Commit

Permalink
chore(deps): pydantic v2 and migration (#160)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Netherton <kevin.netherton@gov.bc.ca>
  • Loading branch information
DerekRoberts and franTarkenton authored Jun 18, 2024
1 parent 8b94dbc commit 94364d5
Show file tree
Hide file tree
Showing 8 changed files with 763 additions and 710 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ test-report.xml

# Python
.venv
**__pycache__
**.pyc
1,395 changes: 743 additions & 652 deletions backend-py/poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion backend-py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ python = "^3.11"
fastapi = "^0.111.0"
uvicorn = "^0.30.0"
sqlalchemy = "^2.0.21"
pydantic = {version ="^1.10.9", extras = ["email"]}
pydantic = {version ="^2.0.0", extras = ["email"]}
requests = "^2.31.0"
psycopg2 = "^2.9.1"
pydantic-settings = "^2.3.3"

[tool.poetry.dev-dependencies]
prospector = "^1.10.2"
Expand Down
16 changes: 10 additions & 6 deletions backend-py/src/core/config.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import os
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Union

from pydantic import BaseSettings, PostgresDsn, validator
from pydantic import field_validator, PostgresDsn
from pydantic_settings import BaseSettings


class Settings(BaseSettings):

API_V1_STR: str = "/api/v1"
POSTGRES_SERVER: str = os.getenv("POSTGRES_HOST", "127.0.0.1:5432")
POSTGRES_USER: str = os.getenv("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD: str = os.getenv("POSTGRES_PASSWORD", "postgres")
POSTGRES_DB: str = os.getenv("POSTGRES_DB", "postgres")
SQLALCHEMY_DATABASE_URI: Optional[PostgresDsn] = PostgresDsn.build(
SQLALCHEMY_DATABASE_URI: Union[Optional[PostgresDsn], Optional[str]] = PostgresDsn.build(
scheme="postgresql",
user=os.getenv("POSTGRES_USER", "postgres"),
username=os.getenv("POSTGRES_USER", "postgres"),
password=os.getenv("POSTGRES_PASSWORD", "postgres"),
host=os.getenv("POSTGRES_HOST", "127.0.0.1:5432"),
path=f"/{os.getenv('POSTGRES_DB', 'postgres')}",
)



@validator("SQLALCHEMY_DATABASE_URI", pre=True)
@field_validator("SQLALCHEMY_DATABASE_URI", mode="before")
@classmethod
def assemble_db_connection(v: Optional[str], values: Dict[str, Any]) -> Any:
if isinstance(v, str):
return v
return PostgresDsn.build(
scheme="postgresql",
user=values.get("POSTGRES_USER"),
username=values.get("POSTGRES_USER"),
password=values.get("POSTGRES_PASSWORD"),
host=values.get("POSTGRES_SERVER"),
path=f"/{values.get('POSTGRES_DB') or ''}",
Expand Down
2 changes: 1 addition & 1 deletion backend-py/src/db/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Generator
from src.core.config import Configuration

Engine = create_engine(Configuration.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True, pool_size=5, max_overflow=5,
Engine = create_engine(Configuration.SQLALCHEMY_DATABASE_URI.unicode_string(), pool_pre_ping=True, pool_size=5, max_overflow=5,
pool_recycle=120, pool_timeout=30)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=Engine)

Expand Down
46 changes: 0 additions & 46 deletions backend-py/src/v1/models/model.py
Original file line number Diff line number Diff line change
@@ -1,46 +0,0 @@
# coding: utf-8
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String, text
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class FlywaySchemaHistory(Base):
__tablename__ = 'flyway_schema_history'
__table_args__ = {'schema': 'py_api'}

installed_rank = Column(Integer, primary_key=True)
version = Column(String(50))
description = Column(String(200), nullable=False)
type = Column(String(20), nullable=False)
script = Column(String(1000), nullable=False)
checksum = Column(Integer)
installed_by = Column(String(100), nullable=False)
installed_on = Column(DateTime, nullable=False, server_default=text("now()"))
execution_time = Column(Integer, nullable=False)
success = Column(Boolean, nullable=False, index=True)


class User(Base):
__tablename__ = 'users'
__table_args__ = {'schema': 'py_api'}

id = Column(Integer, primary_key=True, server_default=text("nextval('py_api.user_id_seq'::regclass)"))
name = Column(String(50), nullable=False)
email = Column(String(50), nullable=False, unique=True)


class UserAddress(Base):
__tablename__ = 'user_addresses'
__table_args__ = {'schema': 'py_api'}

id = Column(Integer, primary_key=True, server_default=text("nextval('py_api.user_addresses_id_seq'::regclass)"))
street = Column(String(50), nullable=False)
city = Column(String(50), nullable=False)
state = Column(String(50), nullable=False)
zip_code = Column(String(10), nullable=False)
user_id = Column(ForeignKey('py_api.users.id', ondelete='CASCADE'), nullable=False)

user = relationship('User')
6 changes: 2 additions & 4 deletions backend-py/src/v1/structs/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Shared properties

from pydantic import BaseModel, EmailStr
from pydantic import ConfigDict, BaseModel, EmailStr


class UserBase(BaseModel):
Expand All @@ -15,6 +15,4 @@ class User(UserBase):

class UserInDBBase(UserBase):
id: int

class Config:
orm_mode = True
model_config = ConfigDict(from_attributes=True)
3 changes: 3 additions & 0 deletions backend-py/start-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ curl -sSL https://install.python-poetry.org | python3 -
export PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
poetry install --no-root -vvv --without dev --sync

# activate the env
source $(poetry env info --path)/bin/activate

# Start app
echo "Starting uvicorn at $(date +'%Y-%m-%d %H:%M:%S.%3N')"
uvicorn src.main:app --host 0.0.0.0 --port 3000 --workers 1 --server-header --date-header --limit-concurrency 100 --reload --log-config ./logger.conf || sleep 300

0 comments on commit 94364d5

Please sign in to comment.