Skip to content

Commit

Permalink
Merge pull request #50 from OCHA-DAP/bugfix/enum-fixes
Browse files Browse the repository at this point in the history
PR Fix for using enum value in postgres
  • Loading branch information
turnerm authored May 29, 2024
2 parents 94facf6 + 4fe2bcc commit 9b183c8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 37 deletions.
26 changes: 18 additions & 8 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.8.9

### Fixed

- Some enums missing built from values

## 0.8.8

### Changes
### Changed

- Rewrite enums to use values rather than variable names
- Switch from "*" to "all" for rollup in enums
Expand All @@ -16,24 +22,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.8.7

## Changes
### Changed

- Add unit to food\_price table primary key

## 0.8.6

## Changes
### Changed

- Added food\_price view as tables by updating the toml parameter file
- Updated poverty\_rate view as table
- Updated operational\_presence view as table by updating the toml parameter file
- Made a few adhoc changes to the view as table generator code to accommodate Decimal columns

## 0.8.5

## Changes
### Changed

- refactor DBPovertyRate to wide instead of narrow
- added org\_type\_description to operational\_presence\_view

## Removed

- rate\_constraint and `PovertyClassification` enum

## 0.8.4
Expand All @@ -42,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- New constraints: rate\_constraint

### CHanged
### Changed

- refactor DBPovertyRate to track rates rather than population, and link to
location (with admin1\_name as a text field)
Expand All @@ -60,19 +70,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Miscellaneous fixes for pipelines

### Changed
- added from\_cods column to location, admin1, and admin2

- added from\_cods column to location, admin1, and admin2

## 0.8.1

Implemented the Food Prices subcategory.

### Added

- New tables and views: food\_price, wfp\_commodity, wfp\_market, currency
- New enums: PriceFlag, PriceType, CommodityCategory
- New constraints: non\_negative\_constraint, latlon\_constraint


## 0.8.0

### Changed
Expand All @@ -85,6 +95,7 @@ Implemented the Food Prices subcategory.
- Programmatically obtain views

### Added

- New tables: humanitarian\_needs, funding, refugees, conflict\_event, poverty\_rate
- New enums: Gender, DisabledMarker, EventType, PopulationGroup, PopulationStatus, IPCPhase, PovertyClassification, IPCType, and RiskClass
- Generalized constraints
Expand All @@ -93,7 +104,6 @@ Implemented the Food Prices subcategory.

- ipc\_phase, ipc\_type, age\_range, and gender tables


## [0.7.3]

### Fixed
Expand Down
11 changes: 7 additions & 4 deletions src/hapi_schema/db_food_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
String,
select,
Expand All @@ -22,7 +21,11 @@
non_negative_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import PriceFlag, PriceType
from hapi_schema.utils.enums import (
PriceFlag,
PriceType,
build_enum_using_values,
)
from hapi_schema.utils.view_params import ViewParams

# normalised table
Expand Down Expand Up @@ -52,10 +55,10 @@ class DBFoodPrice(Base):
)
unit: Mapped[str] = mapped_column(String(32), primary_key=True)
price_flag: Mapped[PriceFlag] = mapped_column(
Enum(PriceFlag), primary_key=True
build_enum_using_values(PriceFlag), primary_key=True
)
price_type: Mapped[PriceType] = mapped_column(
Enum(PriceType), primary_key=True
build_enum_using_values(PriceType), primary_key=True
)
price: Mapped[Decimal] = mapped_column(nullable=False)
reference_period_start: Mapped[datetime] = mapped_column(
Expand Down
9 changes: 5 additions & 4 deletions src/hapi_schema/db_food_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from sqlalchemy import (
DateTime,
Enum,
Float,
ForeignKey,
Integer,
Expand All @@ -22,7 +21,7 @@
population_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import IPCPhase, IPCType
from hapi_schema.utils.enums import IPCPhase, IPCType, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams


Expand All @@ -41,9 +40,11 @@ class DBFoodSecurity(Base):
ForeignKey("admin2.id", onupdate="CASCADE"), primary_key=True
)
ipc_phase: Mapped[IPCPhase] = mapped_column(
Enum(IPCPhase), primary_key=True
build_enum_using_values(IPCPhase), primary_key=True
)
ipc_type: Mapped[IPCType] = mapped_column(
build_enum_using_values(IPCType), primary_key=True
)
ipc_type: Mapped[IPCType] = mapped_column(Enum(IPCType), primary_key=True)
population_in_phase: Mapped[int] = mapped_column(
Integer, nullable=False, index=True
)
Expand Down
12 changes: 7 additions & 5 deletions src/hapi_schema/db_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
Integer,
String,
Expand All @@ -28,6 +27,7 @@
Gender,
PopulationGroup,
PopulationStatus,
build_enum_using_values,
)
from hapi_schema.utils.view_params import ViewParams

