hapi-schema 0.7.3__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 (118) hide show
  1. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.config/pre-commit-config.yaml +8 -7
  2. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.github/workflows/publish.yaml +1 -1
  3. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.github/workflows/run-python-tests.yaml +19 -3
  4. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/PKG-INFO +4 -3
  5. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/changelog.md +25 -1
  6. {hapi_schema-0.7.3 → 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.3 → hapi_schema-0.8.0}/pyproject.toml +2 -2
  10. hapi_schema-0.8.0/requirements.txt +44 -0
  11. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/_version.py +2 -2
  12. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/db_admin1.py +26 -22
  13. {hapi_schema-0.7.3 → 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.3 → hapi_schema-0.8.0}/src/hapi_schema/db_dataset.py +3 -6
  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.3 → 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.3 → hapi_schema-0.8.0}/src/hapi_schema/db_operational_presence.py +53 -52
  22. hapi_schema-0.8.0/src/hapi_schema/db_org.py +47 -0
  23. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/db_patch.py +16 -15
  24. hapi_schema-0.8.0/src/hapi_schema/db_population.py +125 -0
  25. hapi_schema-0.8.0/src/hapi_schema/db_poverty_rate.py +81 -0
  26. hapi_schema-0.8.0/src/hapi_schema/db_refugees.py +101 -0
  27. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/db_resource.py +23 -23
  28. hapi_schema-0.8.0/src/hapi_schema/utils/constraints.py +56 -0
  29. hapi_schema-0.8.0/src/hapi_schema/utils/enums.py +82 -0
  30. hapi_schema-0.8.0/src/hapi_schema/utils/view_as_table_code_generator.py +125 -0
  31. hapi_schema-0.8.0/src/hapi_schema/views.py +30 -0
  32. hapi_schema-0.8.0/tests/conftest.py +176 -0
  33. hapi_schema-0.8.0/tests/sample_data/data_admin1.py +20 -0
  34. hapi_schema-0.8.0/tests/sample_data/data_admin2.py +38 -0
  35. hapi_schema-0.8.0/tests/sample_data/data_conflict_event.py +22 -0
  36. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_dataset.py +0 -2
  37. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_food_security.py +9 -15
  38. hapi_schema-0.8.0/tests/sample_data/data_funding.py +16 -0
  39. hapi_schema-0.8.0/tests/sample_data/data_humanitarian_needs.py +63 -0
  40. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_location.py +7 -4
  41. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_national_risk.py +3 -5
  42. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_operational_presence.py +15 -24
  43. hapi_schema-0.8.0/tests/sample_data/data_org.py +16 -0
  44. hapi_schema-0.8.0/tests/sample_data/data_patch.py +37 -0
  45. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_population.py +15 -20
  46. hapi_schema-0.8.0/tests/sample_data/data_poverty_rate.py +12 -0
  47. hapi_schema-0.8.0/tests/sample_data/data_refugees.py +30 -0
  48. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_resource.py +3 -6
  49. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_sector.py +4 -0
  50. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_admin1.py +18 -33
  51. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_admin2.py +21 -33
  52. hapi_schema-0.8.0/tests/test_conflict_event.py +74 -0
  53. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_dataset.py +3 -3
  54. hapi_schema-0.8.0/tests/test_food_security.py +82 -0
  55. hapi_schema-0.8.0/tests/test_funding.py +113 -0
  56. hapi_schema-0.8.0/tests/test_humanitarian_needs.py +143 -0
  57. hapi_schema-0.8.0/tests/test_location.py +52 -0
  58. hapi_schema-0.8.0/tests/test_national_risk.py +167 -0
  59. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_operational_presence.py +33 -10
  60. hapi_schema-0.8.0/tests/test_org.py +32 -0
  61. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_org_type.py +2 -2
  62. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_patch.py +8 -4
  63. hapi_schema-0.8.0/tests/test_population.py +127 -0
  64. hapi_schema-0.8.0/tests/test_poverty_rate.py +63 -0
  65. hapi_schema-0.8.0/tests/test_refugees.py +110 -0
  66. hapi_schema-0.8.0/tests/test_resource.py +33 -0
  67. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/test_sector.py +2 -2
  68. hapi_schema-0.7.3/requirements.txt +0 -55
  69. hapi_schema-0.7.3/src/hapi_schema/db_age_range.py +0 -22
  70. hapi_schema-0.7.3/src/hapi_schema/db_food_security.py +0 -138
  71. hapi_schema-0.7.3/src/hapi_schema/db_gender.py +0 -21
  72. hapi_schema-0.7.3/src/hapi_schema/db_humanitarian_needs.py +0 -145
  73. hapi_schema-0.7.3/src/hapi_schema/db_ipc_phase.py +0 -28
  74. hapi_schema-0.7.3/src/hapi_schema/db_ipc_type.py +0 -26
  75. hapi_schema-0.7.3/src/hapi_schema/db_national_risk.py +0 -123
  76. hapi_schema-0.7.3/src/hapi_schema/db_org.py +0 -68
  77. hapi_schema-0.7.3/src/hapi_schema/db_population.py +0 -120
  78. hapi_schema-0.7.3/src/hapi_schema/db_population_group.py +0 -23
  79. hapi_schema-0.7.3/src/hapi_schema/db_population_status.py +0 -23
  80. hapi_schema-0.7.3/tests/conftest.py +0 -124
  81. hapi_schema-0.7.3/tests/sample_data/data_admin1.py +0 -37
  82. hapi_schema-0.7.3/tests/sample_data/data_admin2.py +0 -70
  83. hapi_schema-0.7.3/tests/sample_data/data_age_range.py +0 -4
  84. hapi_schema-0.7.3/tests/sample_data/data_gender.py +0 -5
  85. hapi_schema-0.7.3/tests/sample_data/data_humanitarian_needs.py +0 -68
  86. hapi_schema-0.7.3/tests/sample_data/data_ipc_phase.py +0 -35
  87. hapi_schema-0.7.3/tests/sample_data/data_ipc_type.py +0 -14
  88. hapi_schema-0.7.3/tests/sample_data/data_org.py +0 -34
  89. hapi_schema-0.7.3/tests/sample_data/data_patch.py +0 -31
  90. hapi_schema-0.7.3/tests/sample_data/data_population_group.py +0 -4
  91. hapi_schema-0.7.3/tests/sample_data/data_population_status.py +0 -4
  92. hapi_schema-0.7.3/tests/test_age_range.py +0 -16
  93. hapi_schema-0.7.3/tests/test_food_security.py +0 -47
  94. hapi_schema-0.7.3/tests/test_gender.py +0 -15
  95. hapi_schema-0.7.3/tests/test_humanitarian_needs.py +0 -57
  96. hapi_schema-0.7.3/tests/test_ipc_phase.py +0 -34
  97. hapi_schema-0.7.3/tests/test_ipc_type.py +0 -26
  98. hapi_schema-0.7.3/tests/test_location.py +0 -77
  99. hapi_schema-0.7.3/tests/test_national_risk.py +0 -74
  100. hapi_schema-0.7.3/tests/test_org.py +0 -54
  101. hapi_schema-0.7.3/tests/test_population.py +0 -62
  102. hapi_schema-0.7.3/tests/test_population_group.py +0 -16
  103. hapi_schema-0.7.3/tests/test_population_status.py +0 -15
  104. hapi_schema-0.7.3/tests/test_resource.py +0 -52
  105. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.config/coveragerc +0 -0
  106. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.config/pytest.ini +0 -0
  107. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.config/ruff.toml +0 -0
  108. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/.gitignore +0 -0
  109. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/LICENSE +0 -0
  110. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/README.md +0 -0
  111. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/__init__.py +0 -0
  112. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/db_org_type.py +0 -0
  113. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/db_sector.py +0 -0
  114. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/utils/__init__.py +0 -0
  115. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/utils/base.py +0 -0
  116. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/src/hapi_schema/utils/view_params.py +0 -0
  117. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/__init__.py +0 -0
  118. {hapi_schema-0.7.3 → hapi_schema-0.8.0}/tests/sample_data/data_org_type.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.3
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,7 +5,29 @@ 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.3]
8
+ ## 0.8.0
9
+
10
+ ### Changed
11
+
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
28
+
29
+
30
+ ## [0.7.3]
9
31
 
