hapi-schema 0.7.4__tar.gz → 0.8.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.config/pre-commit-config.yaml +8 -7
  2. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.github/workflows/publish.yaml +1 -1
  3. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.github/workflows/run-python-tests.yaml +19 -3
  4. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/PKG-INFO +4 -3
  5. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/changelog.md +17 -2
  6. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/contributing.md +4 -0
  7. hapi_schema-0.8.0/docker/docker-compose.yml +12 -0
  8. hapi_schema-0.8.0/initialize_test_db.sh +5 -0
  9. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/pyproject.toml +2 -2
  10. hapi_schema-0.8.0/requirements.txt +44 -0
  11. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/_version.py +2 -2
  12. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_admin1.py +26 -22
  13. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_admin2.py +29 -25
  14. hapi_schema-0.8.0/src/hapi_schema/db_conflict_event.py +95 -0
  15. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_dataset.py +1 -1
  16. hapi_schema-0.8.0/src/hapi_schema/db_food_security.py +132 -0
  17. hapi_schema-0.8.0/src/hapi_schema/db_funding.py +93 -0
  18. hapi_schema-0.8.0/src/hapi_schema/db_humanitarian_needs.py +164 -0
  19. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_location.py +8 -17
  20. hapi_schema-0.8.0/src/hapi_schema/db_national_risk.py +117 -0
  21. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_operational_presence.py +51 -49
  22. hapi_schema-0.8.0/src/hapi_schema/db_org.py +47 -0
  23. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_population.py +49 -43
  24. hapi_schema-0.8.0/src/hapi_schema/db_poverty_rate.py +81 -0
  25. hapi_schema-0.8.0/src/hapi_schema/db_refugees.py +101 -0
  26. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_resource.py +20 -15
  27. hapi_schema-0.8.0/src/hapi_schema/utils/constraints.py +56 -0
  28. hapi_schema-0.8.0/src/hapi_schema/utils/enums.py +82 -0
  29. hapi_schema-0.8.0/src/hapi_schema/utils/view_as_table_code_generator.py +125 -0
  30. hapi_schema-0.8.0/src/hapi_schema/views.py +30 -0
  31. hapi_schema-0.8.0/tests/conftest.py +176 -0
  32. hapi_schema-0.8.0/tests/sample_data/data_admin1.py +20 -0
  33. hapi_schema-0.8.0/tests/sample_data/data_admin2.py +38 -0
  34. hapi_schema-0.8.0/tests/sample_data/data_conflict_event.py +22 -0
  35. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_food_security.py +6 -12
  36. hapi_schema-0.8.0/tests/sample_data/data_funding.py +16 -0
  37. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_humanitarian_needs.py +24 -29
  38. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_location.py +7 -4
  39. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_national_risk.py +2 -4
  40. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_operational_presence.py +10 -19
  41. hapi_schema-0.8.0/tests/sample_data/data_org.py +16 -0
  42. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_patch.py +0 -3
  43. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_population.py +11 -16
  44. hapi_schema-0.8.0/tests/sample_data/data_poverty_rate.py +12 -0
  45. hapi_schema-0.8.0/tests/sample_data/data_refugees.py +30 -0
  46. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_sector.py +4 -0
  47. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_admin1.py +18 -33
  48. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_admin2.py +21 -33
  49. hapi_schema-0.8.0/tests/test_conflict_event.py +74 -0
  50. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_dataset.py +2 -2
  51. hapi_schema-0.8.0/tests/test_food_security.py +82 -0
  52. hapi_schema-0.8.0/tests/test_funding.py +113 -0
  53. hapi_schema-0.8.0/tests/test_humanitarian_needs.py +143 -0
  54. hapi_schema-0.8.0/tests/test_location.py +52 -0
  55. hapi_schema-0.8.0/tests/test_national_risk.py +167 -0
  56. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_operational_presence.py +32 -10
  57. hapi_schema-0.8.0/tests/test_org.py +32 -0
  58. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_org_type.py +2 -2
  59. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_patch.py +1 -1
  60. hapi_schema-0.8.0/tests/test_population.py +127 -0
  61. hapi_schema-0.8.0/tests/test_poverty_rate.py +63 -0
  62. hapi_schema-0.8.0/tests/test_refugees.py +110 -0
  63. hapi_schema-0.8.0/tests/test_resource.py +33 -0
  64. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/test_sector.py +2 -2
  65. hapi_schema-0.7.4/requirements.txt +0 -55
  66. hapi_schema-0.7.4/src/hapi_schema/db_age_range.py +0 -22
  67. hapi_schema-0.7.4/src/hapi_schema/db_food_security.py +0 -137
  68. hapi_schema-0.7.4/src/hapi_schema/db_gender.py +0 -21
  69. hapi_schema-0.7.4/src/hapi_schema/db_humanitarian_needs.py +0 -144
  70. hapi_schema-0.7.4/src/hapi_schema/db_ipc_phase.py +0 -28
  71. hapi_schema-0.7.4/src/hapi_schema/db_ipc_type.py +0 -26
  72. hapi_schema-0.7.4/src/hapi_schema/db_national_risk.py +0 -122
  73. hapi_schema-0.7.4/src/hapi_schema/db_org.py +0 -68
  74. hapi_schema-0.7.4/src/hapi_schema/db_population_group.py +0 -23
  75. hapi_schema-0.7.4/src/hapi_schema/db_population_status.py +0 -23
  76. hapi_schema-0.7.4/tests/conftest.py +0 -124
  77. hapi_schema-0.7.4/tests/sample_data/data_admin1.py +0 -37
  78. hapi_schema-0.7.4/tests/sample_data/data_admin2.py +0 -70
  79. hapi_schema-0.7.4/tests/sample_data/data_age_range.py +0 -4
  80. hapi_schema-0.7.4/tests/sample_data/data_gender.py +0 -5
  81. hapi_schema-0.7.4/tests/sample_data/data_ipc_phase.py +0 -35
  82. hapi_schema-0.7.4/tests/sample_data/data_ipc_type.py +0 -14
  83. hapi_schema-0.7.4/tests/sample_data/data_org.py +0 -34
  84. hapi_schema-0.7.4/tests/sample_data/data_population_group.py +0 -4
  85. hapi_schema-0.7.4/tests/sample_data/data_population_status.py +0 -4
  86. hapi_schema-0.7.4/tests/test_age_range.py +0 -16
  87. hapi_schema-0.7.4/tests/test_food_security.py +0 -48
  88. hapi_schema-0.7.4/tests/test_gender.py +0 -15
  89. hapi_schema-0.7.4/tests/test_humanitarian_needs.py +0 -58
  90. hapi_schema-0.7.4/tests/test_ipc_phase.py +0 -34
  91. hapi_schema-0.7.4/tests/test_ipc_type.py +0 -26
  92. hapi_schema-0.7.4/tests/test_location.py +0 -77
  93. hapi_schema-0.7.4/tests/test_national_risk.py +0 -75
  94. hapi_schema-0.7.4/tests/test_org.py +0 -54
  95. hapi_schema-0.7.4/tests/test_population.py +0 -63
  96. hapi_schema-0.7.4/tests/test_population_group.py +0 -16
  97. hapi_schema-0.7.4/tests/test_population_status.py +0 -15
  98. hapi_schema-0.7.4/tests/test_resource.py +0 -51
  99. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.config/coveragerc +0 -0
  100. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.config/pytest.ini +0 -0
  101. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.config/ruff.toml +0 -0
  102. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/.gitignore +0 -0
  103. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/LICENSE +0 -0
  104. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/README.md +0 -0
  105. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/__init__.py +0 -0
  106. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_org_type.py +0 -0
  107. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_patch.py +0 -0
  108. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/db_sector.py +0 -0
  109. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/utils/__init__.py +0 -0
  110. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/utils/base.py +0 -0
  111. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/src/hapi_schema/utils/view_params.py +0 -0
  112. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/__init__.py +0 -0
  113. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_dataset.py +0 -0
  114. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_org_type.py +0 -0
  115. {hapi_schema-0.7.4 → hapi_schema-0.8.0}/tests/sample_data/data_resource.py +0 -0
