Skip to content

Commit 2ca1def

Browse files
committed
add leftovers
1 parent 2d28f08 commit 2ca1def

File tree

5 files changed

+88
-76
lines changed

5 files changed

+88
-76
lines changed

docs/examples/usage/test_configuration_22.txt

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

docs/examples/usage/test_configuration_23.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,35 @@
66
import pytest
77

88

9+
def test_extension_config() -> None:
10+
from sqlspec.adapters.asyncpg import AsyncpgConfig
11+
12+
config = AsyncpgConfig(
13+
pool_config={"dsn": "postgresql://localhost/db"},
14+
extension_config={
15+
"litestar": {
16+
"connection_key": "db_connection",
17+
"session_key": "db_session",
18+
"pool_key": "db_pool",
19+
"commit_mode": "autocommit",
20+
"enable_correlation_middleware": True,
21+
}
22+
},
23+
)
24+
assert config.extension_config == {
25+
"litestar": {
26+
"connection_key": "db_connection",
27+
"session_key": "db_session",
28+
"pool_key": "db_pool",
29+
"commit_mode": "autocommit",
30+
"enable_correlation_middleware": True,
31+
}
32+
}
33+
34+
935
@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled")
1036
def test_environment_based_configuration() -> None:
1137
"""Test environment-based configuration pattern."""
12-
from sqlspec.adapters.asyncpg import AsyncpgConfig
1338

1439
# Mock environment variables
1540
env_vars = {
@@ -21,6 +46,8 @@ def test_environment_based_configuration() -> None:
2146
}
2247

2348
with patch.dict(os.environ, env_vars, clear=False):
49+
from sqlspec.adapters.asyncpg import AsyncpgConfig
50+
2451
config = AsyncpgConfig(
2552
pool_config={
2653
"host": os.getenv("DB_HOST", "localhost"),

docs/examples/usage/test_configuration_26.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Test configuration example: Best practice - Tune pool sizes."""
22

3-
43
def test_tune_pool_sizes_best_practice() -> None:
54
"""Test pool sizing best practices for different workloads."""
65

@@ -13,3 +12,15 @@ def test_tune_pool_sizes_best_practice() -> None:
1312
io_bound_pool_config = {"min_size": 20, "max_size": 50}
1413
assert io_bound_pool_config["min_size"] == 20
1514
assert io_bound_pool_config["max_size"] == 50
15+
16+
def test_disable_security_checks_best_practice() -> None:
17+
"""Test disabling security checks when necessary."""
18+
19+
from sqlspec.core.statement import StatementConfig
20+
21+
# Example: Disabling security checks for trusted internal queries
22+
statement_config = StatementConfig(
23+
dialect="postgres",
24+
enable_validation=False, # Skip security checks
25+
)
26+
assert statement_config.enable_validation is False

docs/examples/usage/test_configuration_27.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""Test configuration example: Best practice - Clean up resources."""
22

3-
import tempfile
43

54
import pytest
65

76

87
@pytest.mark.asyncio
98
async def test_cleanup_resources_best_practice() -> None:
109
"""Test resource cleanup best practice."""
10+
import tempfile
11+
1112
from sqlspec import SQLSpec
1213
from sqlspec.adapters.aiosqlite import AiosqliteConfig
1314

docs/usage/configuration.rst

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,17 @@ Basic Migration Config
251251

252252
**Migration CLI**
253253

254-
.. literalinclude:: /examples/usage/test_configuration_22.txt
255-
:language: text
256-
:caption: `migration CLI`
254+
.. code-block:: bash
255+
256+
# Create migration
257+
sqlspec --config myapp.config create-migration -m "Add users table"
258+
259+
# Apply migrations
260+
sqlspec --config myapp.config upgrade
261+
262+
# Rollback
263+
sqlspec --config myapp.config downgrade -1
264+
257265
258266
259267
Extension Migration Versioning
@@ -281,42 +289,22 @@ Framework integrations can be configured via ``extension_config``.
281289
Litestar Plugin Configuration
282290
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
283291

284-
.. code-block:: python
285-
286-
from sqlspec.adapters.asyncpg import AsyncpgConfig
287-
288-
config = AsyncpgConfig(
289-
pool_config={"dsn": "postgresql://localhost/db"},
290-
extension_config={
291-
"litestar": {
292-
"connection_key": "db_connection",
293-
"session_key": "db_session",
294-
"pool_key": "db_pool",
295-
"commit_mode": "autocommit",
296-
"enable_correlation_middleware": True,
297-
}
298-
}
299-
)
292+
.. literalinclude:: /examples/usage/test_configuration_23.py
293+
:language: python
294+
:caption: `litestar plugin configuration`
295+
:lines: 10-31
296+
:dedent: 2
300297

301298
Environment-Based Configuration
302299
-------------------------------
303300

304301
Use environment variables for configuration:
305302

306-
.. code-block:: python
307-
308-
import os
309-
from sqlspec.adapters.asyncpg import AsyncpgConfig
310-
311-
config = AsyncpgConfig(
312-
pool_config={
313-
"host": os.getenv("DB_HOST", "localhost"),
314-
"port": int(os.getenv("DB_PORT", "5432")),
315-
"user": os.getenv("DB_USER"),
316-
"password": os.getenv("DB_PASSWORD"),
317-
"database": os.getenv("DB_NAME"),
318-
}
319-
)
303+
.. literalinclude:: /examples/usage/test_configuration_23.py
304+
:language: python
305+
:caption: `environnment-based configuration`
306+
:lines: 49-59
307+
:dedent: 4
320308

321309
Configuration Best Practices
322310
-----------------------------
@@ -325,59 +313,52 @@ Configuration Best Practices
325313

326314
Always use pooling in production:
327315

328-
.. code-block:: python
329-
330-
config = AsyncpgConfig(
331-
pool_config={
332-
"dsn": "postgresql://localhost/db",
333-
"min_size": 10,
334-
"max_size": 20,
335-
}
336-
)
316+
.. literalinclude:: /examples/usage/test_configuration_24.py
317+
:language: python
318+
:caption: `connection pooling`
319+
:lines: 11-13
320+
:dedent: 2
337321

338322
**2. Enable Caching**
339323

340324
Enable caching to avoid recompiling SQL statements:
341325

342-
.. code-block:: python
343-
344-
statement_config = StatementConfig(
345-
dialect="postgres",
346-
enable_caching=True
347-
)
326+
.. literalinclude:: /examples/usage/test_configuration_25.py
327+
:language: python
328+
:caption: `enable caching`
329+
:lines: 6-8
330+
:dedent: 2
348331

349332
**3. Tune Pool Sizes**
350333

351334
Size pools based on your workload:
352335

353-
.. code-block:: python
354-
355-
# CPU-bound workload
356-
pool_config = {"min_size": 5, "max_size": 10}
357-
358-
# I/O-bound workload
359-
pool_config = {"min_size": 20, "max_size": 50}
336+
.. literalinclude:: /examples/usage/test_configuration_26.py
337+
:language: python
338+
:caption: `tune pool sizes`
339+
:lines: 6-14
340+
:dedent: 2
360341

361342
**4. Disable Validation in Production**
362343

363344
For trusted, performance-critical queries:
364345

365-
.. code-block:: python
346+
.. literalinclude:: /examples/usage/test_configuration_26.py
347+
:language: python
348+
:caption: `no validation`
349+
:lines: 19-25
350+
:dedent: 2
366351

367-
statement_config = StatementConfig(
368-
dialect="postgres",
369-
enable_validation=False, # Skip security checks
370-
)
371352

372353
**5. Clean Up Resources**
373354

374355
Always close pools on shutdown:
375356

376-
.. code-block:: python
377-
378-
# Synchronous cleanup (automatic with atexit)
379-
# Asynchronous cleanup (manual)
380-
await spec.close_all_pools()
357+
.. literalinclude:: /examples/usage/test_configuration_27.py
358+
:language: python
359+
:caption: `cleanup resources`
360+
:lines: 10-27
361+
:dedent: 2
381362

382363
Next Steps
383364
----------

0 commit comments

Comments
 (0)