10
32
  ### Fixed
11
33
 
@@ -29,6 +51,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
29
51
  - Tests on the constraints
30
52
  - `hapi_updated_date` and `hapi_replaced_date` fields
31
53
 
54
+ ## [0.7.0]
55
+
32
56
  ### Changed
33
57
 
34
58
  - Small changes to other dates for HAPI v2
@@ -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.3'
16
- __version_tuple__ = version_tuple = (0, 7, 3)
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
+ )
@@ -1,6 +1,6 @@
1
1
  """Dataset table and view."""
2
2
 
3
- from sqlalchemy import Integer, String, select
3
+ from sqlalchemy import String, select
4
4
  from sqlalchemy.orm import Mapped, mapped_column
5
5
 
6
6
  from hapi_schema.utils.base import Base
@@ -10,12 +10,9 @@ from hapi_schema.utils.view_params import ViewParams
10
10
  class DBDataset(Base):
11
11
  __tablename__ = "dataset"
12
12
 
13
- id: Mapped[int] = mapped_column(Integer, primary_key=True)
14
- hdx_id: Mapped[str] = mapped_column(
15
- String(36), unique=True, nullable=False
16
- )
13
+ hdx_id: Mapped[str] = mapped_column(String(36), primary_key=True)
17
14
  hdx_stub: Mapped[str] = mapped_column(
18
- String(128), unique=True, nullable=False
15
+ String(128), unique=True, nullable=False, index=True
19
16
  )
20
17
  title = mapped_column(String(1024), nullable=False)
21
18
  hdx_provider_stub: Mapped[str] = mapped_column(