@@ -1,5 +1,5 @@
1
1
  default_language_version:
2
- python: python3.11
2
+ python: python3.11
3
3
  repos:
4
4
  - repo: https://github.com/pre-commit/pre-commit-hooks
5
5
  rev: v4.5.0
@@ -8,18 +8,19 @@ repos:
8
8
  - id: end-of-file-fixer
9
9
  - id: check-ast
10
10
  - repo: https://github.com/astral-sh/ruff-pre-commit
11
- rev: v0.1.14
11
+ rev: v0.4.2
12
12
  hooks:
13
13
  # Run the linter.
14
14
  - id: ruff
15
- args: [--config, .config/ruff.toml, --fix]
15
+ args: [ --config, .config/ruff.toml, --fix ]
16
16
  # Run the formatter.
17
17
  - id: ruff-format
18
- args: [--config, .config/ruff.toml]
19
- - repo: https://github.com/jazzband/pip-tools
20
- rev: 7.3.0
18
+ args: [ --config, .config/ruff.toml ]
19
+ - repo: https://github.com/astral-sh/uv-pre-commit
20
+ rev: 0.1.38
21
21
  hooks:
22
+ # Run the pip compile
22
23
  - id: pip-compile
23
24
  name: pip-compile requirements.txt
24
25
  files: pyproject.toml
