pjdev-postgres 3.5.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.
@@ -0,0 +1,165 @@
1
+ .DS_Store
2
+ .idea
3
+
4
+ !**/.gitkeep
5
+
6
+ # Byte-compiled / optimized / DLL files
7
+ __pycache__/
8
+ *.py[cod]
9
+ *$py.class
10
+
11
+ # C extensions
12
+ *.so
13
+
14
+ # Distribution / packaging
15
+ .Python
16
+ build/
17
+ develop-eggs/
18
+ dist/
19
+ downloads/
20
+ eggs/
21
+ .eggs/
22
+ lib/
23
+ lib64/
24
+ parts/
25
+ sdist/
26
+ var/
27
+ wheels/
28
+ share/python-wheels/
29
+ *.egg-info/
30
+ .installed.cfg
31
+ *.egg
32
+ MANIFEST
33
+
34
+ # PyInstaller
35
+ # Usually these files are written by a python script from a template
36
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
37
+ *.manifest
38
+ *.spec
39
+
40
+ # Installer logs
41
+ pip-log.txt
42
+ pip-delete-this-directory.txt
43
+
44
+ # Unit test / coverage reports
45
+ htmlcov/
46
+ .tox/
47
+ .nox/
48
+ .coverage
49
+ .coverage.*
50
+ .cache
51
+ nosetests.xml
52
+ coverage.xml
53
+ *.cover
54
+ *.py,cover
55
+ .hypothesis/
56
+ .pytest_cache/
57
+ cover/
58
+
59
+ # Translations
60
+ *.mo
61
+ *.pot
62
+
63
+ # Django stuff:
64
+ *.log
65
+ local_settings.py
66
+ db.sqlite3
67
+ db.sqlite3-journal
68
+
69
+ # Flask stuff:
70
+ instance/
71
+ .webassets-cache
72
+
73
+ # Scrapy stuff:
74
+ .scrapy
75
+
76
+ # Sphinx documentation
77
+ docs/_build/
78
+
79
+ # PyBuilder
80
+ .pybuilder/
81
+ target/
82
+
83
+ # Jupyter Notebook
84
+ .ipynb_checkpoints
85
+
86
+ # IPython
87
+ profile_default/
88
+ ipython_config.py
89
+
90
+ # pyenv
91
+ # For a library or package, you might want to ignore these files since the code is
92
+ # intended to run in multiple environments; otherwise, check them in:
93
+ # .python-version
94
+
95
+ # pipenv
96
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
97
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
98
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
99
+ # install all needed dependencies.
100
+ #Pipfile.lock
101
+
102
+ # poetry
103
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
104
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
105
+ # commonly ignored for libraries.
106
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
107
+ #poetry.lock
108
+
109
+ # pdm
110
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
111
+ #pdm.lock
112
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
113
+ # in version control.
114
+ # https://pdm.fming.dev/#use-with-ide
115
+ .pdm.toml
116
+
117
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
118
+ __pypackages__/
119
+
120
+ # Celery stuff
121
+ celerybeat-schedule
122
+ celerybeat.pid
123
+
124
+ # SageMath parsed files
125
+ *.sage.py
126
+
127
+ # Environments
128
+ .env
129
+ .venv
130
+ env/
131
+ venv/
132
+ ENV/
133
+ env.bak/
134
+ venv.bak/
135
+
136
+ # Spyder project settings
137
+ .spyderproject
138
+ .spyproject
139
+
140
+ # Rope project settings
141
+ .ropeproject
142
+
143
+ # mkdocs documentation
144
+ /site
145
+
146
+ # mypy
147
+ .mypy_cache/
148
+ .dmypy.json
149
+ dmypy.json
150
+
151
+ # Pyre type checker
152
+ .pyre/
153
+
154
+ # pytype static type analyzer
155
+ .pytype/
156
+
157
+ # Cython debug symbols
158
+ cython_debug/
159
+
160
+ # PyCharm
161
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
162
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
163
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
164
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
165
+ #.idea/
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Chris O'Neill <chris@purplejay.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ Metadata-Version: 2.3
2
+ Name: pjdev-postgres
3
+ Version: 3.5.0
4
+ Project-URL: Documentation, https://gitlab.purplejay.net/keystone/python/-/tree/main/keystone-postgres/README.md
5
+ Project-URL: Issues, https://gitlab.purplejay.net/keystone/python/-/issues
6
+ Project-URL: Source, https://gitlab.purplejay.net/keystone/python
7
+ Author-email: Purple Jay LLC <it@purplejay.io>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: Implementation :: CPython
14
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
15
+ Requires-Python: >=3.12
16
+ Requires-Dist: loguru
17
+ Requires-Dist: psycopg[binary,pool]>=3.1.19
18
+ Requires-Dist: pydantic-settings>=2.3.1
19
+ Requires-Dist: sqlmodel>=0.0.19
20
+ Provides-Extra: dev
21
+ Requires-Dist: ruff; extra == 'dev'
22
+ Provides-Extra: test
23
+ Requires-Dist: coverage; extra == 'test'
24
+ Requires-Dist: pytest; extra == 'test'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # keystone-postgres
28
+
29
+ [![PyPI - Version](https://img.shields.io/pypi/v/pjdev-postgres.svg)](https://pypi.org/project/pjdev-postgres)
30
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pjdev-postgres.svg)](https://pypi.org/project/pjdev-postgres)
31
+
32
+ -----
33
+
34
+ **Table of Contents**
35
+
36
+ - [Installation](#installation)
37
+ - [License](#license)
38
+
39
+ ## Installation
40
+
41
+ ```console
42
+ pip install pjdev-postgres
43
+ ```
44
+
45
+ ## License
46
+
47
+ `pjdev-postgres` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,21 @@
1
+ # keystone-postgres
2
+
3
+ [![PyPI - Version](https://img.shields.io/pypi/v/pjdev-postgres.svg)](https://pypi.org/project/pjdev-postgres)
4
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pjdev-postgres.svg)](https://pypi.org/project/pjdev-postgres)
5
+
6
+ -----
7
+
8
+ **Table of Contents**
9
+
10
+ - [Installation](#installation)
11
+ - [License](#license)
12
+
13
+ ## Installation
14
+
15
+ ```console
16
+ pip install pjdev-postgres
17
+ ```
18
+
19
+ ## License
20
+
21
+ `pjdev-postgres` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,92 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "pjdev-postgres"
7
+ dynamic = ["version"]
8
+ description = ''
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = "MIT"
12
+ keywords = []
13
+ authors = [
14
+ { name = "Purple Jay LLC", email = "it@purplejay.io" },
15
+ ]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Programming Language :: Python",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: Implementation :: CPython",
21
+ "Programming Language :: Python :: Implementation :: PyPy",
22
+ ]
23
+ dependencies = [
24
+ "pydantic-settings>=2.3.1",
25
+ "sqlmodel>=0.0.19",
26
+ "psycopg[binary,pool]>=3.1.19",
27
+ "loguru"
28
+ ]
29
+
30
+ [project.optional-dependencies]
31
+ dev = [
32
+ "ruff",
33
+ ]
34
+
35
+ test = [
36
+ "pytest",
37
+ "coverage"
38
+ ]
39
+
40
+ [project.urls]
41
+ Documentation = "https://gitlab.purplejay.net/keystone/python/-/tree/main/keystone-postgres/README.md"
42
+ Issues = "https://gitlab.purplejay.net/keystone/python/-/issues"
43
+ Source = "https://gitlab.purplejay.net/keystone/python"
44
+
45
+ [tool.hatch.version]
46
+ path = "src/pjdev_postgres/__about__.py"
47
+
48
+ [tool.hatch.envs.default]
49
+ dependencies = [
50
+ "coverage[toml]>=6.5",
51
+ "pytest",
52
+ ]
53
+ [tool.hatch.envs.default.scripts]
54
+ test = "pytest {args:tests}"
55
+ test-cov = "coverage run -m pytest {args:tests}"
56
+ cov-report = [
57
+ "- coverage combine",
58
+ "coverage report",
59
+ ]
60
+ cov = [
61
+ "test-cov",
62
+ "cov-report",
63
+ ]
64
+
65
+ [[tool.hatch.envs.all.matrix]]
66
+ python = ["3.12"]
67
+
68
+ [tool.hatch.envs.types]
69
+ dependencies = [
70
+ "mypy>=1.0.0",
71
+ ]
72
+ [tool.hatch.envs.types.scripts]
73
+ check = "mypy --install-types --non-interactive {args:src/pjdev_postgres tests}"
74
+
75
+ [tool.coverage.run]
76
+ source_pkgs = ["pjdev_postgres", "tests"]
77
+ branch = true
78
+ parallel = true
79
+ omit = [
80
+ "src/pjdev_postgres/__about__.py",
81
+ ]
82
+
83
+ [tool.coverage.paths]
84
+ pjdev_postgres = ["src/pjdev_postgres", "*/pjdev-postgres/src/pjdev_postgres"]
85
+ tests = ["tests", "*/pjdev-postgres/tests"]
86
+
87
+ [tool.coverage.report]
88
+ exclude_lines = [
89
+ "no cov",
90
+ "if __name__ == .__main__.:",
91
+ "if TYPE_CHECKING:",
92
+ ]
@@ -0,0 +1,45 @@
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile --no-emit-index-url --extra=test --extra=dev --output-file=requirements-dev.txt pyproject.toml
3
+ annotated-types==0.7.0
4
+ # via pydantic
5
+ coverage==7.5.3
6
+ # via keystone-postgres (pyproject.toml)
7
+ iniconfig==2.0.0
8
+ # via pytest
9
+ loguru==0.7.2
10
+ # via keystone-postgres (pyproject.toml)
11
+ packaging==24.1
12
+ # via pytest
13
+ pluggy==1.5.0
14
+ # via pytest
15
+ psycopg==3.1.19
16
+ # via keystone-postgres (pyproject.toml)
17
+ psycopg-binary==3.1.19
18
+ # via psycopg
19
+ psycopg-pool==3.2.2
20
+ # via psycopg
21
+ pydantic==2.7.3
22
+ # via
23
+ # pydantic-settings
24
+ # sqlmodel
25
+ pydantic-core==2.18.4
26
+ # via pydantic
27
+ pydantic-settings==2.3.2
28
+ # via keystone-postgres (pyproject.toml)
29
+ pytest==8.2.2
30
+ # via keystone-postgres (pyproject.toml)
31
+ python-dotenv==1.0.1
32
+ # via pydantic-settings
33
+ ruff==0.4.8
34
+ # via keystone-postgres (pyproject.toml)
35
+ sqlalchemy==2.0.30
36
+ # via sqlmodel
37
+ sqlmodel==0.0.19
38
+ # via keystone-postgres (pyproject.toml)
39
+ typing-extensions==4.12.2
40
+ # via
41
+ # psycopg
42
+ # psycopg-pool
43
+ # pydantic
44
+ # pydantic-core
45
+ # sqlalchemy
@@ -0,0 +1,43 @@
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile --no-emit-index-url --extra=test --output-file=requirements-test.txt pyproject.toml
3
+ annotated-types==0.7.0
4
+ # via pydantic
5
+ coverage==7.5.3
6
+ # via keystone-postgres (pyproject.toml)
7
+ iniconfig==2.0.0
8
+ # via pytest
9
+ loguru==0.7.2
10
+ # via keystone-postgres (pyproject.toml)
11
+ packaging==24.1
12
+ # via pytest
13
+ pluggy==1.5.0
14
+ # via pytest
15
+ psycopg==3.1.19
16
+ # via keystone-postgres (pyproject.toml)
17
+ psycopg-binary==3.1.19
18
+ # via psycopg
19
+ psycopg-pool==3.2.2
20
+ # via psycopg
21
+ pydantic==2.7.3
22
+ # via
23
+ # pydantic-settings
24
+ # sqlmodel
25
+ pydantic-core==2.18.4
26
+ # via pydantic
27
+ pydantic-settings==2.3.2
28
+ # via keystone-postgres (pyproject.toml)
29
+ pytest==8.2.2
30
+ # via keystone-postgres (pyproject.toml)
31
+ python-dotenv==1.0.1
32
+ # via pydantic-settings
33
+ sqlalchemy==2.0.30
34
+ # via sqlmodel
35
+ sqlmodel==0.0.19
36
+ # via keystone-postgres (pyproject.toml)
37
+ typing-extensions==4.12.2
38
+ # via
39
+ # psycopg
40
+ # psycopg-pool
41
+ # pydantic
42
+ # pydantic-core
43
+ # sqlalchemy
@@ -0,0 +1,33 @@
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv pip compile --no-emit-index-url --strip-extras --output-file=requirements.txt pyproject.toml
3
+ annotated-types==0.7.0
4
+ # via pydantic
5
+ loguru==0.7.2
6
+ # via keystone-postgres (pyproject.toml)
7
+ psycopg==3.1.19
8
+ # via keystone-postgres (pyproject.toml)
9
+ psycopg-binary==3.1.19
10
+ # via psycopg
11
+ psycopg-pool==3.2.2
12
+ # via psycopg
13
+ pydantic==2.7.3
14
+ # via
15
+ # pydantic-settings
16
+ # sqlmodel
17
+ pydantic-core==2.18.4
18
+ # via pydantic
19
+ pydantic-settings==2.3.2
20
+ # via keystone-postgres (pyproject.toml)
21
+ python-dotenv==1.0.1
22
+ # via pydantic-settings
23
+ sqlalchemy==2.0.30
24
+ # via sqlmodel
25
+ sqlmodel==0.0.19
26
+ # via keystone-postgres (pyproject.toml)
27
+ typing-extensions==4.12.2
28
+ # via
29
+ # psycopg
30
+ # psycopg-pool
31
+ # pydantic
32
+ # pydantic-core
33
+ # sqlalchemy
@@ -0,0 +1,4 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ __version__ = "3.5.0"
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
@@ -0,0 +1,33 @@
1
+ from typing import TypeVar, Optional
2
+ from uuid import UUID, uuid4
3
+ from loguru import logger
4
+
5
+ from pjdev_postgres.models import Versioned, ConcurrencyException
6
+
7
+ T = TypeVar("T", bound=Versioned)
8
+
9
+
10
+ def parse_concurrency_token(token: str) -> Optional[UUID]:
11
+ try:
12
+ concurrency_token = UUID(token) if token is not None else None
13
+ return concurrency_token
14
+ except TypeError as e:
15
+ logger.warning(f"{e}")
16
+ logger.warning(f"Tried parsing invalid uuid: {token}")
17
+
18
+
19
+ def increment_version(obj: T) -> T:
20
+ obj.concurrency_token = uuid4()
21
+
22
+ return obj
23
+
24
+
25
+ def validate_version(obj: T, token: Optional[str | UUID] = None) -> T:
26
+ concurrency_token = token
27
+ if not isinstance(token, UUID):
28
+ concurrency_token = parse_concurrency_token(token)
29
+
30
+ if obj.concurrency_token is not None and obj.concurrency_token != concurrency_token:
31
+ raise ConcurrencyException("Stale data detected")
32
+
33
+ return increment_version(obj)
@@ -0,0 +1,58 @@
1
+ from datetime import date, datetime, UTC
2
+ from typing import Callable, List, Optional, Type, Any
3
+ from loguru import logger
4
+ from pydantic import BeforeValidator
5
+
6
+
7
+ def make_date_validator(
8
+ date_format: str, date_obj_type: Type[date | datetime] = datetime
9
+ ) -> Callable[[str], Optional[date]]:
10
+ def validator(v: Optional[Any]) -> Optional[date]:
11
+ if not v:
12
+ return None
13
+
14
+ if isinstance(v, datetime):
15
+ formatted_value = v
16
+ if date_format == "iso":
17
+ formatted_value = v.astimezone(UTC)
18
+ return (
19
+ formatted_value if date_obj_type is datetime else formatted_value.date()
20
+ )
21
+
22
+ if isinstance(v, date):
23
+ return v
24
+
25
+ if not isinstance(v, str):
26
+ return None
27
+
28
+ stripped_value = v.strip()
29
+
30
+ if stripped_value in ["-", ""]:
31
+ return None
32
+
33
+ if date_format == "iso":
34
+ value = datetime.fromisoformat(v).replace(tzinfo=UTC)
35
+ else:
36
+ value = datetime.strptime(v, date_format)
37
+ return value.date() if date_obj_type is date else value
38
+
39
+ return validator
40
+
41
+
42
+ def make_date_validator_for_formats(
43
+ date_formats: List[str], date_obj_type: Type[date | datetime] = datetime
44
+ ) -> Callable[[str], Optional[date]]:
45
+ def validator(v: Optional[Any]) -> Optional[date]:
46
+ for fmt in date_formats:
47
+ try:
48
+ return make_date_validator(fmt, date_obj_type)(v)
49
+ except ValueError as e:
50
+ logger.warning(e)
51
+ continue
52
+
53
+ return None
54
+
55
+ return validator
56
+
57
+
58
+ date_validator = BeforeValidator(make_date_validator_for_formats(["iso"], date))
@@ -0,0 +1,57 @@
1
+ from datetime import datetime, UTC
2
+ from typing import Optional, Annotated
3
+ from uuid import UUID
4
+
5
+ from pydantic import BaseModel
6
+ from sqlmodel import SQLModel, Field, Index
7
+
8
+ from pjdev_postgres.model_validators import date_validator
9
+
10
+
11
+ class ConnectionOptions(BaseModel):
12
+ echo: bool = False
13
+ pool_size: int = 5
14
+ max_overflow: int = 10
15
+
16
+
17
+ class Versioned(BaseModel):
18
+ concurrency_token: Optional[UUID] = None
19
+
20
+
21
+ class Auditable(BaseModel):
22
+ created_by_id: Optional[str] = None
23
+ created_by: Optional[str] = None
24
+ created_datetime: Annotated[
25
+ datetime, date_validator, Field(default_factory=lambda: datetime.now(UTC))
26
+ ]
27
+ last_modified_by_id: Optional[str] = None
28
+ last_modified_by: Optional[str] = None
29
+ last_modified_datetime: Annotated[Optional[datetime], date_validator] = None
30
+
31
+
32
+ class TableModel(SQLModel):
33
+ row_id: Optional[int] = Field(default=None, primary_key=True)
34
+
35
+
36
+ class Savable(Versioned, Auditable, TableModel):
37
+ pass
38
+
39
+
40
+ class History(TableModel, table=True):
41
+ entity_name: str
42
+ entity_id: int
43
+ value: str
44
+ timestamp: Annotated[
45
+ datetime, date_validator, Field(default_factory=lambda: datetime.now(UTC))
46
+ ]
47
+
48
+ __table_args__ = (
49
+ Index("ix_history_entity_id", "entity_id"),
50
+ Index("ix_history_entity_name", "entity_name"),
51
+ Index("ix_history_timestamp", "timestamp"),
52
+ )
53
+
54
+
55
+ class ConcurrencyException(BaseException):
56
+ def __init__(self, message: str):
57
+ super().__init__(message)
@@ -0,0 +1,95 @@
1
+ from contextlib import contextmanager
2
+ from datetime import datetime, UTC
3
+ from typing import List, Optional, Type, TypeVar, Callable, Tuple
4
+ from uuid import UUID
5
+
6
+ from sqlalchemy import Engine
7
+ from sqlmodel import SQLModel, create_engine, Session as SQLModelSession
8
+
9
+ from pjdev_postgres import postgres_settings, concurrency_service
10
+ from pjdev_postgres.models import ConnectionOptions, Savable, History
11
+
12
+ T = TypeVar("T", bound=Savable)
13
+
14
+
15
+ class Context:
16
+ engine: Optional[Engine] = None
17
+
18
+
19
+ __ctx = Context()
20
+
21
+
22
+ def initialize_engine(
23
+ tables: List[Type[SQLModel]],
24
+ options: Optional[ConnectionOptions] = None,
25
+ ) -> Engine:
26
+ if options is None:
27
+ options = ConnectionOptions()
28
+
29
+ settings = postgres_settings.get_settings()
30
+
31
+ if len(tables) == 0:
32
+ raise ValueError("Must specify at least one table")
33
+
34
+ db_url = f"postgresql+psycopg://{settings.username}:{settings.password}@{settings.host}/{settings.name}"
35
+
36
+ engine = create_engine(
37
+ db_url,
38
+ echo=options.echo,
39
+ pool_size=options.pool_size,
40
+ max_overflow=options.max_overflow,
41
+ )
42
+
43
+ for t in tables:
44
+ t.__table__.create(bind=engine, checkfirst=True)
45
+
46
+ return engine
47
+
48
+
49
+ def configure_single_context(
50
+ tables: List[Type[SQLModel]], options: Optional[ConnectionOptions] = None
51
+ ):
52
+ __ctx.engine = initialize_engine(tables, options)
53
+
54
+
55
+ @contextmanager
56
+ def session_context() -> SQLModelSession:
57
+ with SQLModelSession(__ctx.engine) as session:
58
+ try:
59
+ yield session
60
+ finally:
61
+ session.close()
62
+
63
+
64
+ def get_session() -> SQLModelSession:
65
+ with SQLModelSession(__ctx.engine) as session:
66
+ yield session
67
+
68
+
69
+ def save(
70
+ obj: T,
71
+ user_resolver: Callable[[], Tuple[Optional[str], str]],
72
+ session: SQLModelSession,
73
+ concurrency_token: Optional[UUID] = None,
74
+ commit: bool = True,
75
+ ) -> T:
76
+ updated_obj = concurrency_service.validate_version(obj, concurrency_token)
77
+ updated_obj.last_modified_datetime = datetime.now(UTC)
78
+ user_oid, username = user_resolver()
79
+ updated_obj.last_modified_by_id = user_oid
80
+ updated_obj.last_modified_by = username
81
+
82
+ session.flush([updated_obj])
83
+
84
+ history = History(
85
+ entity_name=updated_obj.__class__.__name__.lower(),
86
+ entity_id=updated_obj.row_id,
87
+ value=updated_obj.model_dump_json(),
88
+ )
89
+ session.add(history)
90
+
91
+ if commit:
92
+ session.commit()
93
+ session.refresh(updated_obj)
94
+
95
+ return updated_obj
@@ -0,0 +1,35 @@
1
+ from pathlib import Path
2
+ from typing import Optional
3
+
4
+ from pydantic_settings import BaseSettings, SettingsConfigDict
5
+
6
+
7
+ class PostgresSettings(BaseSettings):
8
+ host: str = "localhost"
9
+ name: str
10
+ password: Optional[str] = None
11
+ username: Optional[str] = None
12
+ model_config = SettingsConfigDict(
13
+ case_sensitive=False,
14
+ extra="ignore",
15
+ env_nested_delimiter="__",
16
+ env_prefix="db_",
17
+ )
18
+
19
+
20
+ class Context:
21
+ settings: Optional[PostgresSettings] = None
22
+
23
+
24
+ __ctx = Context()
25
+
26
+
27
+ def init_settings(root: Path):
28
+ __ctx.settings = PostgresSettings(_env_file=root / ".env")
29
+
30
+
31
+ def get_settings() -> PostgresSettings:
32
+ if __ctx.settings is None:
33
+ msg = "Settings are not initialized -- call init_settings()"
34
+ raise Exception(msg)
35
+ return __ctx.settings
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ export PYTHONPATH=./src:./tests
4
+ #coverage run -m pytest tests
5
+ pytest tests/*.py -vv
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
@@ -0,0 +1,43 @@
1
+ from pjdev_postgres import postgres_service
2
+ from pjdev_postgres.models import Savable, History
3
+ from sqlmodel import Session, select
4
+ from utilities import init_test_db
5
+
6
+
7
+ def test_history_item_is_created_and_saved():
8
+ class MockObj(Savable):
9
+ pass
10
+
11
+ class MockTable(MockObj, table=True):
12
+ pass
13
+
14
+ engine = init_test_db([MockTable.__table__, History.__table__])
15
+
16
+ with Session(engine) as session:
17
+ obj = MockTable()
18
+ session.add(obj)
19
+ saved_obj = postgres_service.save(
20
+ obj, session=session, user_resolver=lambda: ("mock_oid", "mock_user")
21
+ )
22
+
23
+ assert saved_obj.last_modified_datetime is not None
24
+ assert saved_obj.last_modified_by is not None
25
+ assert saved_obj.last_modified_by_id is not None
26
+
27
+ with Session(engine) as session2:
28
+ mock_row = session2.exec(
29
+ select(MockTable).where(MockTable.row_id == obj.row_id)
30
+ ).one_or_none()
31
+ history_mock = session2.exec(select(History)).all()
32
+ assert len(history_mock) == 1
33
+ assert mock_row is not None
34
+ assert history_mock[0].entity_name == mock_row.__tablename__
35
+
36
+ # Must use the base model (not the model with table=True) to validate due to this:
37
+ # https://github.com/tiangolo/sqlmodel/issues/52#issuecomment-1311987732
38
+
39
+ historical_mock_row = MockObj.model_validate_json(history_mock[0].value)
40
+ assert historical_mock_row == MockObj.model_validate(mock_row)
41
+ assert mock_row.last_modified_datetime is not None
42
+ assert mock_row.last_modified_by is not None
43
+ assert mock_row.last_modified_by_id is not None
@@ -0,0 +1,11 @@
1
+ from typing import List
2
+
3
+ from sqlalchemy import create_engine, Engine, Table
4
+
5
+
6
+ def init_test_db(tables: List[Table]) -> Engine:
7
+ engine = create_engine("sqlite:///", echo=False)
8
+ for t in tables:
9
+ t.create(bind=engine, checkfirst=True)
10
+
11
+ return engine