hapi-schema 0.6.2__tar.gz → 0.7.1__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 (90) hide show
  1. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.config/ruff.toml +4 -1
  2. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/PKG-INFO +2 -2
  3. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/changelog.md +23 -0
  4. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/contributing.md +4 -0
  5. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/requirements.txt +11 -9
  6. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/_version.py +2 -2
  7. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_admin1.py +20 -3
  8. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_admin2.py +21 -3
  9. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_food_security.py +17 -4
  10. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_humanitarian_needs.py +13 -1
  11. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_ipc_phase.py +7 -1
  12. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_ipc_type.py +7 -2
  13. hapi_schema-0.7.1/src/hapi_schema/db_location.py +53 -0
  14. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_national_risk.py +15 -2
  15. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_operational_presence.py +12 -1
  16. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_org.py +17 -2
  17. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_org_type.py +3 -1
  18. hapi_schema-0.7.1/src/hapi_schema/db_patches.py +33 -0
  19. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_population.py +14 -2
  20. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_resource.py +14 -1
  21. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_sector.py +1 -9
  22. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/conftest.py +36 -3
  23. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_admin1.py +9 -3
  24. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_admin2.py +18 -6
  25. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_food_security.py +6 -9
  26. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_location.py +2 -0
  27. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_org.py +9 -3
  28. hapi_schema-0.7.1/tests/sample_data/data_patches.py +31 -0
  29. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_resource.py +6 -0
  30. hapi_schema-0.7.1/tests/sample_data/data_sector.py +18 -0
  31. hapi_schema-0.7.1/tests/test_admin1.py +84 -0
  32. hapi_schema-0.7.1/tests/test_admin2.py +85 -0
  33. hapi_schema-0.6.2/tests/test_food_security_view.py → hapi_schema-0.7.1/tests/test_food_security.py +26 -1
  34. hapi_schema-0.6.2/tests/test_humanitarian_needs_view.py → hapi_schema-0.7.1/tests/test_humanitarian_needs.py +29 -1
  35. hapi_schema-0.6.2/tests/test_ipc_phase_view.py → hapi_schema-0.7.1/tests/test_ipc_phase.py +14 -1
  36. hapi_schema-0.6.2/tests/test_ipc_type_view.py → hapi_schema-0.7.1/tests/test_ipc_type.py +10 -1
  37. hapi_schema-0.7.1/tests/test_location.py +77 -0
  38. hapi_schema-0.7.1/tests/test_national_risk.py +74 -0
  39. hapi_schema-0.6.2/tests/test_operational_presence_view.py → hapi_schema-0.7.1/tests/test_operational_presence.py +21 -0
  40. hapi_schema-0.7.1/tests/test_org.py +54 -0
  41. hapi_schema-0.7.1/tests/test_patches.py +28 -0
  42. hapi_schema-0.7.1/tests/test_population.py +62 -0
  43. hapi_schema-0.7.1/tests/test_resource.py +52 -0
  44. hapi_schema-0.6.2/src/hapi_schema/db_location.py +0 -30
  45. hapi_schema-0.6.2/tests/sample_data/data_sector.py +0 -28
  46. hapi_schema-0.6.2/tests/test_admin1_view.py +0 -15
  47. hapi_schema-0.6.2/tests/test_admin2_view.py +0 -16
  48. hapi_schema-0.6.2/tests/test_location_view.py +0 -16
  49. hapi_schema-0.6.2/tests/test_national_risk_view.py +0 -21
  50. hapi_schema-0.6.2/tests/test_org_view.py +0 -16
  51. hapi_schema-0.6.2/tests/test_population_view.py +0 -22
  52. hapi_schema-0.6.2/tests/test_resource_view.py +0 -16
  53. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.config/coveragerc +0 -0
  54. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.config/pre-commit-config.yaml +0 -0
  55. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.config/pytest.ini +0 -0
  56. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.github/workflows/publish.yaml +0 -0
  57. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.github/workflows/run-python-tests.yaml +0 -0
  58. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/.gitignore +0 -0
  59. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/LICENSE +0 -0
  60. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/README.md +0 -0
  61. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/pyproject.toml +0 -0
  62. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/__init__.py +0 -0
  63. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_age_range.py +0 -0
  64. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_dataset.py +0 -0
  65. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_gender.py +0 -0
  66. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_population_group.py +0 -0
  67. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/db_population_status.py +0 -0
  68. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/utils/__init__.py +0 -0
  69. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/utils/base.py +0 -0
  70. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/src/hapi_schema/utils/view_params.py +0 -0
  71. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/__init__.py +0 -0
  72. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_age_range.py +0 -0
  73. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_dataset.py +0 -0
  74. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_gender.py +0 -0
  75. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_humanitarian_needs.py +0 -0
  76. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_ipc_phase.py +0 -0
  77. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_ipc_type.py +0 -0
  78. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_national_risk.py +0 -0
  79. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_operational_presence.py +0 -0
  80. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_org_type.py +0 -0
  81. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_population.py +0 -0
  82. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_population_group.py +0 -0
  83. {hapi_schema-0.6.2 → hapi_schema-0.7.1}/tests/sample_data/data_population_status.py +0 -0
  84. /hapi_schema-0.6.2/tests/test_age_range_view.py → /hapi_schema-0.7.1/tests/test_age_range.py +0 -0
  85. /hapi_schema-0.6.2/tests/test_dataset_view.py → /hapi_schema-0.7.1/tests/test_dataset.py +0 -0
  86. /hapi_schema-0.6.2/tests/test_gender_view.py → /hapi_schema-0.7.1/tests/test_gender.py +0 -0
  87. /hapi_schema-0.6.2/tests/test_org_type_view.py → /hapi_schema-0.7.1/tests/test_org_type.py +0 -0
  88. /hapi_schema-0.6.2/tests/test_population_group_view.py → /hapi_schema-0.7.1/tests/test_population_group.py +0 -0
  89. /hapi_schema-0.6.2/tests/test_population_status_view.py → /hapi_schema-0.7.1/tests/test_population_status.py +0 -0
  90. /hapi_schema-0.6.2/tests/test_sector_view.py → /hapi_schema-0.7.1/tests/test_sector.py +0 -0