25
- args: [pyproject.toml, --resolver=backtracking, --all-extras, --upgrade, -q, -o, requirements.txt]
26
+ args: [ pyproject.toml, --resolver=backtracking, --all-extras, --upgrade, -q, -o, requirements.txt ]
@@ -18,7 +18,7 @@ jobs:
18
18
  - name: Set up Python
19
19
  uses: actions/setup-python@v4
20
20
  with:
21
- python-version: '3.11'
21
+ python-version: '3.x'
22
22
  - name: Install dependencies
23
23
  run: |
24
24
  python -m pip install --upgrade pip
@@ -17,12 +17,28 @@ jobs:
17
17
  build:
18
18
  runs-on: ubuntu-latest
19
19
 
20
+ services:
21
+ postgres:
22
+ image: postgres:14-alpine
23
+ env:
24
+ POSTGRES_USER: postgres
25
+ POSTGRES_PASSWORD: postgres
26
+ POSTGRES_DB: hapitest
27
+ ports:
28
+ - 5432:5432
29
+ options: >-
30
+ --health-cmd pg_isready
31
+ --health-interval 10s
32
+ --health-timeout 5s
33
+ --health-retries 5
34
+ --name postgres-container
35
+
20
36
  steps:
21
- - uses: actions/checkout@v3
37
+ - uses: actions/checkout@v4
22
38
  - name: Set up Python
23
- uses: actions/setup-python@v4
39
+ uses: actions/setup-python@v5
24
40
  with:
25
- python-version: '3.11'
41
+ python-version: '3.x'
26
42
  - name: Install dependencies
27
43
  run: |
28
44
  python -m pip install --upgrade pip
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: hapi-schema
3
- Version: 0.7.4
3
+ Version: 0.8.0
4
4
  Summary: HAPI database schema specified in SQLAlchemy
5
5
  Project-URL: Homepage, https://github.com/OCHA-DAP/hapi-schemas
6
6
  Author-email: Simon Johnson <simon.johnson@un.org>
@@ -26,11 +26,12 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
26
26
  Requires-Python: >=3.8
27
27
  Requires-Dist: sqlalchemy
28
28
  Provides-Extra: database
29
- Requires-Dist: hdx-python-database>=1.3.0; extra == 'database'
29
+ Requires-Dist: hdx-python-database>=1.3.1; extra == 'database'
30
30
  Provides-Extra: dev
31
31
  Requires-Dist: pre-commit; extra == 'dev'
32
32
  Provides-Extra: test
33
- Requires-Dist: hdx-python-database>=1.3.0; extra == 'test'
33
+ Requires-Dist: hdx-python-database>=1.3.1; extra == 'test'
34
+ Requires-Dist: psycopg[binary]; extra == 'test'
34
35
  Requires-Dist: pytest; extra == 'test'
35
36
  Requires-Dist: pytest-cov; extra == 'test'
36
37
  Description-Content-Type: text/markdown
@@ -5,11 +5,26 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [0.7.4]
8
+ ## 0.8.0
9
9
 
10
10
  ### Changed
11
11
 
12
- - Added columns to the patches table
12
+ - Many small changes to align with V1 of the schema
13
+ - `resource` and `dataset` primary keys are now the UUID
14
+ - Use postgres instead of sqlite for testing
15
+ - Update GitHub Actions workflow to use postgres
16
+ - Created tables which reflect the views
17
+ - Programmatically obtain views
18
+
19
+ ### Added
20
+ - New tables: humanitarian\_needs, funding, refugees, conflict\_events, poverty_rate
21
+ - New enums: Gender, DisabledMarker, EventType, PopulationGroup, PopulationStatus, IPCPhase, PovertyClassification
22
+ IPCType, and RiskClass
23
+ - Generalized constraints
24
+
25
+ ### Removed
26
+
27
+ - ipc_phase, ipc_type, age_range, and gender tables
13
28
 
