wexample-migration 9.3.2__tar.gz → 9.3.3__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 (31) hide show
  1. wexample_migration-9.3.3/PKG-INFO +170 -0
  2. wexample_migration-9.3.3/README.md +151 -0
  3. {wexample_migration-9.3.2 → wexample_migration-9.3.3}/pyproject.toml +7 -7
  4. wexample_migration-9.3.3/src/wexample_orm/__init__.py +13 -0
  5. wexample_migration-9.3.3/src/wexample_orm/common/abstract_repositories_manager.py +54 -0
  6. wexample_migration-9.3.3/src/wexample_orm/entity/abstract_entity.py +31 -0
  7. wexample_migration-9.3.3/src/wexample_orm/exception/repository_session_missing_exception.py +16 -0
  8. wexample_migration-9.3.3/src/wexample_orm/exception/unknown_repository_exception.py +21 -0
  9. {wexample_migration-9.3.2/src/wexample_migration → wexample_migration-9.3.3/src/wexample_orm}/py.typed +0 -0
  10. wexample_migration-9.3.3/src/wexample_orm/repository/abstract_repository.py +63 -0
  11. wexample_migration-9.3.3/src/wexample_orm/session/__init__.py +0 -0
  12. wexample_migration-9.3.3/src/wexample_orm/session/abstract_session_factory.py +70 -0
  13. wexample_migration-9.3.3/src/wexample_orm/testing/__init__.py +11 -0
  14. wexample_migration-9.3.3/src/wexample_orm/testing/sqlite.py +59 -0
  15. wexample_migration-9.3.3/tests/__init__.py +0 -0
  16. wexample_migration-9.3.3/tests/test_abstract.py +93 -0
  17. wexample_migration-9.3.2/PKG-INFO +0 -703
  18. wexample_migration-9.3.2/README.md +0 -682
  19. wexample_migration-9.3.2/src/wexample_migration/abstract_migration.py +0 -53
  20. wexample_migration-9.3.2/src/wexample_migration/filestate/filestate_item_migration.py +0 -46
  21. wexample_migration-9.3.2/src/wexample_migration/migration_context.py +0 -34
  22. wexample_migration-9.3.2/src/wexample_migration/migration_runner.py +0 -142
  23. wexample_migration-9.3.2/src/wexample_migration/migration_stamp.py +0 -40
  24. wexample_migration-9.3.2/src/wexample_migration/workdir/mixin/with_migration_workdir_mixin.py +0 -96
  25. wexample_migration-9.3.2/tests/test_filestate_item_migration.py +0 -58
  26. wexample_migration-9.3.2/tests/test_migration_rollback.py +0 -127
  27. wexample_migration-9.3.2/tests/test_migration_runner.py +0 -184
  28. {wexample_migration-9.3.2/src/wexample_migration → wexample_migration-9.3.3/src/wexample_orm/common}/__init__.py +0 -0
  29. {wexample_migration-9.3.2/src/wexample_migration/filestate → wexample_migration-9.3.3/src/wexample_orm/entity}/__init__.py +0 -0
  30. {wexample_migration-9.3.2/src/wexample_migration/workdir → wexample_migration-9.3.3/src/wexample_orm/exception}/__init__.py +0 -0
  31. {wexample_migration-9.3.2/src/wexample_migration/workdir/mixin → wexample_migration-9.3.3/src/wexample_orm/repository}/__init__.py +0 -0
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.1
2
+ Name: wexample-migration
3
+ Version: 9.3.3
4
+ Summary: Generic ORM abstractions on top of SQLAlchemy.
5
+ Author-Email: weeger <contact@wexample.com>
6
+ License: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Project-URL: homepage, https://github.com/wexample/python-orm
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: attrs>=23.1.0
13
+ Requires-Dist: sqlalchemy<3,>=2
14
+ Requires-Dist: wexample-helpers>=13.0.0
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest; extra == "dev"
17
+ Requires-Dist: pytest-cov; extra == "dev"
18
+ Description-Content-Type: text/markdown
19
+
20
+ # migration
21
+
22
+ Version: 9.3.3
23
+
24
+ Generic ORM abstractions on top of SQLAlchemy.
25
+
26
+ ## Table of Contents
27
+
28
+ - [Tests](#tests)
29
+ - [Suite Integration](#suite-integration)
30
+ - [Dependencies](#dependencies)
31
+ - [Versioning](#versioning)
32
+ - [License](#license)
33
+ - [Suite Integration](#suite-integration)
34
+ - [Suite Signature](#suite-signature)
35
+ - [Introduction](#introduction)
36
+ - [Roadmap](#roadmap)
37
+ - [Status Compatibility](#status-compatibility)
38
+ - [Useful Links](#useful-links)
39
+ - [Migration Notes](#migration-notes)
40
+
41
+ ## Tests
42
+
43
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
44
+
45
+ ### Installation
46
+
47
+ First, install the required testing dependencies:
48
+ ```bash
49
+ .venv/bin/python -m pip install pytest pytest-cov
50
+ ```
51
+
52
+ ### Basic Usage
53
+
54
+ Run all tests with coverage:
55
+ ```bash
56
+ .venv/bin/python -m pytest --cov --cov-report=html
57
+ ```
58
+
59
+ ### Common Commands
60
+ ```bash
61
+ # Run tests with coverage for a specific module
62
+ .venv/bin/python -m pytest --cov=your_module
63
+
64
+ # Show which lines are not covered
65
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
66
+
67
+ # Generate an HTML coverage report
68
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
69
+
70
+ # Combine terminal and HTML reports
71
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
72
+
73
+ # Run specific test file with coverage
74
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
75
+ ```
76
+
77
+ ### Viewing HTML Reports
78
+
79
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
80
+
81
+ ### Coverage Threshold
82
+
83
+ To enforce a minimum coverage percentage:
84
+ ```bash
85
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
86
+ ```
87
+
88
+ This will cause the test suite to fail if coverage drops below 80%.
89
+
90
+ ## Integration in the Suite
91
+
92
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
93
+
94
+ ### Related Packages
95
+
96
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
97
+
98
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
99
+
100
+ ## Dependencies
101
+
102
+ - attrs: >=23.1.0
103
+ - sqlalchemy: <3,>=2
104
+ - wexample-helpers: >=13.0.0
105
+
106
+ ## Versioning & Compatibility Policy
107
+
108
+ Wexample packages follow **Semantic Versioning** (SemVer):
109
+
110
+ - **MAJOR**: Breaking changes
111
+ - **MINOR**: New features, backward compatible
112
+ - **PATCH**: Bug fixes, backward compatible
113
+
114
+ We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
115
+
116
+ ## License
117
+
118
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
119
+
120
+ Free to use in both personal and commercial projects.
121
+
122
+ ## Integration in the Suite
123
+
124
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
125
+
126
+ ### Related Packages
127
+
128
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
129
+
130
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
131
+
132
+ # About us
133
+
134
+ [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
135
+
136
+ This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
137
+
138
+ Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
139
+
140
+ Python Doctrine inspired ORM
141
+
142
+ ## Known Limitations & Roadmap
143
+
144
+ Current limitations and planned features are tracked in the GitHub issues.
145
+
146
+ See the [project roadmap](https://github.com/wexample/python-migration/issues) for upcoming features and improvements.
147
+
148
+ ## Status & Compatibility
149
+
150
+ **Maturity**: Production-ready
151
+
152
+ **Python Support**: >=3.10
153
+
154
+ **OS Support**: Linux, macOS, Windows
155
+
156
+ **Status**: Actively maintained
157
+
158
+ ## Useful Links
159
+
160
+ - **Homepage**: https://github.com/wexample/python-migration
161
+ - **Documentation**: [docs.wexample.com](https://docs.wexample.com)
162
+ - **Issue Tracker**: https://github.com/wexample/python-migration/issues
163
+ - **Discussions**: https://github.com/wexample/python-migration/discussions
164
+ - **PyPI**: [pypi.org/project/migration](https://pypi.org/project/migration/)
165
+
166
+ ## Migration Notes
167
+
168
+ When upgrading between major versions, refer to the migration guides in the documentation.
169
+
170
+ Breaking changes are clearly documented with upgrade paths and examples.
@@ -0,0 +1,151 @@
1
+ # migration
2
+
3
+ Version: 9.3.3
4
+
5
+ Generic ORM abstractions on top of SQLAlchemy.
6
+
7
+ ## Table of Contents
8
+
9
+ - [Tests](#tests)
10
+ - [Suite Integration](#suite-integration)
11
+ - [Dependencies](#dependencies)
12
+ - [Versioning](#versioning)
13
+ - [License](#license)
14
+ - [Suite Integration](#suite-integration)
15
+ - [Suite Signature](#suite-signature)
16
+ - [Introduction](#introduction)
17
+ - [Roadmap](#roadmap)
18
+ - [Status Compatibility](#status-compatibility)
19
+ - [Useful Links](#useful-links)
20
+ - [Migration Notes](#migration-notes)
21
+
22
+ ## Tests
23
+
24
+ This project uses `pytest` for testing and `pytest-cov` for code coverage analysis.
25
+
26
+ ### Installation
27
+
28
+ First, install the required testing dependencies:
29
+ ```bash
30
+ .venv/bin/python -m pip install pytest pytest-cov
31
+ ```
32
+
33
+ ### Basic Usage
34
+
35
+ Run all tests with coverage:
36
+ ```bash
37
+ .venv/bin/python -m pytest --cov --cov-report=html
38
+ ```
39
+
40
+ ### Common Commands
41
+ ```bash
42
+ # Run tests with coverage for a specific module
43
+ .venv/bin/python -m pytest --cov=your_module
44
+
45
+ # Show which lines are not covered
46
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing
47
+
48
+ # Generate an HTML coverage report
49
+ .venv/bin/python -m pytest --cov=your_module --cov-report=html
50
+
51
+ # Combine terminal and HTML reports
52
+ .venv/bin/python -m pytest --cov=your_module --cov-report=term-missing --cov-report=html
53
+
54
+ # Run specific test file with coverage
55
+ .venv/bin/python -m pytest tests/test_file.py --cov=your_module --cov-report=term-missing
56
+ ```
57
+
58
+ ### Viewing HTML Reports
59
+
60
+ After generating an HTML report, open `htmlcov/index.html` in your browser to view detailed line-by-line coverage information.
61
+
62
+ ### Coverage Threshold
63
+
64
+ To enforce a minimum coverage percentage:
65
+ ```bash
66
+ .venv/bin/python -m pytest --cov=your_module --cov-fail-under=80
67
+ ```
68
+
69
+ This will cause the test suite to fail if coverage drops below 80%.
70
+
71
+ ## Integration in the Suite
72
+
73
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
74
+
75
+ ### Related Packages
76
+
77
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
78
+
79
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
80
+
81
+ ## Dependencies
82
+
83
+ - attrs: >=23.1.0
84
+ - sqlalchemy: <3,>=2
85
+ - wexample-helpers: >=13.0.0
86
+
87
+ ## Versioning & Compatibility Policy
88
+
89
+ Wexample packages follow **Semantic Versioning** (SemVer):
90
+
91
+ - **MAJOR**: Breaking changes
92
+ - **MINOR**: New features, backward compatible
93
+ - **PATCH**: Bug fixes, backward compatible
94
+
95
+ We maintain backward compatibility within major versions and provide clear migration guides for breaking changes.
96
+
97
+ ## License
98
+
99
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
100
+
101
+ Free to use in both personal and commercial projects.
102
+
103
+ ## Integration in the Suite
104
+
105
+ This package is part of the Wexample Suite — a collection of high-quality, modular tools designed to work seamlessly together across multiple languages and environments.
106
+
107
+ ### Related Packages
108
+
109
+ The suite includes packages for configuration management, file handling, prompts, and more. Each package can be used independently or as part of the integrated suite.
110
+
111
+ Visit the [Wexample Suite documentation](https://docs.wexample.com) for the complete package ecosystem.
112
+
113
+ # About us
114
+
115
+ [Wexample](https://wexample.com) stands as a cornerstone of the digital ecosystem — a collective of seasoned engineers, researchers, and creators driven by a relentless pursuit of technological excellence. More than a media platform, it has grown into a vibrant community where innovation meets craftsmanship, and where every line of code reflects a commitment to clarity, durability, and shared intelligence.
116
+
117
+ This packages suite embodies this spirit. Trusted by professionals and enthusiasts alike, it delivers a consistent, high-quality foundation for modern development — open, elegant, and battle-tested. Its reputation is built on years of collaboration, refinement, and rigorous attention to detail, making it a natural choice for those who demand both robustness and beauty in their tools.
118
+
119
+ Wexample cultivates a culture of mastery. Each package, each contribution carries the mark of a community that values precision, ethics, and innovation — a community proud to shape the future of digital craftsmanship.
120
+
121
+ Python Doctrine inspired ORM
122
+
123
+ ## Known Limitations & Roadmap
124
+
125
+ Current limitations and planned features are tracked in the GitHub issues.
126
+
127
+ See the [project roadmap](https://github.com/wexample/python-migration/issues) for upcoming features and improvements.
128
+
129
+ ## Status & Compatibility
130
+
131
+ **Maturity**: Production-ready
132
+
133
+ **Python Support**: >=3.10
134
+
135
+ **OS Support**: Linux, macOS, Windows
136
+
137
+ **Status**: Actively maintained
138
+
139
+ ## Useful Links
140
+
141
+ - **Homepage**: https://github.com/wexample/python-migration
142
+ - **Documentation**: [docs.wexample.com](https://docs.wexample.com)
143
+ - **Issue Tracker**: https://github.com/wexample/python-migration/issues
144
+ - **Discussions**: https://github.com/wexample/python-migration/discussions
145
+ - **PyPI**: [pypi.org/project/migration](https://pypi.org/project/migration/)
146
+
147
+ ## Migration Notes
148
+
149
+ When upgrading between major versions, refer to the migration guides in the documentation.
150
+
151
+ Breaking changes are clearly documented with upgrade paths and examples.
@@ -6,7 +6,8 @@ build-backend = "pdm.backend"
6
6
 
7
7
  [project]
8
8
  name = "wexample-migration"
9
- version = "9.3.2"
9
+ version = "9.3.3"
10
+ description = "Generic ORM abstractions on top of SQLAlchemy."
10
11
  authors = [
11
12
  { name = "weeger", email = "contact@wexample.com" },
12
13
  ]
@@ -18,10 +19,8 @@ classifiers = [
18
19
  ]
19
20
  dependencies = [
20
21
  "attrs>=23.1.0",
21
- "cattrs>=23.1.0",
22
- "pyyaml>=6.0",
23
- "wexample-filestate>=11.2.0",
24
- "wexample-helpers>=13.1.0",
22
+ "sqlalchemy>=2,<3",
23
+ "wexample-helpers>=13.0.0",
25
24
  ]
26
25
 
27
26
  [project.readme]
@@ -31,12 +30,13 @@ content-type = "text/markdown"
31
30
  [project.license]
32
31
  text = "MIT"
33
32
 
33
+ [project.urls]
34
+ homepage = "https://github.com/wexample/python-orm"
35
+
34
36
  [project.optional-dependencies]
35
37
  dev = [
36
38
  "pytest",
37
39
  "pytest-cov",
38
- "pytest-cov>=7.1.0",
39
- "pytest>=9.0.2",
40
40
  ]
41
41
 
42
42
  [tool.setuptools.packages.find]
@@ -0,0 +1,13 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_orm.common.abstract_repositories_manager import (
4
+ AbstractRepositoriesManager,
5
+ )
6
+ from wexample_orm.entity.abstract_entity import AbstractEntity
7
+ from wexample_orm.repository.abstract_repository import AbstractRepository
8
+
9
+ __all__ = [
10
+ "AbstractEntity",
11
+ "AbstractRepositoriesManager",
12
+ "AbstractRepository",
13
+ ]
@@ -0,0 +1,54 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.classes.private_field import private_field
8
+ from wexample_helpers.decorator.base_class import base_class
9
+
10
+ if TYPE_CHECKING:
11
+ from wexample_orm.entity.abstract_entity import AbstractEntity
12
+ from wexample_orm.repository.abstract_repository import AbstractRepository
13
+
14
+
15
+ @base_class
16
+ class AbstractRepositoriesManager(BaseClass):
17
+ repository_classes: list[type[AbstractRepository]] = public_field(
18
+ description="Repository classes this manager can instantiate, keyed by their entity type.",
19
+ factory=list,
20
+ )
21
+ session: Any | None = public_field(
22
+ description="SQLAlchemy session shared by all repositories built by this manager.",
23
+ default=None,
24
+ )
25
+ _instances: dict[type[AbstractEntity], AbstractRepository] = private_field(
26
+ description="Cache of repository instances keyed by entity type.",
27
+ factory=dict,
28
+ )
29
+
30
+ def get(self, entity_type: type[AbstractEntity]) -> AbstractRepository:
31
+ if entity_type in self._instances:
32
+ return self._instances[entity_type]
33
+
34
+ for repository_class in self.repository_classes:
35
+ if repository_class.get_entity_type() is entity_type:
36
+ repository = repository_class(session=self.session)
37
+ self._instances[entity_type] = repository
38
+ return repository
39
+
40
+ from wexample_orm.exception.unknown_repository_exception import (
41
+ UnknownRepositoryException,
42
+ )
43
+
44
+ raise UnknownRepositoryException.for_entity(entity_type)
45
+
46
+ def get_by_table_name(self, table_name: str) -> AbstractRepository | None:
47
+ for repository_class in self.repository_classes:
48
+ entity_type = repository_class.get_entity_type()
49
+ if entity_type.get_entity_name() == table_name:
50
+ return self.get(entity_type)
51
+ return None
52
+
53
+ def get_classes(self) -> list[type[AbstractRepository]]:
54
+ return list(self.repository_classes)
@@ -0,0 +1,31 @@
1
+ from __future__ import annotations
2
+
3
+ from sqlalchemy import Integer, Sequence
4
+ from sqlalchemy.orm import Mapped, as_declarative, declared_attr, mapped_column
5
+ from wexample_helpers.helpers.string import string_to_snake_case
6
+
7
+
8
+ @as_declarative()
9
+ class AbstractEntity:
10
+ __abstract__ = True
11
+
12
+ @declared_attr
13
+ def __tablename__(cls) -> str:
14
+ return cls.get_entity_name()
15
+
16
+ @classmethod
17
+ def get_entity_name(cls) -> str:
18
+ return string_to_snake_case(cls.__name__)
19
+
20
+ @classmethod
21
+ def get_sequence(cls) -> Sequence:
22
+ return Sequence(f"{cls.get_entity_name()}_id_seq")
23
+
24
+ @declared_attr
25
+ def id(cls) -> Mapped[int]:
26
+ return mapped_column(
27
+ Integer,
28
+ cls.get_sequence(),
29
+ primary_key=True,
30
+ autoincrement=True,
31
+ )
@@ -0,0 +1,16 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_helpers.exception.undefined_exception import UndefinedException
4
+
5
+
6
+ class RepositorySessionMissingException(UndefinedException):
7
+ error_code = "ORM_REPOSITORY_SESSION_MISSING"
8
+
9
+ def __init__(self, repository_class_name: str) -> None:
10
+ super().__init__(
11
+ message=(
12
+ f"Repository '{repository_class_name}' has no session configured. "
13
+ "Pass `session=` when instantiating the manager or repository."
14
+ ),
15
+ data={"repository_class": repository_class_name},
16
+ )
@@ -0,0 +1,21 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from wexample_helpers.exception.undefined_exception import UndefinedException
6
+
7
+ if TYPE_CHECKING:
8
+ from wexample_orm.entity.abstract_entity import AbstractEntity
9
+
10
+
11
+ class UnknownRepositoryException(UndefinedException):
12
+ error_code = "ORM_UNKNOWN_REPOSITORY"
13
+
14
+ @classmethod
15
+ def for_entity(
16
+ cls, entity_type: type[AbstractEntity]
17
+ ) -> UnknownRepositoryException:
18
+ return cls(
19
+ message=f"No repository registered for entity type: {entity_type.__name__}",
20
+ data={"entity_type": entity_type.__name__},
21
+ )
@@ -0,0 +1,63 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.decorator.base_class import base_class
8
+
9
+ if TYPE_CHECKING:
10
+ from sqlalchemy.orm import Query, Session
11
+
12
+ from wexample_orm.entity.abstract_entity import AbstractEntity
13
+
14
+
15
+ @base_class
16
+ class AbstractRepository(BaseClass):
17
+ session: Any | None = public_field(
18
+ description="SQLAlchemy session used by this repository.",
19
+ default=None,
20
+ )
21
+
22
+ @classmethod
23
+ def get_entity_type(cls) -> type[AbstractEntity]:
24
+ cls._raise_not_implemented_error()
25
+
26
+ def find(self, entity_id: int) -> AbstractEntity | None:
27
+ if self.session is None:
28
+ return None
29
+ return self.session.get(self.get_entity_type(), entity_id)
30
+
31
+ def find_one_by(self, filter_clause: Any) -> AbstractEntity | None:
32
+ return self.query().filter(filter_clause).first()
33
+
34
+ def query(self) -> Query:
35
+ self._require_session()
36
+ return self.session.query(self.get_entity_type())
37
+
38
+ def save(
39
+ self,
40
+ entity: AbstractEntity,
41
+ *,
42
+ flush: bool = True,
43
+ refresh: bool = False,
44
+ ) -> AbstractEntity:
45
+ self._require_session()
46
+
47
+ self.session.add(entity)
48
+ if flush:
49
+ self.session.commit()
50
+ if refresh:
51
+ self.session.refresh(entity)
52
+ return entity
53
+
54
+ def _require_session(self) -> Session:
55
+ if self.session is None:
56
+ from wexample_orm.exception.repository_session_missing_exception import (
57
+ RepositorySessionMissingException,
58
+ )
59
+
60
+ raise RepositorySessionMissingException(
61
+ repository_class_name=self.__class__.__name__
62
+ )
63
+ return self.session
@@ -0,0 +1,70 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from wexample_helpers.classes.base_class import BaseClass
6
+ from wexample_helpers.classes.field import public_field
7
+ from wexample_helpers.classes.private_field import private_field
8
+ from wexample_helpers.decorator.base_class import base_class
9
+
10
+ if TYPE_CHECKING:
11
+ from sqlalchemy.engine import Engine
12
+ from sqlalchemy.orm import Session
13
+
14
+
15
+ @base_class
16
+ class AbstractSessionFactory(BaseClass):
17
+ """Build and cache a SQLAlchemy engine + sessionmaker from a DSN.
18
+
19
+ Subclasses can override `_engine_kwargs` / `_session_kwargs` to inject
20
+ project-specific options (echo, pool_pre_ping, expire_on_commit, ...).
21
+ """
22
+
23
+ dsn: str = public_field(
24
+ description="SQLAlchemy URL (e.g. 'postgresql+psycopg://user:pwd@host/db', 'sqlite:///:memory:').",
25
+ )
26
+ engine_options: dict[str, Any] = public_field(
27
+ description="Extra kwargs forwarded to sqlalchemy.create_engine().",
28
+ factory=dict,
29
+ )
30
+ session_options: dict[str, Any] = public_field(
31
+ description="Extra kwargs forwarded to sqlalchemy.orm.sessionmaker().",
32
+ factory=dict,
33
+ )
34
+ _engine: Engine | None = private_field(
35
+ description="Cached SQLAlchemy engine.",
36
+ default=None,
37
+ )
38
+ _sessionmaker: Any | None = private_field(
39
+ description="Cached sessionmaker bound to the engine.",
40
+ default=None,
41
+ )
42
+
43
+ def create_session(self) -> Session:
44
+ if self._sessionmaker is None:
45
+ from sqlalchemy.orm import sessionmaker
46
+
47
+ self._sessionmaker = sessionmaker(
48
+ bind=self.get_engine(), **self._session_kwargs()
49
+ )
50
+ return self._sessionmaker()
51
+
52
+ def dispose(self) -> None:
53
+ """Release the engine's connection pool. Useful in tests and teardown."""
54
+ if self._engine is not None:
55
+ self._engine.dispose()
56
+ self._engine = None
57
+ self._sessionmaker = None
58
+
59
+ def get_engine(self) -> Engine:
60
+ if self._engine is None:
61
+ from sqlalchemy import create_engine
62
+
63
+ self._engine = create_engine(self.dsn, **self._engine_kwargs())
64
+ return self._engine
65
+
66
+ def _engine_kwargs(self) -> dict[str, Any]:
67
+ return dict(self.engine_options)
68
+
69
+ def _session_kwargs(self) -> dict[str, Any]:
70
+ return dict(self.session_options)
@@ -0,0 +1,11 @@
1
+ from __future__ import annotations
2
+
3
+ from wexample_orm.testing.sqlite import (
4
+ in_memory_engine,
5
+ in_memory_session,
6
+ )
7
+
8
+ __all__ = [
9
+ "in_memory_engine",
10
+ "in_memory_session",
11
+ ]