psycopg-toolkit 0.1.7__tar.gz → 0.2.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.
- psycopg_toolkit-0.2.1/.gitignore +171 -0
- psycopg_toolkit-0.2.1/.vscode/settings.json +27 -0
- psycopg_toolkit-0.2.1/PKG-INFO +331 -0
- psycopg_toolkit-0.2.1/README.md +298 -0
- psycopg_toolkit-0.2.1/pyproject.toml +197 -0
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/__init__.py +27 -13
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/core/__init__.py +2 -0
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/core/config.py +23 -26
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/core/database.py +60 -38
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/core/factory.py +25 -0
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/core/transaction.py +26 -23
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/exceptions.py +96 -0
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/repositories/__init__.py +3 -0
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/repositories/base.py +632 -0
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/utils/__init__.py +1 -1
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/utils/json_handler.py +111 -0
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/src/psycopg_toolkit/utils/psychopg_helper.py +23 -61
- psycopg_toolkit-0.2.1/src/psycopg_toolkit/utils/type_inspector.py +200 -0
- psycopg_toolkit-0.1.7/PKG-INFO +0 -214
- psycopg_toolkit-0.1.7/docs/readme.md +0 -184
- psycopg_toolkit-0.1.7/pyproject.toml +0 -55
- psycopg_toolkit-0.1.7/src/psycopg_toolkit/exceptions.py +0 -48
- psycopg_toolkit-0.1.7/src/psycopg_toolkit/repositories/__init__.py +0 -5
- psycopg_toolkit-0.1.7/src/psycopg_toolkit/repositories/base.py +0 -363
- {psycopg_toolkit-0.1.7 → psycopg_toolkit-0.2.1}/LICENSE +0 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Poetry files (project uses uv/Hatch, not Poetry)
|
|
10
|
+
poetry.lock
|
|
11
|
+
pyproject.toml.poetry
|
|
12
|
+
|
|
13
|
+
# Distribution / packaging
|
|
14
|
+
.Python
|
|
15
|
+
build/
|
|
16
|
+
develop-eggs/
|
|
17
|
+
dist/
|
|
18
|
+
downloads/
|
|
19
|
+
eggs/
|
|
20
|
+
.eggs/
|
|
21
|
+
lib/
|
|
22
|
+
lib64/
|
|
23
|
+
parts/
|
|
24
|
+
sdist/
|
|
25
|
+
var/
|
|
26
|
+
wheels/
|
|
27
|
+
share/python-wheels/
|
|
28
|
+
*.egg-info/
|
|
29
|
+
.installed.cfg
|
|
30
|
+
*.egg
|
|
31
|
+
MANIFEST
|
|
32
|
+
|
|
33
|
+
# PyInstaller
|
|
34
|
+
# Usually these files are written by a python script from a template
|
|
35
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
36
|
+
*.manifest
|
|
37
|
+
*.spec
|
|
38
|
+
|
|
39
|
+
# Installer logs
|
|
40
|
+
pip-log.txt
|
|
41
|
+
pip-delete-this-directory.txt
|
|
42
|
+
|
|
43
|
+
# Unit test / coverage reports
|
|
44
|
+
htmlcov/
|
|
45
|
+
.tox/
|
|
46
|
+
.nox/
|
|
47
|
+
.coverage
|
|
48
|
+
.coverage.*
|
|
49
|
+
.cache
|
|
50
|
+
nosetests.xml
|
|
51
|
+
coverage.xml
|
|
52
|
+
*.cover
|
|
53
|
+
*.py,cover
|
|
54
|
+
.hypothesis/
|
|
55
|
+
.pytest_cache/
|
|
56
|
+
cover/
|
|
57
|
+
|
|
58
|
+
# Translations
|
|
59
|
+
*.mo
|
|
60
|
+
*.pot
|
|
61
|
+
|
|
62
|
+
# Django stuff:
|
|
63
|
+
*.log
|
|
64
|
+
local_settings.py
|
|
65
|
+
db.sqlite3
|
|
66
|
+
db.sqlite3-journal
|
|
67
|
+
|
|
68
|
+
# Flask stuff:
|
|
69
|
+
instance/
|
|
70
|
+
.webassets-cache
|
|
71
|
+
|
|
72
|
+
# Scrapy stuff:
|
|
73
|
+
.scrapy
|
|
74
|
+
|
|
75
|
+
# Sphinx documentation
|
|
76
|
+
docs/_build/
|
|
77
|
+
|
|
78
|
+
# PyBuilder
|
|
79
|
+
.pybuilder/
|
|
80
|
+
target/
|
|
81
|
+
|
|
82
|
+
# Jupyter Notebook
|
|
83
|
+
.ipynb_checkpoints
|
|
84
|
+
|
|
85
|
+
# IPython
|
|
86
|
+
profile_default/
|
|
87
|
+
ipython_config.py
|
|
88
|
+
|
|
89
|
+
# pyenv
|
|
90
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
91
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
92
|
+
# .python-version
|
|
93
|
+
|
|
94
|
+
# pipenv
|
|
95
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
96
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
97
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
98
|
+
# install all needed dependencies.
|
|
99
|
+
#Pipfile.lock
|
|
100
|
+
|
|
101
|
+
# poetry
|
|
102
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
103
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
104
|
+
# commonly ignored for libraries.
|
|
105
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
106
|
+
#poetry.lock
|
|
107
|
+
|
|
108
|
+
# pdm
|
|
109
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
110
|
+
#pdm.lock
|
|
111
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
112
|
+
# in version control.
|
|
113
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
114
|
+
.pdm.toml
|
|
115
|
+
.pdm-python
|
|
116
|
+
.pdm-build/
|
|
117
|
+
|
|
118
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
119
|
+
__pypackages__/
|
|
120
|
+
|
|
121
|
+
# Celery stuff
|
|
122
|
+
celerybeat-schedule
|
|
123
|
+
celerybeat.pid
|
|
124
|
+
|
|
125
|
+
# SageMath parsed files
|
|
126
|
+
*.sage.py
|
|
127
|
+
|
|
128
|
+
# Environments
|
|
129
|
+
.env
|
|
130
|
+
.venv
|
|
131
|
+
env/
|
|
132
|
+
venv/
|
|
133
|
+
ENV/
|
|
134
|
+
env.bak/
|
|
135
|
+
venv.bak/
|
|
136
|
+
|
|
137
|
+
# Spyder project settings
|
|
138
|
+
.spyderproject
|
|
139
|
+
.spyproject
|
|
140
|
+
|
|
141
|
+
# Rope project settings
|
|
142
|
+
.ropeproject
|
|
143
|
+
|
|
144
|
+
# mkdocs documentation
|
|
145
|
+
/site
|
|
146
|
+
|
|
147
|
+
# mypy
|
|
148
|
+
.mypy_cache/
|
|
149
|
+
.dmypy.json
|
|
150
|
+
dmypy.json
|
|
151
|
+
|
|
152
|
+
# Pyre type checker
|
|
153
|
+
.pyre/
|
|
154
|
+
|
|
155
|
+
# pytype static type analyzer
|
|
156
|
+
.pytype/
|
|
157
|
+
|
|
158
|
+
# Cython debug symbols
|
|
159
|
+
cython_debug/
|
|
160
|
+
|
|
161
|
+
# PyCharm
|
|
162
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
163
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
164
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
165
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
166
|
+
.idea/
|
|
167
|
+
|
|
168
|
+
.claude/claudeignore
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"terminal.integrated.env.osx": {
|
|
3
|
+
"PYTHONPATH": "${workspaceFolder}/src"
|
|
4
|
+
},
|
|
5
|
+
"terminal.integrated.env.linux": {
|
|
6
|
+
"PYTHONPATH": "${workspaceFolder}/src"
|
|
7
|
+
},
|
|
8
|
+
"terminal.integrated.env.windows": {
|
|
9
|
+
"PYTHONPATH": "${workspaceFolder}/src"
|
|
10
|
+
},
|
|
11
|
+
"python.envFile": "${workspaceFolder}/.env",
|
|
12
|
+
"python.testing.pytestEnabled": true,
|
|
13
|
+
"python.testing.unittestEnabled": false,
|
|
14
|
+
"python.testing.pytestArgs": [
|
|
15
|
+
"tests",
|
|
16
|
+
"-v",
|
|
17
|
+
"-s",
|
|
18
|
+
"--tb=short"
|
|
19
|
+
],
|
|
20
|
+
"python.testing.unittestArgs": [
|
|
21
|
+
"-v",
|
|
22
|
+
"-s",
|
|
23
|
+
"./tests",
|
|
24
|
+
"-p",
|
|
25
|
+
"test_*.py"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: psycopg-toolkit
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A Python PostgreSQL database utility with connection pooling
|
|
5
|
+
Project-URL: Homepage, https://github.com/descoped/psycopg-toolkit
|
|
6
|
+
Project-URL: Repository, https://github.com/descoped/psycopg-toolkit
|
|
7
|
+
Project-URL: Documentation, https://github.com/descoped/psycopg-toolkit/tree/master/docs
|
|
8
|
+
Project-URL: Issues, https://github.com/descoped/psycopg-toolkit/issues
|
|
9
|
+
Author-email: Ove Ranheim <oranheim@gmail.com>
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: async,database,jsonb,pool,postgresql,psycopg,repository-pattern
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Framework :: Pydantic :: 2
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Database
|
|
24
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.11
|
|
28
|
+
Requires-Dist: psycopg-pool>=3.2.4
|
|
29
|
+
Requires-Dist: psycopg>=3.2.3
|
|
30
|
+
Requires-Dist: pydantic>=2.10.5
|
|
31
|
+
Requires-Dist: tenacity>=9.0.0
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# Psycopg Toolkit
|
|
35
|
+
|
|
36
|
+
[](https://github.com/descoped/psycopg-toolkit/actions/workflows/build-test-native.yml)
|
|
37
|
+
[](https://codecov.io/gh/descoped/psycopg-toolkit)
|
|
38
|
+
[](https://www.python.org/downloads/)
|
|
39
|
+
[](https://opensource.org/licenses/MIT)
|
|
40
|
+
[](https://github.com/descoped/psycopg-toolkit/releases)
|
|
41
|
+
|
|
42
|
+
A robust PostgreSQL database toolkit providing enterprise-grade connection pooling and database management capabilities for Python applications.
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
- Async-first design with connection pooling via `psycopg-pool`
|
|
47
|
+
- Comprehensive transaction management with savepoint support
|
|
48
|
+
- Type-safe repository pattern with Pydantic model validation
|
|
49
|
+
- JSONB support with automatic field detection and psycopg JSON adapters
|
|
50
|
+
- PostgreSQL array field preservation (TEXT[], INTEGER[])
|
|
51
|
+
- Automatic date/timestamp conversion for Pydantic models
|
|
52
|
+
- SQL query builder with SQL injection protection
|
|
53
|
+
- Database schema and test data lifecycle management
|
|
54
|
+
- Automatic retry mechanism with exponential backoff
|
|
55
|
+
- Granular exception hierarchy for error handling
|
|
56
|
+
- Connection health monitoring and validation
|
|
57
|
+
- Database initialization callback system
|
|
58
|
+
- Statement timeout configuration
|
|
59
|
+
- Fully typed with modern Python type hints
|
|
60
|
+
|
|
61
|
+
## Installation
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pip install psycopg-toolkit
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Quick Start
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from psycopg_toolkit import Database, DatabaseSettings
|
|
71
|
+
from uuid import uuid4
|
|
72
|
+
|
|
73
|
+
# Configure database
|
|
74
|
+
settings = DatabaseSettings(
|
|
75
|
+
host="localhost",
|
|
76
|
+
port=5432,
|
|
77
|
+
dbname="your_database",
|
|
78
|
+
user="your_user",
|
|
79
|
+
password="your_password"
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
async def main():
|
|
83
|
+
# Initialize database
|
|
84
|
+
db = Database(settings)
|
|
85
|
+
await db.init_db()
|
|
86
|
+
|
|
87
|
+
# Get transaction manager
|
|
88
|
+
tm = await db.get_transaction_manager()
|
|
89
|
+
|
|
90
|
+
# Execute in transaction
|
|
91
|
+
async with tm.transaction() as conn:
|
|
92
|
+
async with conn.cursor() as cur:
|
|
93
|
+
user_id = uuid4()
|
|
94
|
+
await cur.execute(
|
|
95
|
+
"INSERT INTO users (id, email) VALUES (%s, %s)",
|
|
96
|
+
(user_id, "user@example.com")
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# Clean up
|
|
100
|
+
await db.cleanup()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Core Components
|
|
104
|
+
|
|
105
|
+
### Database Management
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
# Health check
|
|
109
|
+
is_healthy = await db.check_pool_health()
|
|
110
|
+
|
|
111
|
+
# Connection management
|
|
112
|
+
async with db.connection() as conn:
|
|
113
|
+
async with conn.cursor() as cur:
|
|
114
|
+
await cur.execute("SELECT version()")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Transaction Management
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
# Basic transaction
|
|
121
|
+
async with tm.transaction() as conn:
|
|
122
|
+
# Operations automatically rolled back on error
|
|
123
|
+
pass
|
|
124
|
+
|
|
125
|
+
# With savepoint
|
|
126
|
+
async with tm.transaction(savepoint="user_creation") as conn:
|
|
127
|
+
# Nested transaction using savepoint
|
|
128
|
+
pass
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Repository Pattern
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from pydantic import BaseModel
|
|
135
|
+
from psycopg_toolkit import BaseRepository
|
|
136
|
+
|
|
137
|
+
class User(BaseModel):
|
|
138
|
+
id: UUID
|
|
139
|
+
email: str
|
|
140
|
+
|
|
141
|
+
class UserRepository(BaseRepository[User]):
|
|
142
|
+
def __init__(self, conn: AsyncConnection):
|
|
143
|
+
super().__init__(
|
|
144
|
+
db_connection=conn,
|
|
145
|
+
table_name="users",
|
|
146
|
+
model_class=User,
|
|
147
|
+
primary_key="id"
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
# Usage
|
|
151
|
+
async with tm.transaction() as conn:
|
|
152
|
+
repo = UserRepository(conn)
|
|
153
|
+
user = await repo.get_by_id(user_id)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### JSONB Support
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from typing import Dict, List, Any
|
|
160
|
+
from pydantic import BaseModel
|
|
161
|
+
from psycopg_toolkit import BaseRepository
|
|
162
|
+
|
|
163
|
+
class UserProfile(BaseModel):
|
|
164
|
+
id: int
|
|
165
|
+
name: str
|
|
166
|
+
# These fields are automatically detected as JSONB
|
|
167
|
+
metadata: Dict[str, Any]
|
|
168
|
+
preferences: Dict[str, str]
|
|
169
|
+
tags: List[str]
|
|
170
|
+
|
|
171
|
+
class UserRepository(BaseRepository[UserProfile, int]):
|
|
172
|
+
def __init__(self, conn):
|
|
173
|
+
super().__init__(
|
|
174
|
+
db_connection=conn,
|
|
175
|
+
table_name="user_profiles",
|
|
176
|
+
model_class=UserProfile,
|
|
177
|
+
primary_key="id"
|
|
178
|
+
# auto_detect_json=True by default
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# Usage - JSON fields handled automatically
|
|
182
|
+
user = UserProfile(
|
|
183
|
+
id=1,
|
|
184
|
+
name="John Doe",
|
|
185
|
+
metadata={"created_at": "2024-01-01", "source": "web"},
|
|
186
|
+
preferences={"theme": "dark", "language": "en"},
|
|
187
|
+
tags=["premium", "beta_tester"]
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
# JSONB fields automatically serialized/deserialized
|
|
191
|
+
created_user = await repo.create(user)
|
|
192
|
+
retrieved_user = await repo.get_by_id(1)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### PostgreSQL Arrays and Date Fields
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
from typing import List
|
|
199
|
+
from datetime import date
|
|
200
|
+
from pydantic import BaseModel
|
|
201
|
+
from psycopg_toolkit import BaseRepository
|
|
202
|
+
|
|
203
|
+
class User(BaseModel):
|
|
204
|
+
id: UUID
|
|
205
|
+
username: str
|
|
206
|
+
roles: List[str] # PostgreSQL TEXT[] array
|
|
207
|
+
permissions: List[str] # PostgreSQL TEXT[] array
|
|
208
|
+
metadata: Dict[str, Any] # JSONB field
|
|
209
|
+
birthdate: str # ISO date string
|
|
210
|
+
created_at: str # ISO datetime string
|
|
211
|
+
|
|
212
|
+
class UserRepository(BaseRepository[User, UUID]):
|
|
213
|
+
def __init__(self, conn):
|
|
214
|
+
super().__init__(
|
|
215
|
+
db_connection=conn,
|
|
216
|
+
table_name="users",
|
|
217
|
+
model_class=User,
|
|
218
|
+
primary_key="id",
|
|
219
|
+
# Preserve PostgreSQL arrays instead of JSONB
|
|
220
|
+
array_fields={"roles", "permissions"},
|
|
221
|
+
# Auto-convert dates to/from strings
|
|
222
|
+
date_fields={"birthdate", "created_at"}
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# PostgreSQL arrays are preserved, dates are auto-converted
|
|
226
|
+
user = User(
|
|
227
|
+
id=uuid4(),
|
|
228
|
+
username="john",
|
|
229
|
+
roles=["admin", "user"], # Stored as TEXT[]
|
|
230
|
+
permissions=["read", "write"], # Stored as TEXT[]
|
|
231
|
+
metadata={"dept": "IT"}, # Stored as JSONB
|
|
232
|
+
birthdate="1990-01-01", # Converts to/from PostgreSQL DATE
|
|
233
|
+
created_at="2024-01-01T12:00:00" # Converts to/from TIMESTAMP
|
|
234
|
+
)
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Schema Management
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from psycopg_toolkit.core.transaction import SchemaManager
|
|
241
|
+
|
|
242
|
+
class UserSchemaManager(SchemaManager[None]):
|
|
243
|
+
async def create_schema(self, conn: AsyncConnection) -> None:
|
|
244
|
+
await conn.execute("""
|
|
245
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
246
|
+
id UUID PRIMARY KEY,
|
|
247
|
+
email TEXT UNIQUE NOT NULL
|
|
248
|
+
)
|
|
249
|
+
""")
|
|
250
|
+
|
|
251
|
+
async def drop_schema(self, conn: AsyncConnection) -> None:
|
|
252
|
+
await conn.execute("DROP TABLE IF EXISTS users")
|
|
253
|
+
|
|
254
|
+
# Usage
|
|
255
|
+
async with tm.with_schema(UserSchemaManager()) as _:
|
|
256
|
+
# Schema available here
|
|
257
|
+
pass # Automatically dropped after
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Error Handling
|
|
261
|
+
|
|
262
|
+
```python
|
|
263
|
+
from psycopg_toolkit import (
|
|
264
|
+
DatabaseConnectionError,
|
|
265
|
+
DatabasePoolError,
|
|
266
|
+
DatabaseNotAvailable,
|
|
267
|
+
RecordNotFoundError
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
try:
|
|
271
|
+
async with tm.transaction() as conn:
|
|
272
|
+
repo = UserRepository(conn)
|
|
273
|
+
user = await repo.get_by_id(user_id)
|
|
274
|
+
except DatabaseConnectionError as e:
|
|
275
|
+
print(f"Connection error: {e.original_error}")
|
|
276
|
+
except RecordNotFoundError:
|
|
277
|
+
print(f"User {user_id} not found")
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Documentation
|
|
281
|
+
|
|
282
|
+
- [Database Management](docs/database.md)
|
|
283
|
+
- [Transaction Management](docs/transaction_manager.md)
|
|
284
|
+
- [Base Repository](docs/base_repository.md)
|
|
285
|
+
- [JSONB Support](docs/jsonb_support.md)
|
|
286
|
+
- [PsycopgHelper](docs/psycopg_helper.md)
|
|
287
|
+
|
|
288
|
+
## Running Tests
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
# Install dependencies
|
|
292
|
+
uv sync --all-groups
|
|
293
|
+
|
|
294
|
+
# Run all tests except performance tests (default)
|
|
295
|
+
uv run pytest
|
|
296
|
+
|
|
297
|
+
# Run only performance tests
|
|
298
|
+
uv run pytest -m performance
|
|
299
|
+
|
|
300
|
+
# Run all tests including performance
|
|
301
|
+
uv run pytest -m ""
|
|
302
|
+
|
|
303
|
+
# Run specific test categories
|
|
304
|
+
uv run pytest tests/unit/ # Only unit tests
|
|
305
|
+
uv run pytest -m performance # Only performance tests
|
|
306
|
+
|
|
307
|
+
# Run with coverage
|
|
308
|
+
uv run pytest --cov=src/psycopg_toolkit --cov-report=html
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Test Categories
|
|
312
|
+
|
|
313
|
+
The test suite is organized into three categories:
|
|
314
|
+
|
|
315
|
+
- **Unit tests**: Fast, isolated tests that don't require a database (in `tests/unit/`)
|
|
316
|
+
- **Integration tests**: Tests that require a real PostgreSQL database (in `tests/` root)
|
|
317
|
+
- **Performance tests**: Benchmarks and performance measurements (marked with `@pytest.mark.performance`)
|
|
318
|
+
|
|
319
|
+
Performance tests are excluded by default to keep the regular test runs fast. Use the `-m performance` flag to run them explicitly.
|
|
320
|
+
|
|
321
|
+
## Contributing
|
|
322
|
+
|
|
323
|
+
1. Fork the repository
|
|
324
|
+
2. Create a feature branch
|
|
325
|
+
3. Add tests for new features
|
|
326
|
+
4. Ensure all tests pass
|
|
327
|
+
5. Submit a pull request
|
|
328
|
+
|
|
329
|
+
## License
|
|
330
|
+
|
|
331
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|