hapi-schema 0.7.4__tar.gz → 0.8.2__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 (130) hide show
  1. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.config/pre-commit-config.yaml +8 -7
  2. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.github/workflows/publish.yaml +1 -1
  3. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.github/workflows/run-python-tests.yaml +19 -3
  4. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/PKG-INFO +4 -3
  5. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/changelog.md +34 -2
  6. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/contributing.md +4 -0
  7. hapi_schema-0.8.2/docker/docker-compose.yml +12 -0
  8. hapi_schema-0.8.2/initialize_test_db.sh +5 -0
  9. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/pyproject.toml +2 -2
  10. hapi_schema-0.8.2/requirements.txt +44 -0
  11. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/_version.py +2 -2
  12. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_admin1.py +12 -22
  13. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_admin2.py +12 -25
  14. hapi_schema-0.8.2/src/hapi_schema/db_conflict_event.py +95 -0
  15. hapi_schema-0.8.2/src/hapi_schema/db_currency.py +21 -0
  16. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_dataset.py +1 -1
  17. hapi_schema-0.8.2/src/hapi_schema/db_food_price.py +126 -0
  18. hapi_schema-0.8.2/src/hapi_schema/db_food_security.py +96 -0
  19. hapi_schema-0.8.2/src/hapi_schema/db_funding.py +93 -0
  20. hapi_schema-0.8.2/src/hapi_schema/db_humanitarian_needs.py +127 -0
  21. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_location.py +12 -17
  22. hapi_schema-0.8.2/src/hapi_schema/db_national_risk.py +94 -0
  23. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_operational_presence.py +21 -50
  24. hapi_schema-0.8.2/src/hapi_schema/db_org.py +39 -0
  25. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_patch.py +25 -7
  26. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_population.py +21 -43
  27. hapi_schema-0.8.2/src/hapi_schema/db_poverty_rate.py +81 -0
  28. hapi_schema-0.8.2/src/hapi_schema/db_refugees.py +102 -0
  29. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_resource.py +4 -17
  30. hapi_schema-0.8.2/src/hapi_schema/db_views_as_tables.py +301 -0
  31. hapi_schema-0.8.2/src/hapi_schema/db_wfp_commodity.py +25 -0
  32. hapi_schema-0.8.2/src/hapi_schema/db_wfp_market.py +63 -0
  33. hapi_schema-0.8.2/src/hapi_schema/utils/constraints.py +70 -0
  34. hapi_schema-0.8.2/src/hapi_schema/utils/enums.py +105 -0
  35. hapi_schema-0.8.2/src/hapi_schema/utils/hapi_views_code_generator.py +213 -0
  36. hapi_schema-0.8.2/src/hapi_schema/utils/view_as_table_code_generator.py +191 -0
  37. hapi_schema-0.8.2/src/hapi_schema/utils/view_as_table_definitions.toml +159 -0
  38. hapi_schema-0.8.2/src/hapi_schema/views.py +30 -0
  39. hapi_schema-0.8.2/tests/conftest.py +190 -0
  40. hapi_schema-0.8.2/tests/sample_data/data_admin1.py +21 -0
  41. hapi_schema-0.8.2/tests/sample_data/data_admin2.py +41 -0
  42. hapi_schema-0.8.2/tests/sample_data/data_conflict_event.py +22 -0
  43. hapi_schema-0.8.2/tests/sample_data/data_currency.py +10 -0
  44. hapi_schema-0.8.2/tests/sample_data/data_food_price.py +30 -0
  45. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_food_security.py +6 -12
  46. hapi_schema-0.8.2/tests/sample_data/data_funding.py +16 -0
  47. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_humanitarian_needs.py +24 -29
  48. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_location.py +8 -4
  49. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_national_risk.py +2 -4
  50. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_operational_presence.py +10 -19
  51. hapi_schema-0.8.2/tests/sample_data/data_org.py +16 -0
  52. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_patch.py +0 -3
  53. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_population.py +11 -16
  54. hapi_schema-0.8.2/tests/sample_data/data_poverty_rate.py +12 -0
  55. hapi_schema-0.8.2/tests/sample_data/data_refugees.py +30 -0
  56. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_sector.py +4 -0
  57. hapi_schema-0.8.2/tests/sample_data/data_wfp_commodity.py +12 -0
  58. hapi_schema-0.8.2/tests/sample_data/data_wfp_market.py +9 -0
  59. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_admin1.py +18 -33
  60. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_admin2.py +18 -33
  61. hapi_schema-0.8.2/tests/test_conflict_event.py +74 -0
  62. hapi_schema-0.8.2/tests/test_currency.py +15 -0
  63. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_dataset.py +2 -2
  64. hapi_schema-0.8.2/tests/test_food_price.py +78 -0
  65. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_food_security.py +25 -10
  66. hapi_schema-0.8.2/tests/test_funding.py +113 -0
  67. hapi_schema-0.8.2/tests/test_humanitarian_needs.py +124 -0
  68. hapi_schema-0.8.2/tests/test_location.py +64 -0
  69. hapi_schema-0.8.2/tests/test_national_risk.py +149 -0
  70. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_operational_presence.py +5 -10
  71. hapi_schema-0.8.2/tests/test_org.py +15 -0
  72. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_org_type.py +2 -2
  73. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_patch.py +2 -4
  74. hapi_schema-0.8.2/tests/test_population.py +102 -0
  75. hapi_schema-0.8.2/tests/test_poverty_rate.py +63 -0
  76. hapi_schema-0.8.2/tests/test_refugees.py +110 -0
  77. hapi_schema-0.8.2/tests/test_resource.py +15 -0
  78. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/test_sector.py +2 -2
  79. hapi_schema-0.8.2/tests/test_views_as_tables.py +312 -0
  80. hapi_schema-0.8.2/tests/test_wfp_commodity.py +18 -0
  81. hapi_schema-0.8.2/tests/test_wfp_market.py +61 -0
  82. hapi_schema-0.7.4/requirements.txt +0 -55
  83. hapi_schema-0.7.4/src/hapi_schema/db_age_range.py +0 -22
  84. hapi_schema-0.7.4/src/hapi_schema/db_food_security.py +0 -137
  85. hapi_schema-0.7.4/src/hapi_schema/db_gender.py +0 -21
  86. hapi_schema-0.7.4/src/hapi_schema/db_humanitarian_needs.py +0 -144
  87. hapi_schema-0.7.4/src/hapi_schema/db_ipc_phase.py +0 -28
  88. hapi_schema-0.7.4/src/hapi_schema/db_ipc_type.py +0 -26
  89. hapi_schema-0.7.4/src/hapi_schema/db_national_risk.py +0 -122
  90. hapi_schema-0.7.4/src/hapi_schema/db_org.py +0 -68
  91. hapi_schema-0.7.4/src/hapi_schema/db_population_group.py +0 -23
  92. hapi_schema-0.7.4/src/hapi_schema/db_population_status.py +0 -23
  93. hapi_schema-0.7.4/tests/conftest.py +0 -124
  94. hapi_schema-0.7.4/tests/sample_data/data_admin1.py +0 -37
  95. hapi_schema-0.7.4/tests/sample_data/data_admin2.py +0 -70
  96. hapi_schema-0.7.4/tests/sample_data/data_age_range.py +0 -4
  97. hapi_schema-0.7.4/tests/sample_data/data_gender.py +0 -5
  98. hapi_schema-0.7.4/tests/sample_data/data_ipc_phase.py +0 -35
  99. hapi_schema-0.7.4/tests/sample_data/data_ipc_type.py +0 -14
  100. hapi_schema-0.7.4/tests/sample_data/data_org.py +0 -34
  101. hapi_schema-0.7.4/tests/sample_data/data_population_group.py +0 -4
  102. hapi_schema-0.7.4/tests/sample_data/data_population_status.py +0 -4
  103. hapi_schema-0.7.4/tests/test_age_range.py +0 -16
  104. hapi_schema-0.7.4/tests/test_gender.py +0 -15
  105. hapi_schema-0.7.4/tests/test_humanitarian_needs.py +0 -58
  106. hapi_schema-0.7.4/tests/test_ipc_phase.py +0 -34
  107. hapi_schema-0.7.4/tests/test_ipc_type.py +0 -26
  108. hapi_schema-0.7.4/tests/test_location.py +0 -77
  109. hapi_schema-0.7.4/tests/test_national_risk.py +0 -75
  110. hapi_schema-0.7.4/tests/test_org.py +0 -54
  111. hapi_schema-0.7.4/tests/test_population.py +0 -63
  112. hapi_schema-0.7.4/tests/test_population_group.py +0 -16
  113. hapi_schema-0.7.4/tests/test_population_status.py +0 -15
  114. hapi_schema-0.7.4/tests/test_resource.py +0 -51
  115. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.config/coveragerc +0 -0
  116. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.config/pytest.ini +0 -0
  117. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.config/ruff.toml +0 -0
  118. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/.gitignore +0 -0
  119. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/LICENSE +0 -0
  120. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/README.md +0 -0
  121. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/__init__.py +0 -0
  122. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_org_type.py +0 -0
  123. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/db_sector.py +0 -0
  124. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/utils/__init__.py +0 -0
  125. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/utils/base.py +0 -0
  126. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/src/hapi_schema/utils/view_params.py +0 -0
  127. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/__init__.py +0 -0
  128. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_dataset.py +0 -0
  129. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/tests/sample_data/data_org_type.py +0 -0
  130. {hapi_schema-0.7.4 → hapi_schema-0.8.2}/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.2
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,43 @@ 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.2
9
+
10
+ Miscellaneous fixes for pipelines
9
11
 
