ecodev-core 0.0.52__py3-none-any.whl → 0.0.53__py3-none-any.whl

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.

Potentially problematic release.


This version of ecodev-core might be problematic. Click here for more details.

ecodev_core/__init__.py CHANGED
@@ -40,6 +40,7 @@ from ecodev_core.db_insertion import get_raw_df
40
40
  from ecodev_core.db_retrieval import count_rows
41
41
  from ecodev_core.db_retrieval import get_rows
42
42
  from ecodev_core.db_retrieval import ServerSideField
43
+ from ecodev_core.db_upsertion import add_missing_enum_values
43
44
  from ecodev_core.db_upsertion import field
44
45
  from ecodev_core.db_upsertion import filter_to_sfield_dict
45
46
  from ecodev_core.db_upsertion import get_sfield_columns
@@ -107,4 +108,4 @@ __all__ = [
107
108
  'sort_by_keys', 'sort_by_values', 'Settings', 'load_yaml_file', 'Deployment', 'Version',
108
109
  'sfield', 'field', 'upsert_df_data', 'upsert_deletor', 'get_row_versions', 'get_versions',
109
110
  'db_to_value', 'upsert_data', 'upsert_selector', 'get_sfield_columns', 'filter_to_sfield_dict',
110
- 'SETTINGS']
111
+ 'SETTINGS', 'add_missing_enum_values']
@@ -2,6 +2,7 @@
2
2
  Module handling CRUD and version operations
3
3
  """
4
4
  from datetime import datetime
5
+ from enum import EnumType
5
6
  from functools import partial
6
7
  from typing import Any
7
8
  from typing import Union
@@ -14,6 +15,7 @@ from sqlmodel import inspect
14
15
  from sqlmodel import select
15
16
  from sqlmodel import Session
16
17
  from sqlmodel import SQLModel
18
+ from sqlmodel import text
17
19
  from sqlmodel import update
18
20
  from sqlmodel.main import SQLModelMetaclass
19
21
  from sqlmodel.sql.expression import SelectOfScalar
@@ -27,6 +29,31 @@ INFO = 'info'
27
29
  SA_COLUMN_KWARGS = 'sa_column_kwargs'
28
30
 
29
31
 
32
+ def add_missing_enum_values(enum: EnumType, session: Session, new_vals: list | None = None) -> None:
33
+ """
34
+ Add to an existing enum its missing db values. Do so by retrieving what is already in db, and
35
+ insert what is new
36
+ """
37
+
38
+ for val in [e.name for e in new_vals or enum if e.name not in get_enum_values(enum, session)]:
39
+ session.execute(text(f"ALTER TYPE {enum.__name__.lower()} ADD VALUE IF NOT EXISTS '{val}'"))
40
+ session.commit()
41
+
42
+
43
+ def get_enum_values(enum: EnumType, session: Session) -> set[str]:
44
+ """
45
+ Return all enum values in db for the passed enum.
46
+ """
47
+ result = session.execute(text(
48
+ """
49
+ SELECT enumlabel FROM pg_enum
50
+ JOIN pg_type ON pg_enum.enumtypid = pg_type.oid
51
+ WHERE pg_type.typname = :enum_name
52
+ """
53
+ ), {'enum_name': enum.__name__.lower()})
54
+ return {x[0] for x in result}
55
+
56
+
30
57
  def sfield(**kwargs):
31
58
  """
32
59
  Field constructor for columns not to be versioned. Those are the columns on which to select.
@@ -147,11 +174,11 @@ def get_sfield_columns(db_model: SQLModelMetaclass) -> list[str]:
147
174
  for x in inspect(db_model).c
148
175
  if x.info.get(FILTER_ON) is True
149
176
  ]
150
-
151
-
152
- def filter_to_sfield_dict(row: dict | SQLModelMetaclass,
153
- db_schema: SQLModelMetaclass | None = None) \
154
- -> dict[str, dict | SQLModelMetaclass]:
177
+
178
+
179
+ def filter_to_sfield_dict(row: dict | SQLModelMetaclass,
180
+ db_schema: SQLModelMetaclass | None = None) \
181
+ -> dict[str, dict | SQLModelMetaclass]:
155
182
  """
156
183
  Returns a dict with only sfields from object
157
184
  Args:
@@ -162,4 +189,3 @@ def filter_to_sfield_dict(row: dict | SQLModelMetaclass,
162
189
  """
163
190
  return {pk: getattr(row, pk)
164
191
  for pk in get_sfield_columns(db_schema or row.__class__)}
