hapi-schema 0.6.1__tar.gz → 0.7.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 (88) hide show
  1. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/PKG-INFO +4 -4
  2. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/changelog.md +17 -0
  3. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/pyproject.toml +2 -2
  4. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/requirements.txt +10 -8
  5. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/_version.py +2 -2
  6. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_admin1.py +20 -3
  7. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_admin2.py +21 -3
  8. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_food_security.py +21 -4
  9. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_humanitarian_needs.py +13 -1
  10. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_ipc_phase.py +7 -1
  11. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_ipc_type.py +7 -2
  12. hapi_schema-0.7.0/src/hapi_schema/db_location.py +53 -0
  13. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_national_risk.py +15 -2
  14. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_operational_presence.py +12 -1
  15. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_org.py +17 -2
  16. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_org_type.py +3 -1
  17. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_population.py +14 -2
  18. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_resource.py +14 -1
  19. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_sector.py +1 -9
  20. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/conftest.py +32 -3
  21. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_admin1.py +9 -3
  22. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_admin2.py +18 -6
  23. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_food_security.py +6 -9
  24. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_location.py +2 -0
  25. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_org.py +9 -3
  26. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_resource.py +6 -0
  27. hapi_schema-0.7.0/tests/sample_data/data_sector.py +18 -0
  28. hapi_schema-0.7.0/tests/test_admin1.py +84 -0
  29. hapi_schema-0.7.0/tests/test_admin2.py +85 -0
  30. hapi_schema-0.7.0/tests/test_food_security.py +85 -0
  31. hapi_schema-0.6.1/tests/test_humanitarian_needs_view.py → hapi_schema-0.7.0/tests/test_humanitarian_needs.py +29 -1
  32. hapi_schema-0.6.1/tests/test_ipc_phase_view.py → hapi_schema-0.7.0/tests/test_ipc_phase.py +14 -1
  33. hapi_schema-0.6.1/tests/test_ipc_type_view.py → hapi_schema-0.7.0/tests/test_ipc_type.py +10 -1
  34. hapi_schema-0.7.0/tests/test_location.py +77 -0
  35. hapi_schema-0.7.0/tests/test_national_risk.py +74 -0
  36. hapi_schema-0.6.1/tests/test_operational_presence_view.py → hapi_schema-0.7.0/tests/test_operational_presence.py +21 -0
  37. hapi_schema-0.7.0/tests/test_org.py +54 -0
  38. hapi_schema-0.7.0/tests/test_population.py +62 -0
  39. hapi_schema-0.7.0/tests/test_resource.py +52 -0
  40. hapi_schema-0.6.1/src/hapi_schema/db_location.py +0 -30
  41. hapi_schema-0.6.1/tests/sample_data/data_sector.py +0 -28
  42. hapi_schema-0.6.1/tests/test_admin1_view.py +0 -15
  43. hapi_schema-0.6.1/tests/test_admin2_view.py +0 -16
  44. hapi_schema-0.6.1/tests/test_food_security_view.py +0 -22
  45. hapi_schema-0.6.1/tests/test_location_view.py +0 -16
  46. hapi_schema-0.6.1/tests/test_national_risk_view.py +0 -21
  47. hapi_schema-0.6.1/tests/test_org_view.py +0 -16
  48. hapi_schema-0.6.1/tests/test_population_view.py +0 -22
  49. hapi_schema-0.6.1/tests/test_resource_view.py +0 -16
  50. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.config/coveragerc +0 -0
  51. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.config/pre-commit-config.yaml +0 -0
  52. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.config/pytest.ini +0 -0
  53. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.config/ruff.toml +0 -0
  54. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.github/workflows/publish.yaml +0 -0
  55. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.github/workflows/run-python-tests.yaml +0 -0
  56. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/.gitignore +0 -0
  57. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/LICENSE +0 -0
  58. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/README.md +0 -0
  59. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/contributing.md +0 -0
  60. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/__init__.py +0 -0
  61. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_age_range.py +0 -0
  62. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_dataset.py +0 -0
  63. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_gender.py +0 -0
  64. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_population_group.py +0 -0
  65. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/db_population_status.py +0 -0
  66. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/utils/__init__.py +0 -0
  67. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/utils/base.py +0 -0
  68. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/src/hapi_schema/utils/view_params.py +0 -0
  69. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/__init__.py +0 -0
  70. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_age_range.py +0 -0
  71. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_dataset.py +0 -0
  72. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_gender.py +0 -0
  73. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_humanitarian_needs.py +0 -0
  74. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_ipc_phase.py +0 -0
  75. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_ipc_type.py +0 -0
  76. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_national_risk.py +0 -0
  77. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_operational_presence.py +0 -0
  78. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_org_type.py +0 -0
  79. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_population.py +0 -0
  80. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_population_group.py +0 -0
  81. {hapi_schema-0.6.1 → hapi_schema-0.7.0}/tests/sample_data/data_population_status.py +0 -0
  82. /hapi_schema-0.6.1/tests/test_age_range_view.py → /hapi_schema-0.7.0/tests/test_age_range.py +0 -0
  83. /hapi_schema-0.6.1/tests/test_dataset_view.py → /hapi_schema-0.7.0/tests/test_dataset.py +0 -0
  84. /hapi_schema-0.6.1/tests/test_gender_view.py → /hapi_schema-0.7.0/tests/test_gender.py +0 -0
  85. /hapi_schema-0.6.1/tests/test_org_type_view.py → /hapi_schema-0.7.0/tests/test_org_type.py +0 -0
  86. /hapi_schema-0.6.1/tests/test_population_group_view.py → /hapi_schema-0.7.0/tests/test_population_group.py +0 -0
  87. /hapi_schema-0.6.1/tests/test_population_status_view.py → /hapi_schema-0.7.0/tests/test_population_status.py +0 -0
  88. /hapi_schema-0.6.1/tests/test_sector_view.py → /hapi_schema-0.7.0/tests/test_sector.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: hapi-schema
