Skip to content

Commit

Permalink
HAPI-347 schema and constraint updates
Browse files Browse the repository at this point in the history
  • Loading branch information
turnerm committed Mar 21, 2024
1 parent 286b3e9 commit 4f448eb
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 21 deletions.
17 changes: 17 additions & 0 deletions src/hapi_schema/db_admin1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Integer,
String,
UniqueConstraint,
select,
text,
)
Expand All @@ -20,6 +22,17 @@

class DBAdmin1(Base):
__tablename__ = "admin1"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
CheckConstraint(
"((hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_update_date)",
name="hapi_dates",
),
UniqueConstraint("code", "hapi_updated_date", name="code_date_unique"),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
location_ref: Mapped[int] = mapped_column(
Expand All @@ -37,6 +50,10 @@ class DBAdmin1(Base):
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
)
hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
hapi_replaced_date: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL"), index=True
)

location = relationship("DBLocation")

Expand Down
17 changes: 17 additions & 0 deletions src/hapi_schema/db_admin2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Integer,
String,
UniqueConstraint,
select,
text,
)
Expand All @@ -21,6 +23,17 @@

class DBAdmin2(Base):
__tablename__ = "admin2"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
CheckConstraint(
"((hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_update_date)",
name="hapi_dates",
),
UniqueConstraint("code", "hapi_updated_date", name="code_date_unique"),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
admin1_ref: Mapped[int] = mapped_column(
Expand All @@ -38,6 +51,10 @@ class DBAdmin2(Base):
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
)
hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
hapi_replaced_date: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL"), index=True
)

admin1 = relationship("DBAdmin1")