14
29
 
15
30
  ## [0.7.3]
@@ -40,6 +40,10 @@ To check if your changes pass pre-commit without committing, run:
40
40
 
41
41
  ## Testing
42
42
 
43
+ Testing is now done against a Postgres database which can be deployed from the Docker definition in the `docker` directory using:
44
+
45
+ `docker-compose up -d`
46
+
43
47
  To run the tests and view coverage, execute:
44
48
 
45
49
  pytest -c .config/pytest.ini --cov hapi_schema --cov-config .config/coveragerc
@@ -0,0 +1,12 @@
1
+ version: '3.8'
2
+ services:
3
+ db:
4
+ image: postgres:14-alpine
5
+ container_name: hapi-test-db
6
+ restart: always
7
+ environment:
8
+ - POSTGRES_USER=postgres
9
+ - POSTGRES_PASSWORD=postgres
10
+ - POSTGRES_DB=hapitest
11
+ ports:
12
+ - '5432:5432'
@@ -0,0 +1,5 @@
1
+ #!/bin/sh
2
+
3
+ cd docker
4
+ docker-compose up -d
5
+ cd ..
@@ -42,9 +42,9 @@ content-type = "text/markdown"
42
42
  Homepage = "https://github.com/OCHA-DAP/hapi-schemas"
43
43
 
44
44
  [project.optional-dependencies]
45
- database = ["hdx-python-database>=1.3.0"]
45
+ database = ["hdx-python-database>=1.3.1"]
46
46
  dev = ["pre-commit"]
47
- test = ["hdx-python-database>=1.3.0", "pytest", "pytest-cov"]
47
+ test = ["hdx-python-database>=1.3.1", "psycopg[binary]", "pytest", "pytest-cov"]
48
48
 
49
49
  #########
50
50
  # Hatch #
@@ -0,0 +1,44 @@
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile pyproject.toml --resolver=backtracking --all-extras -o requirements.txt
3
+ cfgv==3.4.0
4
+ # via pre-commit
5
+ coverage==7.5.1
6
+ # via pytest-cov
7
+ distlib==0.3.8
8
+ # via virtualenv
9
+ filelock==3.14.0
10
+ # via virtualenv
11
+ greenlet==3.0.3
12
+ # via sqlalchemy
13
+ hdx-python-database==1.3.1
14
+ identify==2.5.36
15
+ # via pre-commit
16
+ iniconfig==2.0.0
17
+ # via pytest
18
+ nodeenv==1.8.0
19
+ # via pre-commit
20
+ packaging==24.0
21
+ # via pytest
22
+ platformdirs==4.2.1
23
+ # via virtualenv
24
+ pluggy==1.5.0
25
+ # via pytest
26
+ pre-commit==3.7.0
27
+ psycopg==3.1.18
28
+ psycopg-binary==3.1.18
29
+ # via psycopg
30
+ pytest==8.2.0
31
+ # via pytest-cov
32
+ pytest-cov==5.0.0
33
+ pyyaml==6.0.1
34
+ # via pre-commit
35
+ setuptools==69.5.1
36
+ # via nodeenv
37
+ sqlalchemy==2.0.30
38
+ # via hdx-python-database
39
+ typing-extensions==4.11.0
40
+ # via
41
+ # psycopg
42
+ # sqlalchemy
43
+ virtualenv==20.26.1
44
+ # via pre-commit
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.7.4'
16
- __version_tuple__ = version_tuple = (0, 7, 4)
15
+ __version__ = version = '0.8.0'
16
+ __version_tuple__ = version_tuple = (0, 8, 0)
@@ -4,12 +4,10 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  Boolean,
7
- CheckConstraint,
8
7
  DateTime,
9
8
  ForeignKey,
10
9
  Integer,
11
10
  String,
12
- UniqueConstraint,
13
11
  select,
14
12
  text,
15
13
  )
@@ -17,21 +15,18 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
17
15
 
18
16
  from hapi_schema.db_location import DBLocation
19
17
  from hapi_schema.utils.base import Base
18
+ from hapi_schema.utils.constraints import (
19
+ code_and_reference_period_unique_constraint,
20
+ reference_period_constraint,
21
+ )
20
22
  from hapi_schema.utils.view_params import ViewParams
21
23
 
22
24
 
23
25
  class DBAdmin1(Base):
24
26
  __tablename__ = "admin1"
25
27
  __table_args__ = (
26
- CheckConstraint(
27
- "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
28
- name="reference_period",
29
- ),
30
- CheckConstraint(
31
- "(hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_updated_date)",
32
- name="hapi_dates",
33
- ),
34
- UniqueConstraint("code", "hapi_updated_date"),
28
+ reference_period_constraint(),
29
+ code_and_reference_period_unique_constraint(admin_level="admin1"),
35
30
  )
36
31
 
37
32
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
@@ -39,10 +34,10 @@ class DBAdmin1(Base):
39
34
  ForeignKey("location.id", onupdate="CASCADE", ondelete="CASCADE"),
40
35
  nullable=False,
41
36
  )
42
- code: Mapped[str] = mapped_column(String(128), nullable=False)
43
- name: Mapped[str] = mapped_column(String(512), nullable=False)
37
+ code: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
38
+ name: Mapped[str] = mapped_column(String(512), nullable=False, index=True)
44
39
  is_unspecified: Mapped[bool] = mapped_column(
45
- Boolean, server_default=text("FALSE")
40
+ Boolean, server_default=text("FALSE"), nullable=False
46
41
  )
47
42
  reference_period_start: Mapped[datetime] = mapped_column(
48
43
  DateTime, nullable=True, server_default=text("NULL"), index=True
@@ -50,10 +45,6 @@ class DBAdmin1(Base):
50
45
  reference_period_end: Mapped[datetime] = mapped_column(
51
46
  DateTime, nullable=True, server_default=text("NULL"), index=True
52
47
  )
53
- hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
54
- hapi_replaced_date: Mapped[datetime] = mapped_column(
55
- DateTime, nullable=True, server_default=text("NULL"), index=True
56
- )
57
48
 
58
49
  location = relationship("DBLocation")
59
50
 
@@ -65,10 +56,6 @@ view_params_admin1 = ViewParams(
65
56
  *DBAdmin1.__table__.columns,
66
57
  DBLocation.code.label("location_code"),
67
58
  DBLocation.name.label("location_name"),
68
- DBLocation.reference_period_start.label(
69
- "location_reference_period_start"
70
- ),
71
- DBLocation.reference_period_end.label("location_reference_period_end"),
72
59
  ).select_from(
73
60
  DBAdmin1.__table__.join(
74
61
  DBLocation.__table__,
@@ -77,3 +64,20 @@ view_params_admin1 = ViewParams(
77
64
  )
78
65
  ),
79
66
  )
67
+
68
+
69
+ class DBAdmin1VAT(Base):
70
+ __tablename__ = "admin1_vat"
71
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
72
+ location_ref: Mapped[int] = mapped_column(Integer)
73
+ code: Mapped[str] = mapped_column(String(128))
74
+ name: Mapped[str] = mapped_column(String(512))
75
+ is_unspecified: Mapped[bool] = mapped_column(Boolean)
76
+ reference_period_start: Mapped[datetime] = mapped_column(
77
+ DateTime, index=True
78
+ )
79
+ reference_period_end: Mapped[datetime] = mapped_column(
80
+ DateTime, index=True
81
+ )
82
+ location_code: Mapped[str] = mapped_column(String(128), index=True)
83
+ location_name: Mapped[str] = mapped_column(String(512), index=True)
@@ -4,12 +4,10 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  Boolean,
7
- CheckConstraint,
8
7
  DateTime,
9
8
  ForeignKey,
10
9
  Integer,
11
10
  String,
12
- UniqueConstraint,
13
11
  select,
14
12
  text,
15
13
  )
@@ -18,21 +16,18 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
18
16
  from hapi_schema.db_admin1 import DBAdmin1
19
17
  from hapi_schema.db_location import DBLocation
20
18
  from hapi_schema.utils.base import Base
19
+ from hapi_schema.utils.constraints import (
20
+ code_and_reference_period_unique_constraint,
21
+ reference_period_constraint,
22
+ )
21
23
  from hapi_schema.utils.view_params import ViewParams
22
24
 
23
25
 
24
26
  class DBAdmin2(Base):
25
27
  __tablename__ = "admin2"
26
28
  __table_args__ = (
27
- CheckConstraint(
28
- "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
29
- name="reference_period",
30
- ),
31
- CheckConstraint(
32
- "(hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_updated_date)",
33
- name="hapi_dates",
34
- ),
35
- UniqueConstraint("code", "hapi_updated_date"),
29
+ reference_period_constraint(),
30
+ code_and_reference_period_unique_constraint(admin_level="admin2"),
36
31
  )