Expand All @@ -49,7 +49,9 @@ class DBHumanitarianNeeds(Base):
ForeignKey("admin2.id", onupdate="CASCADE"),
primary_key=True,
)
gender: Mapped[Gender] = mapped_column(Enum(Gender), primary_key=True)
gender: Mapped[Gender] = mapped_column(
build_enum_using_values(Gender), primary_key=True
)
age_range: Mapped[str] = mapped_column(String(32), primary_key=True)
min_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
max_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
Expand All @@ -58,17 +60,17 @@ class DBHumanitarianNeeds(Base):
primary_key=True,
)
population_group: Mapped[PopulationGroup] = mapped_column(
Enum(PopulationGroup),
build_enum_using_values(PopulationGroup),
primary_key=True,
)

population_status: Mapped[PopulationStatus] = mapped_column(
Enum(PopulationStatus),
build_enum_using_values(PopulationStatus),
primary_key=True,
)

disabled_marker: Mapped[DisabledMarker] = mapped_column(
Enum(DisabledMarker), primary_key=True
build_enum_using_values(DisabledMarker), primary_key=True
)
population: Mapped[int] = mapped_column(Integer, nullable=False)
reference_period_start: Mapped[datetime] = mapped_column(
Expand Down
5 changes: 2 additions & 3 deletions src/hapi_schema/db_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from sqlalchemy import (
CheckConstraint,
DateTime,
Enum,
Float,
ForeignKey,
Integer,
Expand All @@ -20,7 +19,7 @@
general_risk_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import RiskClass
from hapi_schema.utils.enums import RiskClass, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams


Expand Down Expand Up @@ -54,7 +53,7 @@ class DBNationalRisk(Base):
ForeignKey("location.id", onupdate="CASCADE"), primary_key=True
)
risk_class: Mapped[RiskClass] = mapped_column(
Enum(RiskClass), nullable=False
build_enum_using_values(RiskClass), nullable=False
)
global_rank: Mapped[int] = mapped_column(Integer, nullable=False)
overall_risk: Mapped[Decimal] = mapped_column(Float, nullable=False)
Expand Down
7 changes: 4 additions & 3 deletions src/hapi_schema/db_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
Integer,
String,
Expand All @@ -22,7 +21,7 @@
population_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import Gender
from hapi_schema.utils.enums import Gender, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams


Expand All @@ -43,7 +42,9 @@ class DBPopulation(Base):
ForeignKey("admin2.id", onupdate="CASCADE"),
primary_key=True,
)
gender: Mapped[Gender] = mapped_column(Enum(Gender), primary_key=True)
gender: Mapped[Gender] = mapped_column(
build_enum_using_values(Gender), primary_key=True
)
age_range: Mapped[str] = mapped_column(String(32), primary_key=True)
min_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
max_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
Expand Down
13 changes: 9 additions & 4 deletions src/hapi_schema/db_refugees.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
Integer,
String,
Expand All @@ -23,7 +22,11 @@
population_constraint,
reference_period_constraint,
)
from hapi_schema.utils.enums import Gender, PopulationGroup
from hapi_schema.utils.enums import (
Gender,
PopulationGroup,
build_enum_using_values,
)
from hapi_schema.utils.view_params import ViewParams


Expand All @@ -48,9 +51,11 @@ class DBRefugees(Base):
)
# population_group is broader than we need, but it will do
population_group: Mapped[PopulationGroup] = mapped_column(
Enum(PopulationGroup), primary_key=True
build_enum_using_values(PopulationGroup), primary_key=True
)
gender: Mapped[Gender] = mapped_column(
build_enum_using_values(Gender), primary_key=True
)
gender: Mapped[Gender] = mapped_column(Enum(Gender), primary_key=True)
age_range: Mapped[str] = mapped_column(String(32), primary_key=True)
min_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
max_age: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
Expand Down
6 changes: 3 additions & 3 deletions src/hapi_schema/db_wfp_commodity.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""wfp_commodity table and view."""

from sqlalchemy import Enum, String, select
from sqlalchemy import String, select
from sqlalchemy.orm import Mapped, mapped_column

from hapi_schema.utils.base import Base
from hapi_schema.utils.enums import CommodityCategory
from hapi_schema.utils.enums import CommodityCategory, build_enum_using_values
from hapi_schema.utils.view_params import ViewParams


Expand All @@ -13,7 +13,7 @@ class DBWFPCommodity(Base):

code: Mapped[str] = mapped_column(String(32), primary_key=True)
category: Mapped[CommodityCategory] = mapped_column(
Enum(CommodityCategory), index=True
build_enum_using_values(CommodityCategory), index=True
)
name: Mapped[str] = mapped_column(String(512), nullable=False, index=True)

Expand Down
6 changes: 3 additions & 3 deletions src/hapi_schema/utils/enums.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from enum import Enum as PythonEnum
from typing import List
from typing import List, Type

from sqlalchemy import Enum as SQLAlchemyEnum

"""The two functions below create enums using the values rather than the keys"""


def _get_list_of_values(enum_class: PythonEnum) -> List[str]:
def _get_list_of_values(enum_class: Type[PythonEnum]) -> List[str]:
return [e.value for e in enum_class]


def build_enum_using_values(enum_class: PythonEnum) -> SQLAlchemyEnum:
def build_enum_using_values(enum_class: Type[PythonEnum]) -> SQLAlchemyEnum:
return SQLAlchemyEnum(enum_class, values_callable=_get_list_of_values)


Expand Down

0 comments on commit 9b183c8

Please sign in to comment.