10
12
  ### Changed
13
+ - added from\_cods column to location, admin1, and admin2
14
+
15
+
16
+ ## 0.8.1
17
+
18
+ Implemented the Food Prices subcategory.
19
+
20
+ ### Added
21
+ - New tables and views: food\_price, wfp\_commodity, wfp\_market, currency
22
+ - New enums: PriceFlag, PriceType, CommodityCategory
23
+ - New constraints: non\_negative\_constraint, latlon\_constraint
24
+
25
+
26
+ ## 0.8.0
27
+
28
+ ### Changed
29
+
30
+ - Many small changes to align with V1 of the schema
31
+ - `resource` and `dataset` primary keys are now the UUID
32
+ - Use postgres instead of sqlite for testing
33
+ - Update GitHub Actions workflow to use postgres
34
+ - Created tables which reflect the views
35
+ - Programmatically obtain views
36
+
37
+ ### Added
38
+ - New tables: humanitarian\_needs, funding, refugees, conflict\_event, poverty\_rate
39
+ - New enums: Gender, DisabledMarker, EventType, PopulationGroup, PopulationStatus, IPCPhase, PovertyClassification, IPCType, and RiskClass
40
+ - Generalized constraints
41
+
42
+ ### Removed
11
43
 
12
- - Added columns to the patches table
44
+ - ipc\_phase, ipc\_type, age\_range, and gender tables
13
45
 