@@ -9,7 +9,10 @@ select = [
9
9
  "I" # isort
10
10
  ]
11
11
  ignore = [
12
- "E501" # Line too long
12
+ "E501", # Line too long
13
+ "D100", # missing-module-docstring
14
+ "D101", # missing-class-docstring
15
+ "D103" # missing-function-docstring
13
16
  ]
14
17
 
15
18
  [lint.isort]
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: hapi-schema
3
- Version: 0.6.2
3
+ Version: 0.7.1
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>
@@ -5,6 +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.1]
9
+
10
+ ### Changed
11
+
12
+ - Removed constraint from food security
13
+
14
+ ## [0.7.0]
15
+
16
+ ### Added
17
+ - Missing constraints
18
+ - Tests on the constraints
19
+ - `hapi_updated_date` and `hapi_replaced_date` fields
20
+
21
+ ### Changed
22
+
23
+ - Small changes to other dates for HAPI v2
24
+
25
+ ## [0.6.2]
26
+
27
+ ### Changed
28
+
29
+ - Update requirements
30
+
8
31
  ## [0.6.1]
9
32
 
10
33
  ### Changed
@@ -13,6 +13,10 @@ development by running:
13
13
 
14
14
  pip install -r requirements.txt
15
15
 
16
+ and then make an editable installation of the package:
17
+
18
+ pip install -e .
19
+
16
20
  ## Pre-Commit
17
21
 
18
22
  Also be sure to install `pre-commit`, which is run every time
@@ -6,13 +6,15 @@
6
6
  #
7
7
  cfgv==3.4.0
8
8
  # via pre-commit
9
- coverage[toml]==7.4.3
9
+ colorama==0.4.6
10
+ # via pytest
11
+ coverage[toml]==7.4.4
10
12
  # via
11
13
  # coverage
12
14
  # pytest-cov
13
15
  distlib==0.3.8
14
16
  # via virtualenv
15
- filelock==3.13.1
17
+ filelock==3.13.4
16
18
  # via virtualenv
17
19
  greenlet==3.0.3
18
20
  # via sqlalchemy
@@ -24,29 +26,29 @@ iniconfig==2.0.0
24
26
  # via pytest
25
27
  nodeenv==1.8.0
26
28
  # via pre-commit
27
- packaging==23.2
29
+ packaging==24.0
28
30
  # via pytest
29
31
  platformdirs==4.2.0
30
32
  # via virtualenv
31
33
  pluggy==1.4.0
32
34
  # via pytest
33
- pre-commit==3.6.2
35
+ pre-commit==3.7.0
34
36
  # via hapi-schema (pyproject.toml)
35
- pytest==8.0.2
37
+ pytest==8.1.1
36
38
  # via
37
39
  # hapi-schema (pyproject.toml)
38
40
  # pytest-cov
39
- pytest-cov==4.1.0
41
+ pytest-cov==5.0.0
40
42
  # via hapi-schema (pyproject.toml)
41
43
  pyyaml==6.0.1