Expand Down
28 changes: 24 additions & 4 deletions src/hapi_schema/db_food_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from sqlalchemy import (
CheckConstraint,
DateTime,
Float,
ForeignKey,
Expand All @@ -26,6 +27,23 @@

class DBFoodSecurity(Base):
__tablename__ = "food_security"
__table_args__ = (
CheckConstraint(
"ipc_phase_code IN (1, 2, 3, 4, 5)", name="ipc_phase_code"
),
CheckConstraint(
'ipc_type_code IN ("current", "first projection", "second projection")',
name="ipc_phase_code",
),
CheckConstraint(
"population_fraction_in_phase >= 0 AND population_fraction_in_phase <=1",
name="population_fraction_in_phase",
),
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref: Mapped[int] = mapped_column(
Expand All @@ -41,15 +59,17 @@ class DBFoodSecurity(Base):
ipc_type_code: Mapped[str] = mapped_column(
ForeignKey("ipc_type.code", onupdate="CASCADE"), nullable=False
)
population_in_phase: Mapped[int] = mapped_column(Integer, nullable=False)
population_in_phase: Mapped[int] = mapped_column(
Integer, nullable=False, index=True
)
population_fraction_in_phase: Mapped[float] = mapped_column(
Float, nullable=False
Float, nullable=False, index=True
)
reference_period_start: Mapped[datetime] = mapped_column(
DateTime, nullable=False
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

Expand Down
10 changes: 9 additions & 1 deletion src/hapi_schema/db_humanitarian_needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Integer,
Expand All @@ -25,6 +26,13 @@

class DBHumanitarianNeeds(Base):
__tablename__ = "humanitarian_needs"
__table_args__ = (
CheckConstraint("population >= 0", name="population"),
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref: Mapped[int] = mapped_column(
Expand Down Expand Up @@ -60,7 +68,7 @@ class DBHumanitarianNeeds(Base):
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

Expand Down
25 changes: 24 additions & 1 deletion src/hapi_schema/db_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

from datetime import datetime

from sqlalchemy import DateTime, Integer, String, select, text
from sqlalchemy import (
CheckConstraint,
DateTime,
Integer,
String,
UniqueConstraint,
select,
text,
)
from sqlalchemy.orm import Mapped, mapped_column

from hapi_schema.utils.base import Base
Expand All @@ -11,6 +19,17 @@

class DBLocation(Base):
__tablename__ = "location"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
CheckConstraint(
"((hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_update_date)",
name="hapi_dates",
),
UniqueConstraint("code", "hapi_updated_date", name="code_date_unique"),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
code: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
Expand All @@ -21,6 +40,10 @@ class DBLocation(Base):
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
)
hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
hapi_replaced_date: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL"), index=True
)


view_params_location = ViewParams(
Expand Down
13 changes: 12 additions & 1 deletion src/hapi_schema/db_national_risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from sqlalchemy import (
CheckConstraint,
DateTime,
Float,
ForeignKey,
Expand All @@ -24,6 +25,16 @@

class DBNationalRisk(Base):
__tablename__ = "national_risk"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
CheckConstraint(
"((hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_update_date)",
name="hapi_dates",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref: Mapped[int] = mapped_column(
Expand All @@ -49,7 +60,7 @@ class DBNationalRisk(Base):
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

Expand Down
9 changes: 8 additions & 1 deletion src/hapi_schema/db_operational_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from sqlalchemy import (
CheckConstraint,
DateTime,
ForeignKey,
Integer,
Expand All @@ -26,6 +27,12 @@

class DBOperationalPresence(Base):
__tablename__ = "operational_presence"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref = mapped_column(
Expand All @@ -45,7 +52,7 @@ class DBOperationalPresence(Base):
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

Expand Down
17 changes: 16 additions & 1 deletion src/hapi_schema/db_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from sqlalchemy import (
CheckConstraint,
DateTime,
ForeignKey,
Integer,
Expand All @@ -19,6 +20,16 @@

class DBOrg(Base):
__tablename__ = "org"
__table_args__ = (
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
CheckConstraint(
"((hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_update_date)",
name="hapi_dates",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
acronym = mapped_column(String(32), nullable=False, index=True)
Expand All @@ -31,7 +42,11 @@ class DBOrg(Base):
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
hapi_replaced_date: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL"), index=True
)

org_type = relationship("DBOrgType")
Expand Down
4 changes: 3 additions & 1 deletion src/hapi_schema/db_org_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class DBOrgType(Base):
__tablename__ = "org_type"

code: Mapped[str] = mapped_column(String(32), primary_key=True)
description: Mapped[str] = mapped_column(String(512), nullable=False)
description: Mapped[str] = mapped_column(
String(512), nullable=False, index=True
)


view_params_org_type = ViewParams(
Expand Down
12 changes: 10 additions & 2 deletions src/hapi_schema/db_population.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from sqlalchemy import (
CheckConstraint,
DateTime,
ForeignKey,
Integer,
Expand All @@ -23,6 +24,13 @@

class DBPopulation(Base):
__tablename__ = "population"
__table_args__ = (
CheckConstraint("population >= 0", name="population"),
CheckConstraint(
"(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
name="reference_period",
),
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
resource_ref: Mapped[int] = mapped_column(
Expand All @@ -42,10 +50,10 @@ class DBPopulation(Base):
Integer, nullable=False, index=True
)
reference_period_start: Mapped[datetime] = mapped_column(
DateTime, nullable=False
DateTime, nullable=False, index=True
)
reference_period_end: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL")
DateTime, nullable=True, server_default=text("NULL"), index=True
)
source_data: Mapped[str] = mapped_column(Text, nullable=True)

Expand Down
10 changes: 9 additions & 1 deletion src/hapi_schema/db_resource.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"""Resource table and view."""

from datetime import datetime

from sqlalchemy import (
Boolean,
CheckConstraint,
DateTime,
ForeignKey,
Integer,
String,
select,
text,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

Expand All @@ -17,6 +21,7 @@

class DBResource(Base):
__tablename__ = "resource"
__table_args__ = CheckConstraint("hapi_replaced_date >= hapi_updated_date")

id: Mapped[int] = mapped_column(Integer, primary_key=True)
dataset_ref: Mapped[int] = mapped_column(
Expand All @@ -33,7 +38,10 @@ class DBResource(Base):
String(1024), nullable=False, unique=True
)
is_hxl: Mapped[bool] = mapped_column(Boolean, nullable=False, index=True)

hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
hapi_replaced_date: Mapped[datetime] = mapped_column(
DateTime, nullable=True, server_default=text("NULL"), index=True
)
dataset = relationship("DBDataset")


Expand Down
Loading

0 comments on commit 4f448eb

Please sign in to comment.