37
32
 
38
33
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
@@ -40,10 +35,10 @@ class DBAdmin2(Base):
40
35
  ForeignKey("admin1.id", onupdate="CASCADE", ondelete="CASCADE"),
41
36
  nullable=False,
42
37
  )
43
- code: Mapped[str] = mapped_column(String(128), nullable=False)
44
- name: Mapped[str] = mapped_column(String(512), nullable=False)
38
+ code: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
39
+ name: Mapped[str] = mapped_column(String(512), nullable=False, index=True)
45
40
  is_unspecified: Mapped[bool] = mapped_column(
46
- Boolean, server_default=text("FALSE")
41
+ Boolean, server_default=text("FALSE"), nullable=False
47
42
  )
48
43
  reference_period_start: Mapped[datetime] = mapped_column(
49
44
  DateTime, nullable=True, server_default=text("NULL"), index=True
@@ -52,11 +47,6 @@ class DBAdmin2(Base):
52
47
  DateTime, nullable=True, server_default=text("NULL"), index=True
53
48
  )
54
49
 
55
- hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
56
- hapi_replaced_date: Mapped[datetime] = mapped_column(
57
- DateTime, nullable=True, server_default=text("NULL"), index=True
58
- )
59
-
60
50
  admin1 = relationship("DBAdmin1")
61
51
 
62
52
 
@@ -68,14 +58,8 @@ view_params_admin2 = ViewParams(
68
58
  DBAdmin1.code.label("admin1_code"),
69
59
  DBAdmin1.name.label("admin1_name"),
70
60
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
71
- DBAdmin1.reference_period_start.label("admin1_reference_period_start"),
72
- DBAdmin1.reference_period_end.label("admin1_reference_period_end"),
73
61
  DBLocation.code.label("location_code"),
74
62
  DBLocation.name.label("location_name"),
75
- DBLocation.reference_period_start.label(
76
- "location_reference_period_start"
77
- ),
78
- DBLocation.reference_period_end.label("location_reference_period_end"),
79
63
  ).select_from(
80
64
  DBAdmin2.__table__.join(
81
65
  DBAdmin1.__table__,
@@ -88,3 +72,23 @@ view_params_admin2 = ViewParams(
88
72
  ),
89
73
  ),
90
74
  )