165
-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ecodev-core
3
- Version: 0.0.52
3
+ Version: 0.0.53
4
4
  Summary: Low level sqlmodel/fastapi/pydantic building blocks
5
5
  License: MIT
6
6
  Author: Thomas Epelbaum
@@ -1,4 +1,4 @@
1
- ecodev_core/__init__.py,sha256=ULUNsG-OwP5pZkqFemYcA5NZURCdSjrC2v4PNTlreDs,6001
1
+ ecodev_core/__init__.py,sha256=UgK7wYtjjqjhAN772FlxiY16FkwvjbBdKUgAFGKggBs,6089
2
2
  ecodev_core/app_activity.py,sha256=KBtI-35LBLPDppFB7xjxWthXQrY3Z_aGDnC-HrW8Ea0,4641
3
3
  ecodev_core/app_rights.py,sha256=RZPdDtydFqc_nFj96huKAc56BS0qS6ScKv4Kghqd6lc,726
4
4
  ecodev_core/app_user.py,sha256=r1bqA4H08x53XmxmjwyGKl_PFjYQazzBbVErdkztqeE,2947
@@ -11,7 +11,7 @@ ecodev_core/db_connection.py,sha256=hhqeyTrl0DlQA7RkUs6pIOpZeE3yS_Q5mqj5uGPfG_Y,
11
11
  ecodev_core/db_filters.py,sha256=T_5JVF27UEu7sC6NOm7-W3_Y0GLfbWQO_EeTXcD2cv8,5041
12
12
  ecodev_core/db_insertion.py,sha256=k-r798MMrW1sRb-gb8lQTxyJrb4QP5iZT8GDzCYYwlo,4544
13
13
  ecodev_core/db_retrieval.py,sha256=sCP7TDGIcTOK5gT3Inga91bE4S31HbQPw4yI22WJbss,7392
14
- ecodev_core/db_upsertion.py,sha256=_OdCILP1NEw1hkbvTn9ZOF5YkU9U02Fj_U3E7uY5AoI,5861
14
+ ecodev_core/db_upsertion.py,sha256=Pdu_LkcgePygICM3flfOIADHbL_NRUOC6WqAh-p3YcI,6757
15
15
  ecodev_core/deployment.py,sha256=z8ACI00EtKknXOB8xyPwYIXTvPjIDOH9z9cBGEU0YrA,281
16
16
  ecodev_core/email_sender.py,sha256=V3UGweuq6Iy09Z9to8HzM6JOVDVGHZXHGjUSkW94Tac,1912
17
17
  ecodev_core/enum_utils.py,sha256=BkQ4YQ97tXBYmMcQiSIi0mbioD5CgVU79myg1BBAXuA,556
@@ -26,7 +26,7 @@ ecodev_core/safe_utils.py,sha256=Q8N15El1tSxZJJsy1i_1CCycuBN1_98QQoHmYJRcLIY,690
26
26
  ecodev_core/settings.py,sha256=UvaTv8S_HvfFAL-m1Rfqv_geSGcccuV3ziR1o1d5wu4,1795
27
27
  ecodev_core/sqlmodel_utils.py,sha256=t57H3QPtKRy4ujic1clMK_2L4p0yjGJLZbDjHPZ8M94,453
28
28
  ecodev_core/version.py,sha256=eyIf8KkW_t-hMuYFIoy0cUlNaMewLe6i45m2HKZKh0Q,4403
29
- ecodev_core-0.0.52.dist-info/LICENSE.md,sha256=8dqVJEbwXjPWjjRKjdLMym5k9Gi8hwtrHh84sti6KIs,1068
30
- ecodev_core-0.0.52.dist-info/METADATA,sha256=UgEUyk-1FHtzUXSP3t7fattxjRHbSVXjwvMI8_Ioa8g,3509
31
- ecodev_core-0.0.52.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
32
- ecodev_core-0.0.52.dist-info/RECORD,,
29
+ ecodev_core-0.0.53.dist-info/LICENSE.md,sha256=8dqVJEbwXjPWjjRKjdLMym5k9Gi8hwtrHh84sti6KIs,1068
30
+ ecodev_core-0.0.53.dist-info/METADATA,sha256=J57EVk_FfORxTLvYXjn7KLrN8p98J0K4C8tMGyixab4,3509
31
+ ecodev_core-0.0.53.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
32
+ ecodev_core-0.0.53.dist-info/RECORD,,