3
- Version: 0.6.1
3
+ Version: 0.7.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,11 @@ 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.2.9; extra == 'database'
29
+ Requires-Dist: hdx-python-database>=1.3.0; 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.2.9; extra == 'test'
33
+ Requires-Dist: hdx-python-database>=1.3.0; extra == 'test'
34
34
  Requires-Dist: pytest; extra == 'test'
35
35
  Requires-Dist: pytest-cov; extra == 'test'
36
36
  Description-Content-Type: text/markdown
@@ -5,6 +5,23 @@ 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.0]
9
+
10
+ ### Added
11
+ - Missing constraints
12
+ - Tests on the constraints
13
+ - `hapi_updated_date` and `hapi_replaced_date` fields
14
+
15
+ ### Changed
16
+
17
+ - Small changes to other dates for HAPI v2
18
+
19
+ ## [0.6.2]
20
+
21
+ ### Changed
22
+
23
+ - Update requirements
24
+
8
25
  ## [0.6.1]
9
26
 
10
27
  ### Changed
@@ -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.2.9"]
45
+ database = ["hdx-python-database>=1.3.0"]
46
46
  dev = ["pre-commit"]
47
- test = ["hdx-python-database>=1.2.9", "pytest", "pytest-cov"]
47
+ test = ["hdx-python-database>=1.3.0", "pytest", "pytest-cov"]
48
48
 
49
49
  #########
50
50
  # Hatch #
@@ -6,17 +6,19 @@
6
6
  #
7
7
  cfgv==3.4.0
8
8
  # via pre-commit
9
- coverage[toml]==7.4.1
10
- # via pytest-cov
9
+ coverage[toml]==7.4.3
10
+ # via
11
+ # coverage
12
+ # pytest-cov
11
13
  distlib==0.3.8
12
14
  # via virtualenv
13
15
  filelock==3.13.1
14
16
  # via virtualenv
15
17
  greenlet==3.0.3
16
18
  # via sqlalchemy
17
- hdx-python-database==1.2.9
19
+ hdx-python-database==1.3.0
18
20
  # via hapi-schema (pyproject.toml)
19
- identify==2.5.33
21
+ identify==2.5.35
20
22
  # via pre-commit
21
23
  iniconfig==2.0.0
22
24
  # via pytest
@@ -28,9 +30,9 @@ platformdirs==4.2.0
28
30
  # via virtualenv
29
31
  pluggy==1.4.0
30
32
  # via pytest
31
- pre-commit==3.6.0
33
+ pre-commit==3.6.2
32
34
  # via hapi-schema (pyproject.toml)