42
44
  # via pre-commit
43
- sqlalchemy==2.0.27
45
+ sqlalchemy==2.0.29
44
46
  # via
45
47
  # hapi-schema (pyproject.toml)
46
48
  # hdx-python-database
47
- typing-extensions==4.9.0
49
+ typing-extensions==4.11.0
48
50
  # via sqlalchemy
49
- virtualenv==20.25.1
51
+ virtualenv==20.25.3
50
52
  # via pre-commit
51
53
 
52
54
  # 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.2'
16
- __version_tuple__ = version_tuple = (0, 6, 2)
15
+ __version__ = version = '0.7.1'
16
+ __version_tuple__ = version_tuple = (0, 7, 1)
@@ -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,12 @@ 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
+ "(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[int] = mapped_column(
@@ -41,15 +48,17 @@ class DBFoodSecurity(Base):
41
48
  ipc_type_code: Mapped[str] = mapped_column(
42
49
  ForeignKey("ipc_type.code", onupdate="CASCADE"), nullable=False
43
50
  )
44
- population_in_phase: Mapped[int] = mapped_column(Integer, nullable=False)
51
+ population_in_phase: Mapped[int] = mapped_column(
52
+ Integer, nullable=False, index=True
53
+ )
45
54
  population_fraction_in_phase: Mapped[float] = mapped_column(
46
- Float, nullable=False
55
+ Float, nullable=False, index=True
47
56
  )
48
57
  reference_period_start: Mapped[datetime] = mapped_column(
49
- DateTime, nullable=False
58
+ DateTime, nullable=False, index=True
50
59
  )
51
60
  reference_period_end: Mapped[datetime] = mapped_column(
52
- DateTime, nullable=True, server_default=text("NULL")
61
+ DateTime, nullable=True, server_default=text("NULL"), index=True
53
62
  )
54
63
  source_data: Mapped[str] = mapped_column(Text, nullable=True)
55
64
 
@@ -73,14 +82,18 @@ view_params_food_security = ViewParams(
73
82
  DBResource.hdx_id.label("resource_hdx_id"),
74
83
  DBResource.name.label("resource_name"),
75
84
  DBResource.update_date.label("resource_update_date"),
85
+ DBResource.hapi_updated_date.label("hapi_updated_date"),
86
+ DBResource.hapi_replaced_date.label("hapi_replaced_date"),
76
87
  DBLocation.code.label("location_code"),
77
88
  DBLocation.name.label("location_name"),
78
89
  DBAdmin1.code.label("admin1_code"),
79
90
  DBAdmin1.name.label("admin1_name"),
80
91
  DBAdmin1.is_unspecified.label("admin1_is_unspecified"),
92
+ DBAdmin1.location_ref.label("location_ref"),
81
93
  DBAdmin2.code.label("admin2_code"),
82
94
  DBAdmin2.name.label("admin2_name"),
83
95
  DBAdmin2.is_unspecified.label("admin2_is_unspecified"),
96
+ DBAdmin2.admin1_ref.label("admin1_ref"),
84
97
  ).select_from(
85
98
  # Join pop to admin2 to admin1 to loc
86
99
  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(
@@ -0,0 +1,33 @@
1
+ """Patches table"""
2
+
3
+ from datetime import datetime
4
+
5
+ from sqlalchemy import (
6
+ DateTime,
7
+ Integer,
8
+ String,
9
+ )
10
+ from sqlalchemy.orm import Mapped, mapped_column
11
+
12
+ from hapi_schema.utils.base import Base
13
+
14
+
15
+ class DBPatches(Base):
16
+ __tablename__ = "patches"
17
+
18
+ id: Mapped[int] = mapped_column(Integer, primary_key=True)
19
+ patch_sequence_number: Mapped[int] = mapped_column(Integer, nullable=False)
20
+ commit_hash: Mapped[str] = mapped_column(
21
+ String(48), unique=True, nullable=False
22
+ )
23
+ commit_date: Mapped[datetime] = mapped_column(DateTime, nullable=False)
24
+ patch_path: Mapped[str] = mapped_column(String(128), nullable=False)
25
+ permanent_download_url: Mapped[str] = mapped_column(
26
+ String(1024), nullable=False, unique=True
27
+ )
28
+ state: Mapped[str] = mapped_column(String(16), nullable=False)
29
+ # discovered -> it was found in the patch repo
30
+ # executed -> the patch was executed successfully
31
+ # failed -> HWA tried to execute the patch but it failed (either pre-conditions
32
+ # were not met OR the transaction failed and was rolled back)
33
+ # canceled -> it was marked as canceled in the patch repo