Commit dcab3d1
feat: Add database adapter interface for multi-backend support (Phase 2)
Implement the adapter pattern to abstract database-specific logic and enable
PostgreSQL support alongside MySQL. This is Phase 2 of the PostgreSQL support
implementation plan (POSTGRES_SUPPORT.md).
New modules:
- src/datajoint/adapters/base.py: DatabaseAdapter abstract base class defining
the complete interface for database operations (connection management, SQL
generation, type mapping, error translation, introspection)
- src/datajoint/adapters/mysql.py: MySQLAdapter implementation with extracted
MySQL-specific logic (backtick quoting, ON DUPLICATE KEY UPDATE, SHOW
commands, information_schema queries)
- src/datajoint/adapters/postgres.py: PostgreSQLAdapter implementation with
PostgreSQL-specific SQL dialect (double-quote quoting, ON CONFLICT,
INTERVAL syntax, enum type management)
- src/datajoint/adapters/__init__.py: Adapter registry with get_adapter()
factory function
Dependencies:
- Added optional PostgreSQL dependency: psycopg2-binary>=2.9.0
(install with: pip install 'datajoint[postgres]')
Tests:
- tests/unit/test_adapters.py: Comprehensive unit tests for both adapters
(24 tests for MySQL, 21 tests for PostgreSQL when psycopg2 available)
- All tests pass or properly skip when dependencies unavailable
- Pre-commit hooks pass (ruff, mypy, codespell)
Key features:
- Complete abstraction of database-specific SQL generation
- Type mapping between DataJoint core types and backend SQL types
- Error translation from backend errors to DataJoint exceptions
- Introspection query generation for schema, tables, columns, keys
- PostgreSQL enum type lifecycle management (CREATE TYPE/DROP TYPE)
- No changes to existing DataJoint code (adapters are standalone)
Phase 2 Status: ✅ Complete
Next phases: Configuration updates, connection refactoring, SQL generation
integration, testing with actual databases.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent f4b0258 commit dcab3d1
File tree
6 files changed
+2826
-0
lines changed- src/datajoint/adapters
- tests/unit
6 files changed
+2826
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments