Commit c9e5425
authored
feat!: refactor Litestar extension - remove wrapper classes, unify handlers (#96)
## Summary
Major refactor of the Litestar extension that removes the `DatabaseConfig` wrapper class pattern, unifies duplicate handler files, and fixes type errors and test pollution issues.
## Breaking Changes
**BREAKING**: The `DatabaseConfig` wrapper class has been removed.
### Before
```python
from sqlspec.extensions.litestar import DatabaseConfig, SQLSpecPlugin
from sqlspec.adapters.asyncpg import AsyncpgConfig
asyncpg_config = AsyncpgConfig(pool_config={"dsn": "..."})
db_config = DatabaseConfig(config=asyncpg_config) # Wrapper!
plugin = SQLSpecPlugin(configs=[db_config])
```
### After
```python
from sqlspec import SQLSpec
from sqlspec.extensions.litestar import SQLSpecPlugin
from sqlspec.adapters.asyncpg import AsyncpgConfig
asyncpg_config = AsyncpgConfig(
pool_config={"dsn": "..."},
extension_config={"litestar": {"commit_mode": "autocommit"}}
)
sql = SQLSpec()
sql.add_config(asyncpg_config)
plugin = SQLSpecPlugin(sqlspec=sql)
```
## Migration Guide
1. **Remove `DatabaseConfig` wrapper**:
- Pass core config classes directly to `SQLSpec`
- Use `extension_config` field for Litestar-specific settings
2. **Update Litestar plugin initialization**:
- Create `SQLSpec()` instance
- Add configs with `sql.add_config()`
- Pass `SQLSpec` instance to `SQLSpecPlugin(sqlspec=sql)`
3. **Move Litestar settings to `extension_config`**:
```python
config = AsyncpgConfig(
pool_config={"dsn": "..."},
extension_config={
"litestar": {
"connection_key": "db_connection",
"pool_key": "db_pool",
"session_key": "db_session",
"commit_mode": "autocommit"
}
}
)
```
## Changes
### Removed DatabaseConfig Wrapper Class
- **Deleted**: `sqlspec/extensions/litestar/config.py` (292 lines)
- **Deleted**: `tests/unit/test_extensions/test_litestar/test_config.py` (482 lines)
- Plugin now accepts `SQLSpec` instance directly
- All configs from `SQLSpec` are automatically included
### Added extension_config Infrastructure
- Added `extension_config` field to all core database config classes
- Added `LitestarConfig` TypedDict for type-safe Litestar configuration
- Configs read from `extension_config["litestar"]` namespace
### Unified Handler Implementation
- **Merged**: `handlers_async.py` + `handlers_sync.py` → `handlers.py`
- Single implementation with conditional `is_async` logic
- Direct `await` for async drivers, `ensure_async_()` for sync drivers
- Simplified plugin with single `_setup_handlers()` method
### Plugin Refactoring
- Rewritten to work directly with core configs
- Uses `config.is_async` to route to appropriate handlers
- Eliminated wrapper overhead and complexity
- Cleaner dependency injection setup
### Fixed Type Errors
- Removed dead `pool_type` code
- Fixed `"Never" is not awaitable` errors
- Removed redundant `get_config()` delegation
- All mypy and pyright errors resolved
### Fixed Test Database File Pollution
- Tests no longer create 30+ database files in project root
- Fixed SQLite URI mode handling (7 locations)
- Converted hardcoded paths to use pytest `tmp_path` (5 locations)
- Added defensive validation to SQLite config classes
### Code Quality Improvements
- Removed defensive programming anti-patterns
- Fixed nested import violations
- Enforced CLAUDE.md standards throughout
- All functions under 75-line limit
## Benefits
- **Simpler API**: No wrapper classes, direct config usage
- **Less Code**: Removed ~774 lines (wrapper + duplicate handlers)
- **Type Safety**: mypy and pyright clean
- **Better Architecture**: Plugin uses core configs directly
- **Cleaner Tests**: No file pollution, proper temp directories
## Test Results
```
✅ 14/14 Litestar handler tests passing
✅ 230/230 integration tests passing
✅ mypy: 0 errors
✅ pyright: 0 errors
✅ ruff: all checks passed
```
## Files Changed
```
24 files changed, 808 insertions(+), 1118 deletions(-)
```
**Major changes**:
- Deleted wrapper class and duplicate handlers
- Refactored plugin to use `SQLSpec` directly
- Added `extension_config` infrastructure
- Fixed all type errors and test pollution
- Updated all examples and documentation
## Related
- Supersedes portions of PR #83
- Includes merged PR #97 (handler unification + fixes)1 parent b9f24f4 commit c9e5425
File tree
24 files changed
+808
-1118
lines changed- docs/examples
- sqlspec
- adapters
- aiosqlite
- sqlite
- extensions/litestar
- tests
- integration/test_adapters
- test_aiosqlite
- test_duckdb
- test_sqlite
- unit/test_extensions/test_litestar
24 files changed
+808
-1118
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
84 | 82 | | |
85 | | - | |
| 83 | + | |
| 84 | + | |
86 | 85 | | |
87 | 86 | | |
88 | 87 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | | - | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | | - | |
34 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
51 | | - | |
| 53 | + | |
| 54 | + | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
52 | 49 | | |
53 | | - | |
| 50 | + | |
| 51 | + | |
54 | 52 | | |
55 | 53 | | |
56 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
24 | | - | |
25 | | - | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
39 | 36 | | |
40 | | - | |
| 37 | + | |
| 38 | + | |
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | | - | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
27 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
82 | 91 | | |
83 | 92 | | |
84 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
67 | 79 | | |
68 | 80 | | |
69 | 81 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
164 | 173 | | |
165 | 174 | | |
166 | 175 | | |
| |||
0 commit comments