14
46
 
15
47
  ## [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.2'
16
+ __version_tuple__ = version_tuple = (0, 8, 2)
@@ -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,13 @@ 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
41
+ )
42
+ from_cods: Mapped[bool] = mapped_column(
43
+ Boolean, nullable=False, server_default=text("TRUE")
46
44
  )
47
45
  reference_period_start: Mapped[datetime] = mapped_column(
48
46
  DateTime, nullable=True, server_default=text("NULL"), index=True
@@ -50,10 +48,6 @@ class DBAdmin1(Base):
50
48
  reference_period_end: Mapped[datetime] = mapped_column(
51
49
  DateTime, nullable=True, server_default=text("NULL"), index=True
52
50
  )
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
51
 
58
52
  location = relationship("DBLocation")
59
53
 
@@ -65,10 +59,6 @@ view_params_admin1 = ViewParams(
65
59
  *DBAdmin1.__table__.columns,
66
60
  DBLocation.code.label("location_code"),
67
61
  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
62
  ).select_from(
73
63
  DBAdmin1.__table__.join(
74
64
  DBLocation.__table__,
@@ -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,13 @@ 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
42
+ )
43
+ from_cods: Mapped[bool] = mapped_column(
44
+ Boolean, nullable=False, server_default=text("TRUE")
47
45
  )
48
46
  reference_period_start: Mapped[datetime] = mapped_column(
49
47
  DateTime, nullable=True, server_default=text("NULL"), index=True
@@ -52,11 +50,6 @@ class DBAdmin2(Base):
52
50
  DateTime, nullable=True, server_default=text("NULL"), index=True
53
51
  )
54
52
 
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
53
  admin1 = relationship("DBAdmin1")
61
54
 
62
55
 
@@ -68,14 +61,8 @@ view_params_admin2 = ViewParams(
68
61
  DBAdmin1.code.label("admin1_code"),
69
62
  DBAdmin1.name.label("admin1_name"),
70
63
  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
64
  DBLocation.code.label("location_code"),
74
65
  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
66
  ).select_from(
80
67
  DBAdmin2.__table__.join(
81
68
  DBAdmin1.__table__,
@@ -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
+ )
@@ -0,0 +1,21 @@
1
+ """Currency table and view."""
2
+
3
+ from sqlalchemy import String, select
4
+ from sqlalchemy.orm import Mapped, mapped_column
5
+
6
+ from hapi_schema.utils.base import Base
7
+ from hapi_schema.utils.view_params import ViewParams
8
+
9
+
10
+ class DBCurrency(Base):
11
+ __tablename__ = "currency"
12
+
13
+ code: Mapped[str] = mapped_column(String(32), primary_key=True)
14
+ name: Mapped[str] = mapped_column(String(512), nullable=False, index=True)
15
+
16
+
17
+ view_params_currency = ViewParams(
18
+ name="currency_view",
19
+ metadata=Base.metadata,
20
+ selectable=select(*DBCurrency.__table__.columns),
21
+ )
@@ -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,126 @@
1
+ """FoodPrice table and view."""
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ DateTime,
7
+ Enum,
8
+ Float,
9
+ ForeignKey,
10
+ String,
11
+ select,
12
+ )
13
+ from sqlalchemy.orm import Mapped, mapped_column, relationship
14
+
15
+ from hapi_schema.db_admin2 import DBAdmin1, DBAdmin2, DBLocation
16
+ from hapi_schema.db_currency import DBCurrency
17
+ from hapi_schema.db_resource import DBResource
18
+ from hapi_schema.db_wfp_commodity import DBWFPCommodity
19
+ from hapi_schema.db_wfp_market import DBWFPMarket
20
+ from hapi_schema.utils.base import Base
21
+ from hapi_schema.utils.constraints import (
22
+ non_negative_constraint,
23
+ reference_period_constraint,
24
+ )
25
+ from hapi_schema.utils.enums import PriceFlag, PriceType
26
+ from hapi_schema.utils.view_params import ViewParams
27
+
28
+ # normalised table
29
+
30
+
31
+ class DBFoodPrice(Base):
32
+ __tablename__ = "food_price"
33
+ __table_args__ = (
34
+ non_negative_constraint(var_name="price"),
35
+ reference_period_constraint(),
36
+ )
37
+
38
+ resource_hdx_id: Mapped[str] = mapped_column(
39
+ ForeignKey("resource.hdx_id", onupdate="CASCADE", ondelete="CASCADE"),
40
+ nullable=False,
41
+ )
42
+ market_code: Mapped[str] = mapped_column(
43
+ ForeignKey("wfp_market.code"), primary_key=True
44
+ )
45
+ commodity_code: Mapped[str] = mapped_column(
46
+ ForeignKey("wfp_commodity.code"), primary_key=True
47
+ )
48
+ currency_code: Mapped[str] = mapped_column(
49
+ ForeignKey("currency.code", onupdate="CASCADE"),
50
+ nullable=False,
51
+ index=True,
52
+ )
53
+ unit: Mapped[str] = mapped_column(String(32), nullable=False)
54
+ price_flag: Mapped[PriceFlag] = mapped_column(
55
+ Enum(PriceFlag), primary_key=True
56
+ )
57
+ price_type: Mapped[PriceType] = mapped_column(
58
+ Enum(PriceType), primary_key=True
59
+ )
60
+ price: Mapped[float] = mapped_column(Float, nullable=False)
61
+ reference_period_start: Mapped[datetime] = mapped_column(
62
+ DateTime, primary_key=True
63
+ )
64
+ reference_period_end: Mapped[datetime] = mapped_column(
65
+ DateTime, nullable=False, index=True
66
+ )
67
+
68
+ resource = relationship(DBResource)
69
+ market = relationship(DBWFPMarket)
70
+ commodity = relationship(DBWFPCommodity)
71
+ currency = relationship(DBCurrency)
72
+
73
+
74
+ # denormalised view
75
+
76
+
77
+ view_params_food_price = ViewParams(
78
+ name="food_price_view",
79
+ metadata=Base.metadata,
80
+ selectable=select(
81
+ *DBFoodPrice.__table__.columns,
82
+ DBWFPMarket.admin2_ref.label("admin2_ref"),
83
+ DBWFPMarket.name.label("market_name"),
84
+ DBWFPMarket.lat.label("lat"),
85
+ DBWFPMarket.lon.label("lon"),
86
+ DBWFPCommodity.category.label("commodity_category"),
87
+ DBWFPCommodity.name.label("commodity_name"),
88
+ DBLocation.code.label("location_code"),
89
+ DBLocation.name.label("location_name"),
90
+ DBAdmin1.location_ref.label("location_ref"),
91
+ DBAdmin1.code.label("admin1_code"),
92
+ DBAdmin1.name.label("admin1_name"),
93
+ DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
94
+ DBAdmin2.admin1_ref.label("admin1_ref"),
95
+ DBAdmin2.code.label("admin2_code"),
96
+ DBAdmin2.name.label("admin2_name"),
97
+ DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
98
+ ).select_from(
99
+ # the admin2 comes from wfp_market
100
+ DBFoodPrice.__table__.join(
101
+ DBWFPMarket.__table__,
102
+ DBFoodPrice.market_code == DBWFPMarket.code,
103
+ isouter=True,
104
+ )
105
+ .join(
106
+ DBAdmin2.__table__,
107
+ DBWFPMarket.admin2_ref == DBAdmin2.id,
108
+ isouter=True,
109
+ )
110
+ .join(
111
+ DBAdmin1.__table__,
112
+ DBAdmin2.admin1_ref == DBAdmin1.id,
113
+ isouter=True,
114
+ )
115
+ .join(
116
+ DBLocation.__table__,
117
+ DBAdmin1.location_ref == DBLocation.id,
118
+ isouter=True,
119
+ )
120
+ .join(
121
+ DBWFPCommodity.__table__,
122
+ DBFoodPrice.commodity_code == DBWFPCommodity.code,
123
+ isouter=True,
124
+ )
125
+ ),
126
+ )