Skip to content

Commit dc5967f

Browse files
authored
chore: PostgreSQL backend adaptation to new expression-dialect architecture (#4)
* feat(postgres): Refactor dialect formatting for SQL injection prevention and add RICH_AVAILABLE check - Refactored ormat_limit_offset in dialect.py to separate SQL fragments and parameters. - Added RICH_AVAILABLE check to __main__.py for conditional rich output. * fix(postgres): parameterize LIMIT/OFFSET in PostgresDialect Updated the `format_limit_offset` method in `PostgresDialect` to return a tuple of `(sql_fragment, params_list)`. This change aligns with the new parameterization strategy, ensuring that LIMIT and OFFSET values are passed as parameters instead of being embedded directly into the SQL string. * feat(postgres): migrate backend to modern Mixin+Protocol architecture - Replace old capabilities system with modern Mixin+Protocol architecture - Update backend.py to use new StorageBackend base class - Implement async backend with async/await support using psycopg - Add async transaction manager for PostgreSQL - Remove dependency on deprecated capabilities module - Maintain all PostgreSQL-specific behaviors and features - Ensure version-specific feature support remains intact - Update imports to use correct module paths (DatabaseType, TypeMapping) BREAKING CHANGE: Removes old capabilities system in favor of protocol-based architecture * feat: Add async implementations for basic and query providers and test files - Add async methods to BasicProvider and QueryProvider - Update test files to include async fixtures - Ensure parity between sync and async functionality - Update schema files to support both sync and async models * feat: Update backend implementations with proper execute method signatures - Add async implementations for all backend methods - Update execute methods to accept options parameter while maintaining backward compatibility - Add missing methods like ping and _handle_error - Ensure sync/async parity across all implementations * feat(postgres): enhance PostgreSQL backend implementation with proper transaction management - Fix transaction manager initialization for both sync and async backends - Add proper auto-commit handling for PostgreSQL - Enhance error handling for PostgreSQL-specific errors (duplicate keys, foreign key constraints, deadlocks) - Implement get_default_adapter_suggestions method for both sync and async backends - Align PostgreSQL backend implementation with base class requirements - Follow SQLite implementation patterns for consistency - Improve async transaction manager with complete async method implementations - Ensure proper connection handling in transaction managers - Add proper isolation level support for PostgreSQL transactions * Add codex instructions - Add codex.md with project-specific guidance - Reference shared .gemini guidelines via relative paths * fix: add placeholder conversion and async provider support - Add _prepare_sql_and_params to convert '?' to '%s' for PostgreSQL - Add async provider methods for PostgreSQL basic tests - Add _active_async_backends tracking for proper cleanup * refactor(postgres): consolidate transaction management and improve async support - Merge AsyncPostgresTransactionManager into transaction.py module - Add PostgresListAdapter for native PostgreSQL array type handling - Improve connection parameter handling in async backend - Add proper async test support in providers - Enable pytest asyncio auto mode * docs: add comprehensive documentation and query test bridges - Rename .gemini/ to .claude/ for consistency with other repos - Add complete bilingual documentation (en_US/zh_CN) covering: - Introduction and supported versions - Installation and configuration - PostgreSQL-specific features (JSONB, arrays, UUID, etc.) - Transaction support with isolation levels - Type adapters for Python-PostgreSQL mapping - Testing guide with parallel execution warning - Troubleshooting for common issues - Add query test bridge files for testsuite integration * fix: update basic test schemas for PostgreSQL compatibility - mixed_annotation_items.sql: Change id INT to SERIAL for auto-increment - type_adapter_tests.sql: Increase VARCHAR length from 3 to 10 for custom_bool fields These changes allow basic tests to pass when using custom column mappings with UseColumn annotations. * fix: share backend instance across related models and use native TEXT[] for tags - Fix _setup_multiple_models to share a single backend instance across all related models, resolving foreign key constraint failures when setting up tables with dependencies - Change tags column from TEXT to TEXT[] to leverage PostgreSQL's native array support for list fields - Add PostgreSQL-specific PostgresMixedAnnotationModel with native list type annotation for tags field * docs: add array type comparison documentation - Add array_comparison.md (EN/CN) comparing PostgreSQL arrays with databases without native array support - Update arrays.md with corrected operator syntax examples - Add link to array_comparison.md in README.md The documentation covers: - Native array support differences - Array operators (@>, &&, ANY(), ALL(), array_length) - Portability strategies for multi-database projects * docs: revamp README to AI-guided exploration style * docs: add provider responsibilities to testing documentation - Add Provider Responsibilities section to testing README - Document cleanup order requirements: DROP TABLE → Close Cursors → Disconnect - Document PostgreSQL-specific considerations (custom types) * test: remove unreferenced testsuite files and update postgres backend - Remove testsuite test files that are no longer referenced: - test_function_expressions.py - test_grouped_aggregate.py - test_joins_2.py - test_relation_cache.py - test_relation_path_processing.py - test_relations_complex.py - Update postgres backend implementation files * test(backend): rewrite PostgreSQL backend tests for new architecture - Remove deprecated expression tests using old create_expression API - Rewrite CRUD, transaction, dialect, and column mapping tests - Fix version test to remove non-working cache check - Fix adapters test for correct supported_types structure - Fix async transaction test to avoid direct autocommit manipulation * chore: add changelog fragment and update CI sources for backend dialect formatting
1 parent 69c65e1 commit dc5967f

154 files changed

Lines changed: 6742 additions & 4008 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
File renamed without changes.
File renamed without changes.
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
> **Important**: This document is a supplement to the main **Testing Architecture and Execution Guide** ([local](../../python-activerecord/.gemini/testing.md) | [github](https://github.com/Rhosocial/python-activerecord/blob/main/.gemini/testing.md)). Please read the core documentation first to understand the overall testing architecture, provider patterns, and capability negotiation. This guide focuses only on details specific to the PostgreSQL backend.
1+
> **Important**: This document is a supplement to the main **Testing Architecture and Execution Guide** ([local](../../python-activerecord/.claude/testing.md) | [github](https://github.com/Rhosocial/python-activerecord/blob/main/.claude/testing.md)). Please read the core documentation first to understand the overall testing architecture, provider patterns, and capability negotiation. This guide focuses only on details specific to the PostgreSQL backend.
22
33
# PostgreSQL Backend Testing Guide
44

@@ -201,3 +201,39 @@ pytest -k "jsonb"
201201
# View coverage
202202
pytest --cov=rhosocial.activerecord.backend.impl.postgres tests/
203203
```
204+
205+
## 8. CRITICAL: No Parallel Test Execution
206+
207+
**Tests MUST be executed serially. Do NOT use pytest-xdist or similar parallel execution tools.**
208+
209+
### Why Parallel Execution Is Not Supported
210+
211+
The test suite creates database tables with **fixed names** (e.g., `users`, `orders`, `posts`). Each test:
212+
213+
1. Drops existing tables: `DROP TABLE IF EXISTS ... CASCADE`
214+
2. Creates fresh tables with the same names
215+
3. Inserts test data
216+
4. Cleans up after completion
217+
218+
If tests run in parallel, they will:
219+
- Drop tables while other tests are using them → Errors
220+
- Create conflicting data → Test failures
221+
- Cleanup prematurely → Data loss
222+
223+
### What NOT To Do
224+
225+
```bash
226+
# DO NOT use parallel execution
227+
pytest -n auto # ❌ WILL CAUSE FAILURES
228+
pytest -n 4 # ❌ WILL CAUSE FAILURES
229+
pytest --dist=loadfile # ❌ WILL CAUSE FAILURES
230+
```
231+
232+
### Correct Execution
233+
234+
```bash
235+
# Always run tests serially (default behavior)
236+
pytest # ✅ Correct - serial execution
237+
```
238+
239+
For more details, see the main project's [Testing Documentation](https://github.com/Rhosocial/python-activerecord/blob/main/.claude/testing.md).

‎.github/workflows/test.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ jobs:
6767
6868
- name: Install activerecord and testsuite from git
6969
run: |
70-
pip install rhosocial-activerecord==1.0.0.dev14
71-
pip install rhosocial-activerecord-testsuite==1.0.0.dev4
70+
pip install git+https://github.com/rhosocial/python-activerecord.git@release/v1.0.0.dev15
71+
pip install git+https://github.com/rhosocial/python-activerecord-testsuite.git@release/v1.0.0.dev5
7272
7373
- name: Create postgres scenario config
7474
run: |

0 commit comments

Comments
 (0)