75
+
76
+
77
+ class DBAdmin2VAT(Base):
78
+ __tablename__ = "admin2_vat"
79
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
80
+ admin1_ref: Mapped[int] = mapped_column(Integer)
81
+ code: Mapped[str] = mapped_column(String(128), index=True)
82
+ name: Mapped[str] = mapped_column(String(512), index=True)
83
+ is_unspecified: Mapped[bool] = mapped_column(Boolean)
84
+ reference_period_start: Mapped[datetime] = mapped_column(
85
+ DateTime, index=True
86
+ )
87
+ reference_period_end: Mapped[datetime] = mapped_column(
88
+ DateTime, index=True
89
+ )
90
+ admin1_code: Mapped[str] = mapped_column(String(128))
91
+ admin1_name: Mapped[str] = mapped_column(String(512))
92
+ admin1_is_unspecified: Mapped[bool] = mapped_column(Boolean)
93
+ location_code: Mapped[str] = mapped_column(String(128), index=True)
94
+ location_name: Mapped[str] = mapped_column(String(512), index=True)
@@ -0,0 +1,95 @@
1
+ """ConflictEvent table and view."""
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ DateTime,
7
+ Enum,
8
+ ForeignKey,
9
+ Integer,
10
+ select,
11
+ )
12
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
13
+
14
+ from hapi_schema.db_admin1 import DBAdmin1, DBLocation
15
+ from hapi_schema.db_admin2 import DBAdmin2
16
+ from hapi_schema.utils.base import Base
17
+ from hapi_schema.utils.constraints import (
18
+ population_constraint,
19
+ reference_period_constraint,
20
+ )
21
+ from hapi_schema.utils.enums import EventType
22
+ from hapi_schema.utils.view_params import ViewParams
23
+
24
+
25
+ # normalised table
26
+ class DBConflictEvent(Base):
27
+ __tablename__ = "conflict_event"
28
+ __table_args__ = (
29
+ population_constraint(
30
+ population_var_name="events"
31
+ ), # not really a population
32
+ population_constraint(population_var_name="fatalities"),
33
+ reference_period_constraint(),
34
+ )
35
+
36
+ resource_hdx_id: Mapped[str] = mapped_column(
37
+ ForeignKey("resource.hdx_id", onupdate="CASCADE", ondelete="CASCADE"),
38
+ nullable=False,
39
+ )
40
+ admin2_ref: Mapped[int] = mapped_column(
41
+ ForeignKey("admin2.id", onupdate="CASCADE"),
42
+ nullable=False,
43
+ primary_key=True,
44
+ )
45
+ event_type: Mapped[EventType] = mapped_column(
46
+ Enum(EventType), nullable=False, primary_key=True
47
+ )
48
+ events: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
49
+ fatalities: Mapped[int] = mapped_column(Integer, nullable=True, index=True)
50
+ reference_period_start: Mapped[datetime] = mapped_column(
51
+ DateTime, primary_key=True
52
+ )
53
+ reference_period_end: Mapped[datetime] = mapped_column(
54
+ DateTime, nullable=False, index=True
55
+ )
56
+
57
+ resource = relationship("DBResource")
58
+ admin2 = relationship("DBAdmin2")
59
+
60
+
61
+ # view
62
+ view_params_conflict_event = ViewParams(
63
+ name="conflict_event_view",
64
+ metadata=Base.metadata,
65
+ selectable=select(
66
+ *DBConflictEvent.__table__.columns,
67
+ DBLocation.code.label("location_code"),
68
+ DBLocation.name.label("location_name"),
69
+ DBAdmin1.code.label("admin1_code"),
70
+ DBAdmin1.name.label("admin1_name"),
71
+ DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
72
+ DBAdmin1.location_ref.label("location_ref"),
73
+ DBAdmin2.code.label("admin2_code"),
74
+ DBAdmin2.name.label("admin2_name"),
75
+ DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
76
+ DBAdmin2.admin1_ref.label("admin1_ref"),
77
+ ).select_from(
78
+ # Join risk to admin2
79
+ DBConflictEvent.__table__.join(
80
+ DBAdmin2.__table__,
81
+ DBConflictEvent.admin2_ref == DBAdmin2.id,
82
+ isouter=True,
83
+ )
84
+ .join(
85
+ DBAdmin1.__table__,
86
+ DBAdmin2.admin1_ref == DBAdmin1.id,
87
+ isouter=True,
88
+ )
89
+ .join(
90
+ DBLocation.__table__,
91
+ DBAdmin1.location_ref == DBLocation.id,
92
+ isouter=True,
93
+ )
94
+ ),
95
+ )
@@ -12,7 +12,7 @@ class DBDataset(Base):
12
12
 
13
13
  hdx_id: Mapped[str] = mapped_column(String(36), primary_key=True)
14
14
  hdx_stub: Mapped[str] = mapped_column(
15
- String(128), unique=True, nullable=False
15
+ String(128), unique=True, nullable=False, index=True
16
16
  )
17
17
  title = mapped_column(String(1024), nullable=False)