33
- pytest==8.0.0
35
+ pytest==8.0.2
34
36
  # via
35
37
  # hapi-schema (pyproject.toml)
36
38
  # pytest-cov
@@ -38,13 +40,13 @@ pytest-cov==4.1.0
38
40
  # via hapi-schema (pyproject.toml)
39
41
  pyyaml==6.0.1
40
42
  # via pre-commit
41
- sqlalchemy==2.0.25
43
+ sqlalchemy==2.0.27
42
44
  # via
43
45
  # hapi-schema (pyproject.toml)
44
46
  # hdx-python-database
45
47
  typing-extensions==4.9.0
46
48
  # via sqlalchemy
47
- virtualenv==20.25.0
49
+ virtualenv==20.25.1
48
50
  # via pre-commit
49
51
 
50
52
  # The following packages are considered to be unsafe in a requirements file:
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.6.1'
16
- __version_tuple__ = version_tuple = (0, 6, 1)
15
+ __version__ = version = '0.7.0'
16
+ __version_tuple__ = version_tuple = (0, 7, 0)
@@ -4,10 +4,12 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  Boolean,
7
+ CheckConstraint,
7
8
  DateTime,
8
9
  ForeignKey,
9
10
  Integer,
10
11
  String,
12
+ UniqueConstraint,
11
13
  select,
12
14
  text,
13
15
  )
@@ -20,22 +22,37 @@ from hapi_schema.utils.view_params import ViewParams
20
22
 
21
23
  class DBAdmin1(Base):
22
24
  __tablename__ = "admin1"
25
+ __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"),
35
+ )
23
36
 
24
37
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
25
38
  location_ref: Mapped[int] = mapped_column(
26
39
  ForeignKey("location.id", onupdate="CASCADE", ondelete="CASCADE"),
27
40
  nullable=False,
28
41
  )
29
- code: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
42
+ code: Mapped[str] = mapped_column(String(128), nullable=False)
30
43
  name: Mapped[str] = mapped_column(String(512), nullable=False)
31
44
  is_unspecified: Mapped[bool] = mapped_column(
32
45
  Boolean, server_default=text("FALSE")
33
46
  )
34
47
  reference_period_start: Mapped[datetime] = mapped_column(
35
- DateTime, nullable=False
48
+ DateTime, nullable=True, server_default=text("NULL"), index=True
36
49
  )
37
50
  reference_period_end: Mapped[datetime] = mapped_column(
38
- DateTime, nullable=True, server_default=text("NULL")
51
+ DateTime, nullable=True, server_default=text("NULL"), index=True
52
+ )
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
39
56
  )
40
57
 
41
58
  location = relationship("DBLocation")
@@ -4,10 +4,12 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  Boolean,
7
+ CheckConstraint,
7
8
  DateTime,
8
9
  ForeignKey,
9
10
  Integer,
10
11
  String,
12
+ UniqueConstraint,
11
13
  select,
12
14
  text,
13
15
  )
@@ -21,22 +23,38 @@ from hapi_schema.utils.view_params import ViewParams
21
23
 
22
24
  class DBAdmin2(Base):
23
25
  __tablename__ = "admin2"
26
+ __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", name="code_date_unique"),
36
+ )
24
37
 
25
38
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
26
39
  admin1_ref: Mapped[int] = mapped_column(
27
40
  ForeignKey("admin1.id", onupdate="CASCADE", ondelete="CASCADE"),
28
41
  nullable=False,
29
42
  )
30
- code: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
43
+ code: Mapped[str] = mapped_column(String(128), nullable=False)
31
44
  name: Mapped[str] = mapped_column(String(512), nullable=False)
32
45
  is_unspecified: Mapped[bool] = mapped_column(
33
46
  Boolean, server_default=text("FALSE")
34
47
  )
35
48
  reference_period_start: Mapped[datetime] = mapped_column(
36
- DateTime, nullable=False
49
+ DateTime, nullable=True, server_default=text("NULL"), index=True
37
50
  )
38
51
  reference_period_end: Mapped[datetime] = mapped_column(
39
- DateTime, nullable=True, server_default=text("NULL")
52
+ DateTime, nullable=True, server_default=text("NULL"), index=True
53
+ )
54
+
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
40
58
  )
