Skip to content

Commit 94f8065

Browse files
committed
Merge branch 'main' into develop
2 parents 00d9db1 + 35c7b1a commit 94f8065

24 files changed

+132
-63
lines changed

.github/workflows/release_docs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: peaceiris/actions-gh-pages@v3
3131
with:
3232
personal_token: ${{ secrets.PERSONAL_TOKEN }}
33-
external_repository: qaspen-python/qaspen-python.github.io
33+
external_repository: psqlpy-python/psqlpy-python.github.io
3434
publish_dir: ./docs_dist
3535
user_name: "github-actions[bot]"
3636
user_email: "github-actions[bot]@users.noreply.github.com"

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "psqlpy"
3-
version = "0.7.3"
3+
version = "0.7.5"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Driver for PostgreSQL written fully in Rust and exposed to Python.
88
Main goals of the library is speed and type safety.
99

1010
## Documentation
11-
You can find full documentation here - [PSQLPy documentation](https://qaspen-python.github.io/)
11+
You can find full documentation here - [PSQLPy documentation](https://psqlpy-python.github.io/)
1212

1313
## Installation
1414

docs/.vuepress/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export default defineUserConfig({
1212
bundler: viteBundler(),
1313

1414
theme: hopeTheme({
15-
repo: "qaspen-python/psqlpy",
15+
repo: "psqlpy-python/psqlpy",
1616

1717
repoLabel: "GitHub",
1818

1919
repoDisplay: true,
2020

2121
sidebar,
2222

23-
hostname: "https://qaspen-python.github.io/",
23+
hostname: "https://psqlpy-python.github.io/",
2424

2525
plugins: {
2626
readingTime: false,

docs/.vuepress/theme.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { hopeTheme } from "vuepress-theme-hope";
22
import sidebar from "./sidebar.js";
33

44
export default hopeTheme({
5-
hostname: "https://github.com/qaspen-python/psqlpy",
5+
hostname: "https://github.com/psqlpy-python/psqlpy",
66

77
iconAssets: "fontawesome-with-brands",
88

99
logo: "./logo.png",
1010

11-
repo: "qaspen-python/psqlpy",
11+
repo: "psqlpy-python/psqlpy",
1212

1313
docsDir: "src",
1414

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ poetry add psqlpy
5252
@tab git
5353

5454
```bash
55-
pip install git+https://github.com/qaspen-python/psqlpy
55+
pip install git+https://github.com/psqlpy-python/psqlpy
5656
```
5757

5858
:::

docs/components/connection_pool.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ You cannot set the minimum size for the connection pool, by it is 0.
1313
So, if you set `max_db_pool_size` to 100, pool will create new connection every time there aren't enough connections to handle the load.
1414
:::
1515

16-
## Connection pool methods
17-
1816
### All available ConnectionPool parameters
1917

2018
- `dsn`: Full dsn connection string.
@@ -105,9 +103,6 @@ db_pool: Final = ConnectionPool(
105103
db_name="postgres",
106104
max_db_pool_size=10,
107105
)
108-
109-
async def main() -> None:
110-
111106
```
112107

113108
### Initialize Connection Pool with DSN
@@ -124,9 +119,6 @@ db_pool: Final = ConnectionPool(
124119
dsn="postgres://postgres:postgres@localhost:5432/postgres",
125120
max_db_pool_size=10,
126121
)
127-
128-
async def main() -> None:
129-
130122
```
131123

132124
### Create Connection Pool with one function
@@ -143,6 +135,26 @@ db_pool: Final = connect(
143135
```
144136
`connect` function has the same parameters as `ConnectionPool`.
145137

138+
### Use Connection Pool as context manager
139+
```py
140+
from typing import Final
141+
142+
from psqlpy import ConnectionPool
143+
144+
145+
async def main() -> None:
146+
with ConnectionPool(
147+
dsn="postgres://postgres:postgres@localhost:5432/postgres",
148+
max_db_pool_size=10,
149+
) as db_pool:
150+
# ConnectionPool is opened
151+
await db_pool.execute("SOME_SQL")
152+
# ConnectionPool is opened
153+
# ConnectionPool is closed
154+
```
155+
156+
## Connection pool methods
157+
146158
### Resize
147159
Resize connection pool capacity.
148160

docs/contribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ There are several rules for contributors:
88
- Please do not add malware.
99
- Please make sure that your request solves the problem.
1010

11-
If you struggle with something or feel frustrated, you either create an issue, create a [discussions](https://github.com/qaspen-python/psqlpy/discussions). page or publish a draft PR and ask your question in the description.
11+
If you struggle with something or feel frustrated, you either create an issue, create a [discussions](https://github.com/psqlpy-python/psqlpy/discussions). page or publish a draft PR and ask your question in the description.
1212

1313
We have lots of tests in CI. But since CI runs from first-time contributors should be approved, you better test locally. It just takes less time to prepare PR for merging.
1414

docs/introduction/lets_start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ poetry add psqlpy
2222
@tab git
2323

2424
```bash
25-
pip install git+https://github.com/qaspen-python/psqlpy
25+
pip install git+https://github.com/psqlpy-python/psqlpy
2626
```
2727

2828
:::

docs/usage/frameworks/fastapi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
3131
)
3232
app.state.db_pool = db_pool
3333
yield
34-
await db_pool.close()
34+
db_pool.close()
3535

3636

3737
app = FastAPI(lifespan=lifespan)

docs/usage/frameworks/litestar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def start_db_pool(app: Litestar) -> ConnectionPool:
3535

3636
async def stop_db_pool(app: Litestar) -> None:
3737
"""Close database connection pool."""
38-
if getattr(app.state, "engine", None):
38+
if getattr(app.state, "db_pool", None):
3939
db_pool = cast(ConnectionPool, app.state.db_pool)
4040
db_pool.close()
4141

docs/usage/frameworks/robyn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def pg_pool_example(request: Request) -> list[dict[Any, Any]]:
3737

3838
async def main() -> None:
3939
try:
40-
app.start(host="127.0.0.1", port=8000)
40+
await app.start(host="127.0.0.1", port=8000)
4141
finally:
4242
db_pool.close()
4343

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "psqlpy-docs",
3-
"repository": "git@github.com:qaspen-python/psqlpy.git",
3+
"repository": "git@github.com:psqlpy-python/psqlpy.git",
44
"author": "Kiselev Aleksandr <askiselev00@gmail.com>",
55
"license": "MIT",
66
"private": false,

psqlpy-stress/poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

psqlpy-stress/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pytz = "^2024.1"
2121
psycopg2-binary = "^2.9.9"
2222
gunicorn = "^21.2.0"
2323
uvicorn = "^0.29.0"
24-
psqlpy-piccolo = { git = "https://github.com/qaspen-python/psqlpy-piccolo" }
24+
psqlpy-piccolo = { git = "https://github.com/psqlpy-python/psqlpy-piccolo" }
2525
piccolo = "^1.12.0"
2626

2727

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ module-name = "psqlpy._internal"
4444
features = ["pyo3/extension-module"]
4545

4646
[project.urls]
47-
homepage = "https://github.com/qaspen-python/psqlpy"
48-
repository = "https://github.com/qaspen-python/psqlpy"
49-
documentation = "https://qaspen-python.github.io/"
47+
homepage = "https://github.com/psqlpy-python/psqlpy"
48+
repository = "https://github.com/psqlpy-python/psqlpy"
49+
documentation = "https://psqlpy-python.github.io/"
5050

5151
[tool.isort]
5252
profile = "black"

python/psqlpy/_internal/__init__.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,14 @@ class ConnectionPool:
12151215
- `ca_file`: Loads trusted root certificates from a file.
12161216
The file should contain a sequence of PEM-formatted CA certificates.
12171217
"""
1218+
def __iter__(self: Self) -> Self: ...
1219+
def __enter__(self: Self) -> Self: ...
1220+
def __exit__(
1221+
self: Self,
1222+
exception_type: type[BaseException] | None,
1223+
exception: BaseException | None,
1224+
traceback: types.TracebackType | None,
1225+
) -> None: ...
12181226
def status(self: Self) -> ConnectionPoolStatus:
12191227
"""Return information about connection pool.
12201228

python/tests/test_connection_pool.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,15 @@ async def test_close_connection_pool() -> None:
175175

176176
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
177177
await pg_pool.execute("SELECT 1")
178+
179+
180+
async def test_connection_pool_as_context_manager() -> None:
181+
"""Test connection pool as context manager."""
182+
with ConnectionPool(
183+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
184+
) as pg_pool:
185+
res = await pg_pool.execute("SELECT 1")
186+
assert res.result()
187+
188+
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
189+
await pg_pool.execute("SELECT 1")

python/tests/test_ssl_mode.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,25 @@ async def test_ssl_mode_require_pool_builder(
7474
pool = builder.build()
7575

7676
await pool.execute("SELECT 1")
77+
78+
79+
async def test_ssl_mode_require_without_ca_file(
80+
postgres_host: str,
81+
postgres_user: str,
82+
postgres_password: str,
83+
postgres_port: int,
84+
postgres_dbname: str,
85+
) -> None:
86+
builder = (
87+
ConnectionPoolBuilder()
88+
.max_pool_size(10)
89+
.host(postgres_host)
90+
.port(postgres_port)
91+
.user(postgres_user)
92+
.password(postgres_password)
93+
.dbname(postgres_dbname)
94+
.ssl_mode(SslMode.Require)
95+
)
96+
pool = builder.build()
97+
98+
await pool.execute("SELECT 1")

src/Untitled-1.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/driver/common_options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl TargetSessionAttrs {
6464
}
6565

6666
#[pyclass]
67-
#[derive(Clone, Copy)]
67+
#[derive(Clone, Copy, PartialEq)]
6868
pub enum SslMode {
6969
/// Do not use TLS.
7070
Disable,

src/driver/connection_pool.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::runtime::tokio_runtime;
22
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod};
3-
use openssl::ssl::{SslConnector, SslMethod};
3+
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
44
use postgres_openssl::MakeTlsConnector;
5-
use pyo3::{pyclass, pyfunction, pymethods, PyAny};
5+
use pyo3::{pyclass, pyfunction, pymethods, Py, PyAny};
66
use std::{sync::Arc, vec};
77
use tokio_postgres::NoTls;
88

@@ -13,7 +13,7 @@ use crate::{
1313
};
1414

1515
use super::{
16-
common_options::{ConnRecyclingMethod, LoadBalanceHosts, SslMode, TargetSessionAttrs},
16+
common_options::{self, ConnRecyclingMethod, LoadBalanceHosts, SslMode, TargetSessionAttrs},
1717
connection::Connection,
1818
utils::build_connection_config,
1919
};
@@ -104,6 +104,15 @@ pub fn connect(
104104
builder.set_ca_file(ca_file)?;
105105
let tls_connector = MakeTlsConnector::new(builder.build());
106106
mgr = Manager::from_config(pg_config, tls_connector, mgr_config);
107+
} else if let Some(ssl_mode) = ssl_mode {
108+
if ssl_mode == common_options::SslMode::Require {
109+
let mut builder = SslConnector::builder(SslMethod::tls())?;
110+
builder.set_verify(SslVerifyMode::NONE);
111+
let tls_connector = MakeTlsConnector::new(builder.build());
112+
mgr = Manager::from_config(pg_config, tls_connector, mgr_config);
113+
} else {
114+
mgr = Manager::from_config(pg_config, NoTls, mgr_config);
115+
}
107116
} else {
108117
mgr = Manager::from_config(pg_config, NoTls, mgr_config);
109118
}
@@ -244,6 +253,28 @@ impl ConnectionPool {
244253
)
245254
}
246255

256+
#[must_use]
257+
pub fn __iter__(self_: Py<Self>) -> Py<Self> {
258+
self_
259+
}
260+
261+
#[allow(clippy::needless_pass_by_value)]
262+
fn __enter__(self_: Py<Self>) -> Py<Self> {
263+
self_
264+
}
265+
266+
#[allow(clippy::needless_pass_by_value)]
267+
fn __exit__(
268+
self_: Py<Self>,
269+
_exception_type: Py<PyAny>,
270+
_exception: Py<PyAny>,
271+
_traceback: Py<PyAny>,
272+
) {
273+
pyo3::Python::with_gil(|gil| {
274+
self_.borrow(gil).close();
275+
});
276+
}
277+
247278
#[must_use]
248279
pub fn status(&self) -> ConnectionPoolStatus {
249280
let inner_status = self.0.status();
@@ -413,7 +444,7 @@ impl ConnectionPool {
413444
Ok(Connection::new(Some(Arc::new(db_connection)), None))
414445
}
415446

416-
/// Return new single connection.
447+
/// Close connection pool.
417448
///
418449
/// # Errors
419450
/// May return Err Result if cannot get new connection from the pool.

0 commit comments

Comments
 (0)