18
18
  hdx_provider_stub: Mapped[str] = mapped_column(
@@ -0,0 +1,132 @@
1
+ """Food security table and view."""
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ Boolean,
7
+ DateTime,
8
+ Enum,
9
+ Float,
10
+ ForeignKey,
11
+ Integer,
12
+ String,
13
+ select,
14
+ )
15
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
16
+
17
+ from hapi_schema.db_admin1 import DBAdmin1
18
+ from hapi_schema.db_admin2 import DBAdmin2
19
+ from hapi_schema.db_location import DBLocation
20
+ from hapi_schema.db_resource import DBResource
21
+ from hapi_schema.utils.base import Base
22
+ from hapi_schema.utils.constraints import (
23
+ population_constraint,
24
+ reference_period_constraint,
25
+ )
26
+ from hapi_schema.utils.enums import IPCPhase, IPCType
27
+ from hapi_schema.utils.view_params import ViewParams
28
+
29
+
30
+ class DBFoodSecurity(Base):
31
+ __tablename__ = "food_security"
32
+ __table_args__ = (
33
+ reference_period_constraint(),
34
+ population_constraint(population_var_name="population_in_phase"),
35
+ )
36
+
37
+ resource_hdx_id: Mapped[str] = mapped_column(
38
+ ForeignKey("resource.hdx_id", onupdate="CASCADE", ondelete="CASCADE"),
39
+ nullable=False,
40
+ )
41
+ admin2_ref: Mapped[int] = mapped_column(
42
+ ForeignKey("admin2.id", onupdate="CASCADE"), primary_key=True
43
+ )
44
+ ipc_phase: Mapped[IPCPhase] = mapped_column(
45
+ Enum(IPCPhase), primary_key=True
46
+ )
47
+ ipc_type: Mapped[IPCType] = mapped_column(Enum(IPCType), primary_key=True)
48
+ population_in_phase: Mapped[int] = mapped_column(
49
+ Integer, nullable=False, index=True
50
+ )
51
+ population_fraction_in_phase: Mapped[float] = mapped_column(
52
+ Float, nullable=False, index=True
53
+ )
54
+ reference_period_start: Mapped[datetime] = mapped_column(
55
+ DateTime, primary_key=True
56
+ )
57
+ reference_period_end: Mapped[datetime] = mapped_column(
58
+ DateTime, nullable=False, index=True
59
+ )
60
+
61
+ resource = relationship(DBResource)
62
+ admin2 = relationship(DBAdmin2)
63
+
64
+
65
+ view_params_food_security = ViewParams(
66
+ name="food_security_view",
67
+ metadata=Base.metadata,
68
+ selectable=select(
69
+ *DBFoodSecurity.__table__.columns,
70
+ DBLocation.code.label("location_code"),
71
+ DBLocation.name.label("location_name"),
72
+ DBAdmin1.code.label("admin1_code"),
73
+ DBAdmin1.name.label("admin1_name"),
74
+ DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
75
+ DBAdmin1.location_ref.label("location_ref"),
76
+ DBAdmin2.code.label("admin2_code"),
77
+ DBAdmin2.name.label("admin2_name"),
78
+ DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
79
+ DBAdmin2.admin1_ref.label("admin1_ref"),
80
+ ).select_from(
81
+ # Join pop to admin2 to admin1 to loc
82
+ DBFoodSecurity.__table__.join(
83
+ DBAdmin2.__table__,
84
+ DBFoodSecurity.admin2_ref == DBAdmin2.id,
85
+ isouter=True,
86
+ )
87
+ .join(
88
+ DBAdmin1.__table__,
89
+ DBAdmin2.admin1_ref == DBAdmin1.id,
90
+ isouter=True,
91
+ )
92
+ .join(
93
+ DBLocation.__table__,
94
+ DBAdmin1.location_ref == DBLocation.id,
95
+ isouter=True,
96
+ )
97
+ ),
98
+ )
99
+
100
+
101
+ class DBFoodSecurityVAT(Base):
102
+ __tablename__ = "food_security_vat"
103
+ resource_hdx_id: Mapped[str] = mapped_column(String(36))
104
+ admin2_ref: Mapped[int] = mapped_column(
105
+ Integer, index=True, primary_key=True
106
+ )
107
+ ipc_phase: Mapped[IPCPhase] = mapped_column(
108
+ Enum(IPCPhase), index=True, primary_key=True
109
+ )
110
+ ipc_type: Mapped[IPCType] = mapped_column(
111
+ Enum(IPCType), index=True, primary_key=True
112
+ )
113
+ population_in_phase: Mapped[int] = mapped_column(Integer, primary_key=True)
114
+ population_fraction_in_phase: Mapped[float] = mapped_column(
115
+ Float, index=True, primary_key=True
116
+ )
117
+ reference_period_start: Mapped[datetime] = mapped_column(
118
+ DateTime, index=True
119
+ )
120
+ reference_period_end: Mapped[datetime] = mapped_column(
121
+ DateTime, index=True
122
+ )
123
+ location_code: Mapped[str] = mapped_column(String(128))
124
+ location_name: Mapped[str] = mapped_column(String(512), index=True)
125
+ admin1_code: Mapped[str] = mapped_column(String(128))
126
+ admin1_name: Mapped[str] = mapped_column(String(512))
127
+ admin1_is_unspecified: Mapped[bool] = mapped_column(Boolean)
128
+ location_ref: Mapped[int] = mapped_column(Integer)
129
+ admin2_code: Mapped[str] = mapped_column(String(128), index=True)
130
+ admin2_name: Mapped[str] = mapped_column(String(512), index=True)
131
+ admin2_is_unspecified: Mapped[bool] = mapped_column(Boolean)
132
+ admin1_ref: Mapped[int] = mapped_column(Integer)