41
59
 
42
60
  admin1 = relationship("DBAdmin1")
@@ -3,6 +3,7 @@
3
3
  from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
+ CheckConstraint,
6
7
  DateTime,
7
8
  Float,
8
9
  ForeignKey,
@@ -26,6 +27,16 @@ from hapi_schema.utils.view_params import ViewParams
26
27
 
27
28
  class DBFoodSecurity(Base):
28
29
  __tablename__ = "food_security"
30
+ __table_args__ = (
31
+ CheckConstraint(
32
+ "population_fraction_in_phase >= 0 AND population_fraction_in_phase <=1",
33
+ name="population_fraction_in_phase",
34
+ ),
35
+ CheckConstraint(
36
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
37
+ name="reference_period",
38
+ ),
39
+ )
29
40
 
30
41
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
31
42
  resource_ref: Mapped[int] = mapped_column(
@@ -41,15 +52,17 @@ class DBFoodSecurity(Base):
41
52
  ipc_type_code: Mapped[str] = mapped_column(
42
53
  ForeignKey("ipc_type.code", onupdate="CASCADE"), nullable=False
43
54
  )
44
- population_in_phase: Mapped[int] = mapped_column(Integer, nullable=False)
55
+ population_in_phase: Mapped[int] = mapped_column(
56
+ Integer, nullable=False, index=True
57
+ )
45
58
  population_fraction_in_phase: Mapped[float] = mapped_column(
46
- Float, nullable=False
59
+ Float, nullable=False, index=True
47
60
  )
48
61
  reference_period_start: Mapped[datetime] = mapped_column(
49
- DateTime, nullable=False
62
+ DateTime, nullable=False, index=True
50
63
  )
51
64
  reference_period_end: Mapped[datetime] = mapped_column(
52
- DateTime, nullable=True, server_default=text("NULL")
65
+ DateTime, nullable=True, server_default=text("NULL"), index=True
53
66
  )
54
67
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
55
68
 
@@ -73,14 +86,18 @@ view_params_food_security = ViewParams(
73
86
  DBResource.hdx_id.label("resource_hdx_id"),
74
87
  DBResource.name.label("resource_name"),
75
88
  DBResource.update_date.label("resource_update_date"),
89
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
90
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
76
91
  DBLocation.code.label("location_code"),
77
92
  DBLocation.name.label("location_name"),
78
93
  DBAdmin1.code.label("admin1_code"),
79
94
  DBAdmin1.name.label("admin1_name"),
80
95
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
96
+ DBAdmin1.location_ref.label("location_ref"),
81
97
  DBAdmin2.code.label("admin2_code"),
82
98
  DBAdmin2.name.label("admin2_name"),
83
99
  DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
100
+ DBAdmin2.admin1_ref.label("admin1_ref"),
84
101
  ).select_from(
85
102
  # Join pop to admin2 to admin1 to loc
86
103
  DBFoodSecurity.__table__.join(
@@ -4,6 +4,7 @@ from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
6
  Boolean,
7
+ CheckConstraint,
7
8
  DateTime,
8
9
  ForeignKey,
9
10
  Integer,
@@ -25,6 +26,13 @@ from hapi_schema.utils.view_params import ViewParams
25
26
 
26
27
  class DBHumanitarianNeeds(Base):
27
28
  __tablename__ = "humanitarian_needs"
29
+ __table_args__ = (
30
+ CheckConstraint("population >= 0", name="population"),
31
+ CheckConstraint(
32
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
33
+ name="reference_period",
34
+ ),
35
+ )
28
36
 
29
37
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
30
38
  resource_ref: Mapped[int] = mapped_column(
@@ -60,7 +68,7 @@ class DBHumanitarianNeeds(Base):
60
68
  DateTime, nullable=False, index=True
61
69
  )
62
70
  reference_period_end: Mapped[datetime] = mapped_column(
63
- DateTime, nullable=True, server_default=text("NULL")
71
+ DateTime, nullable=True, server_default=text("NULL"), index=True
64
72
  )
65
73
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
66
74
 
@@ -86,14 +94,18 @@ view_params_humanitarian_needs = ViewParams(
86
94
  DBResource.hdx_id.label("resource_hdx_id"),
87
95
  DBResource.name.label("resource_name"),
88
96
  DBResource.update_date.label("resource_update_date"),
97
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
98
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
89
99
  DBLocation.code.label("location_code"),
90
100
  DBLocation.name.label("location_name"),
91
101
  DBAdmin1.code.label("admin1_code"),
92
102
  DBAdmin1.name.label("admin1_name"),
93
103
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
104
+ DBAdmin1.location_ref.label("location_ref"),
94
105
  DBAdmin2.code.label("admin2_code"),
95
106
  DBAdmin2.name.label("admin2_name"),
96
107
  DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
108
+ DBAdmin2.admin1_ref.label("admin1_ref"),
97
109
  DBSector.name.label("sector_name"),
98
110
  ).select_from(
99
111
  # Join pop to admin2 to admin1 to loc
@@ -1,6 +1,6 @@
1
1
  """IPC phase table and view."""
2
2
 
3
- from sqlalchemy import String, select
3
+ from sqlalchemy import CheckConstraint, String, select
4
4
  from sqlalchemy.orm import Mapped, mapped_column
5
5
 
6
6
  from hapi_schema.utils.base import Base
@@ -9,6 +9,12 @@ from hapi_schema.utils.view_params import ViewParams
9
9
 
10
10
  class DBIpcPhase(Base):
11
11
  __tablename__ = "ipc_phase"
12
+ __table_args__ = (
13
+ CheckConstraint(
14
+ "code IN ('1', '2', '3', '4', '5', '3+', 'all')",
15
+ name="ipc_phase_code",
16
+ ),
17
+ )
12
18
 
13
19
  code: Mapped[int] = mapped_column(String(32), primary_key=True)
14
20
  name: Mapped[str] = mapped_column(String(32), nullable=False)
@@ -1,6 +1,6 @@
1
1
  """IPC type table and view."""
2
2
 
3
- from sqlalchemy import String, select
3
+ from sqlalchemy import CheckConstraint, String, select
4
4
  from sqlalchemy.orm import Mapped, mapped_column
5
5
 
6
6
  from hapi_schema.utils.base import Base
@@ -9,7 +9,12 @@ from hapi_schema.utils.view_params import ViewParams
9
9
 
10
10
  class DBIpcType(Base):
11
11
  __tablename__ = "ipc_type"
12
-
12
+ __table_args__ = (
13
+ CheckConstraint(
14
+ 'code IN ("current", "first projection", "second projection")',
15
+ name="ipc_phase_type",
16
+ ),
17
+ )
13
18
  code: Mapped[str] = mapped_column(String(32), primary_key=True)
14
19
  description: Mapped[str] = mapped_column(String(512), nullable=False)
15
20
 
@@ -0,0 +1,53 @@
1
+ """Location table and view."""
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ CheckConstraint,
7
+ DateTime,
8
+ Integer,
9
+ String,
10
+ UniqueConstraint,
11
+ select,
12
+ text,
13
+ )
14
+ from sqlalchemy.orm import Mapped, mapped_column
15
+
16
+ from hapi_schema.utils.base import Base
17
+ from hapi_schema.utils.view_params import ViewParams
18
+
19
+
20
+ class DBLocation(Base):
21
+ __tablename__ = "location"
22
+ __table_args__ = (
23
+ CheckConstraint(
24
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
25
+ name="reference_period",
26
+ ),
27
+ CheckConstraint(
28
+ "(hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_updated_date)",
29
+ name="hapi_dates",
30
+ ),
31
+ UniqueConstraint("code", "hapi_updated_date", name="code_date_unique"),
32
+ )
33
+
34
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
35
+ code: Mapped[str] = mapped_column(String(128), nullable=False)
36
+ name: Mapped[str] = mapped_column(String(512), nullable=False)
37
+ reference_period_start: Mapped[datetime] = mapped_column(
38
+ DateTime, nullable=True, server_default=text("NULL"), index=True
39
+ )
40
+ reference_period_end: Mapped[datetime] = mapped_column(
41
+ DateTime, nullable=True, server_default=text("NULL"), index=True
42
+ )
43
+ hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
44
+ hapi_replaced_date: Mapped[datetime] = mapped_column(
45
+ DateTime, nullable=True, server_default=text("NULL"), index=True
46
+ )
47
+
48
+
49
+ view_params_location = ViewParams(
50
+ name="location_view",
51
+ metadata=Base.metadata,
52
+ selectable=select(*DBLocation.__table__.columns),
53
+ )
@@ -3,6 +3,7 @@
3
3
  from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
+ CheckConstraint,
6
7
  DateTime,
7
8
  Float,
8
9
  ForeignKey,
@@ -24,6 +25,16 @@ from hapi_schema.utils.view_params import ViewParams
24
25
 
25
26
  class DBNationalRisk(Base):
26
27
  __tablename__ = "national_risk"
28
+ __table_args__ = (
29
+ CheckConstraint(
30
+ "meta_avg_recentness_years >= 0.0",
31
+ name="meta_avg_recentness_years",
32
+ ),
33
+ CheckConstraint(
34
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
35
+ name="reference_period",
36
+ ),
37
+ )
27
38
 
28
39
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
29
40
  resource_ref: Mapped[int] = mapped_column(
@@ -46,10 +57,10 @@ class DBNationalRisk(Base):
46
57
  Float, nullable=True
47
58
  )
48
59
  reference_period_start: Mapped[datetime] = mapped_column(
49
- DateTime, nullable=False, index=True
60
+ DateTime, nullable=True, server_default=text("NULL"), index=True
50
61
  )
51
62
  reference_period_end: Mapped[datetime] = mapped_column(
52
- DateTime, nullable=True, server_default=text("NULL")
63
+ DateTime, nullable=True, server_default=text("NULL"), index=True
53
64
  )
54
65
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
55
66
 
@@ -70,6 +81,8 @@ view_params_national_risk = ViewParams(
70
81
  DBResource.hdx_id.label("resource_hdx_id"),
71
82
  DBResource.name.label("resource_name"),
72
83
  DBResource.update_date.label("resource_update_date"),
84
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
85
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
73
86
  DBLocation.code.label("location_code"),
74
87
  DBLocation.name.label("location_name"),
75
88
  DBAdmin1.code.label("admin1_code"),
@@ -3,6 +3,7 @@
3
3
  from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
+ CheckConstraint,
6
7
  DateTime,
7
8
  ForeignKey,
8
9
  Integer,
@@ -26,6 +27,12 @@ from hapi_schema.utils.view_params import ViewParams
26
27
 
27
28
  class DBOperationalPresence(Base):
28
29
  __tablename__ = "operational_presence"
30
+ __table_args__ = (
31
+ CheckConstraint(
32
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
33
+ name="reference_period",
34
+ ),
35
+ )
29
36
 
30
37
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
31
38
  resource_ref = mapped_column(
@@ -45,7 +52,7 @@ class DBOperationalPresence(Base):
45
52
  DateTime, nullable=False, index=True
46
53
  )
47
54
  reference_period_end: Mapped[datetime] = mapped_column(
48
- DateTime, nullable=True, server_default=text("NULL")
55
+ DateTime, nullable=True, server_default=text("NULL"), index=True
49
56
  )
50
57
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
51
58
 
@@ -68,14 +75,18 @@ view_params_operational_presence = ViewParams(
68
75
  DBResource.hdx_id.label("resource_hdx_id"),
69
76
  DBResource.name.label("resource_name"),
70
77
  DBResource.update_date.label("resource_update_date"),
78
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
79
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
71
80
  DBLocation.code.label("location_code"),
72
81
  DBLocation.name.label("location_name"),
73
82
  DBAdmin1.code.label("admin1_code"),
74
83
  DBAdmin1.name.label("admin1_name"),
75
84
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
85
+ DBAdmin1.location_ref.label("location_ref"),
76
86
  DBAdmin2.code.label("admin2_code"),
77
87
  DBAdmin2.name.label("admin2_name"),
78
88
  DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
89
+ DBAdmin2.admin1_ref.label("admin1_ref"),
79
90
  DBOrg.acronym.label("org_acronym"),
80
91
  DBOrg.name.label("org_name"),
81
92
  DBOrg.org_type_code.label("org_type_code"),
@@ -3,6 +3,7 @@
3
3
  from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
+ CheckConstraint,
6
7
  DateTime,
7
8
  ForeignKey,
8
9
  Integer,
@@ -19,6 +20,16 @@ from hapi_schema.utils.view_params import ViewParams
19
20
 
20
21
  class DBOrg(Base):
21
22
  __tablename__ = "org"
23
+ __table_args__ = (
24
+ CheckConstraint(
25
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
26
+ name="reference_period",
27
+ ),
28
+ CheckConstraint(
29
+ "(hapi_replaced_date IS NULL) OR (hapi_replaced_date >= hapi_updated_date)",
30
+ name="hapi_dates",
31
+ ),
32
+ )
22
33
 
23
34
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
24
35
  acronym = mapped_column(String(32), nullable=False, index=True)
@@ -28,10 +39,14 @@ class DBOrg(Base):
28
39
  nullable=True,
29
40
  )
30
41
  reference_period_start: Mapped[datetime] = mapped_column(
31
- DateTime, nullable=False, index=True
42
+ DateTime, nullable=True, server_default=text("NULL"), index=True
32
43
  )
33
44
  reference_period_end: Mapped[datetime] = mapped_column(
34
- DateTime, nullable=True, server_default=text("NULL")
45
+ DateTime, nullable=True, server_default=text("NULL"), index=True
46
+ )
47
+ hapi_updated_date = mapped_column(DateTime, nullable=False, index=True)
48
+ hapi_replaced_date: Mapped[datetime] = mapped_column(
49
+ DateTime, nullable=True, server_default=text("NULL"), index=True
35
50
  )
36
51
 
37
52
  org_type = relationship("DBOrgType")
@@ -11,7 +11,9 @@ class DBOrgType(Base):
11
11
  __tablename__ = "org_type"
12
12
 
13
13
  code: Mapped[str] = mapped_column(String(32), primary_key=True)
14
- description: Mapped[str] = mapped_column(String(512), nullable=False)
14
+ description: Mapped[str] = mapped_column(
15
+ String(512), nullable=False, index=True
16
+ )
15
17
 
16
18
 
17
19
  view_params_org_type = ViewParams(
@@ -3,6 +3,7 @@
3
3
  from datetime import datetime
4
4
 
5
5
  from sqlalchemy import (
6
+ CheckConstraint,
6
7
  DateTime,
7
8
  ForeignKey,
8
9
  Integer,
@@ -23,6 +24,13 @@ from hapi_schema.utils.view_params import ViewParams
23
24
 
24
25
  class DBPopulation(Base):
25
26
  __tablename__ = "population"
27
+ __table_args__ = (
28
+ CheckConstraint("population >= 0", name="population"),
29
+ CheckConstraint(
30
+ "(reference_period_end >= reference_period_start) OR (reference_period_start IS NULL)",
31
+ name="reference_period",
32
+ ),
33
+ )
26
34
 
27
35
  id: Mapped[int] = mapped_column(Integer, primary_key=True)
28
36
  resource_ref: Mapped[int] = mapped_column(
@@ -42,10 +50,10 @@ class DBPopulation(Base):
42
50
  Integer, nullable=False, index=True
43
51
  )
44
52
  reference_period_start: Mapped[datetime] = mapped_column(
45
- DateTime, nullable=False
53
+ DateTime, nullable=False, index=True
46
54
  )
47
55
  reference_period_end: Mapped[datetime] = mapped_column(
48
- DateTime, nullable=True, server_default=text("NULL")
56
+ DateTime, nullable=True, server_default=text("NULL"), index=True
49
57
  )
50
58
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
51
59
 
@@ -68,14 +76,18 @@ view_params_population = ViewParams(
68
76
  DBResource.hdx_id.label("resource_hdx_id"),
69
77
  DBResource.name.label("resource_name"),
70
78
  DBResource.update_date.label("resource_update_date"),
79
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
80
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
71
81
  DBLocation.code.label("location_code"),
72
82
  DBLocation.name.label("location_name"),
73
83
  DBAdmin1.code.label("admin1_code"),
74
84
  DBAdmin1.name.label("admin1_name"),
75
85
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
86
+ DBAdmin1.location_ref.label("location_ref"),
76
87
  DBAdmin2.code.label("admin2_code"),
77
88
  DBAdmin2.name.label("admin2_name"),
78
89
  DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
90
+ DBAdmin2.admin1_ref.label("admin1_ref"),
79
91
  ).select_from(
80
92
  # Join pop to admin2 to admin1 to loc
81
93
  DBPopulation.__table__.join(