Skip to content

feat(database): add shared utilities module#4

Merged
maltonoloco merged 13 commits intoOpenTaberna:mainfrom
PhilippTheServer:feature/crud-item-store
Dec 7, 2025
Merged

feat(database): add shared utilities module#4
maltonoloco merged 13 commits intoOpenTaberna:mainfrom
PhilippTheServer:feature/crud-item-store

Conversation

@PhilippTheServer
Copy link
Contributor

Centralize common imports and fallback exceptions to eliminate
code duplication across database components. Reduces ~100 lines
of duplicated try/except blocks.

  • Add get_logger() with fallback NullLogger
  • Add DatabaseError, NotFoundError, InternalError fallback classes
  • Add get_settings() import helper
  • Enable optional dependencies for shared modules## Core Change

Description

Setup database modul. Database usage is now possible.

Scope

  • Authentication/Authorization
  • Database layer
  • Configuration management
  • Error handling
  • Logging infrastructure
  • Dependency management
  • Other:

Motivation

For better dev and a step to prod release.

Testing Strategy

  • Unit tests updated/added
  • Integration tests updated/added
  • All existing tests pass

Documentation

  • Architecture documentation updated
  • API documentation updated
  • Migration guide created if needed

Checklist

  • Backward compatibility considered
  • All services tested
  • Security implications reviewed
  • Performance impact assessed

Centralize common imports and fallback exceptions to eliminate
code duplication across database components. Reduces ~100 lines
of duplicated try/except blocks.

- Add get_logger() with fallback NullLogger
- Add DatabaseError, NotFoundError, InternalError fallback classes
- Add get_settings() import helper
- Enable optional dependencies for shared modules
Implement declarative base and reusable mixins for database models.

- Add Base class with to_dict() and __repr__() methods
- Add TimestampMixin for automatic created_at/updated_at tracking
- Add SoftDeleteMixin with soft_delete() and restore() methods
- Use UTC timezone for all timestamp fields
- Enable generic model functionality across all tables
Implement async PostgreSQL engine with connection pooling and
lifecycle management.

- Add create_engine() with configurable connection pool
- Add create_test_engine() with NullPool for testing
- Add init_database()/close_database() for app lifecycle
- Add get_engine() singleton accessor
- Configure connection pool with overflow and timeout settings
- Integrate error handling with DatabaseError/InternalError
- Add comprehensive logging for all operations
Implement async session factory with context managers and
FastAPI dependency injection support.

- Add create_session_factory() with configurable options
- Add get_session() context manager with auto-commit/rollback
- Add get_session_dependency() for FastAPI Depends()
- Wrap all errors in DatabaseError for consistent handling
- Enable expire_on_commit=False for post-commit access
- Integrate with engine module for database connection
Implement type-safe generic CRUD repository with comprehensive
query operations.

- Add BaseRepository[ModelType] with Generic[T] for type safety
- Add CRUD methods: get, get_by, get_all, filter with pagination
- Add bulk operations: create_many, update_many, delete_many
- Add helper methods: count, exists, get_or_raise, get_by_or_raise
- Add execute() for custom SQL queries
- Integrate NotFoundError for missing entities
- Add soft delete support via is_deleted filter
- Enable reusable repository pattern across all models
Implement transaction context manager with savepoint support
for nested transactions.

- Add transaction() context manager with auto-commit/rollback
- Add nested transaction support via savepoints
- Wrap all errors in DatabaseError
- Enable manual transaction control in async contexts
- Integrate with session module for database operations
Implement database health checks and connection monitoring.

- Add check_database_health() with connectivity test and latency
- Add get_database_info() with version, connections, pool stats
- Return structured HealthStatus and DatabaseInfo dataclasses
- Measure query execution time for performance monitoring
- Query PostgreSQL system tables for connection statistics
- Enable health endpoint integration for production monitoring
Implement Alembic migration utilities for schema management.

- Add get_alembic_config() for configuration loading
- Add run_migrations() for applying pending migrations
- Add rollback_migration() for reverting changes
- Add create_migration() for generating new migrations
- Add get_migration_history() for tracking applied migrations
- Configure async database URL for Alembic
- Enable programmatic migration control
- Integrate logging for all migration operations
Export database module public interface for clean imports.

- Export engine functions: init_database, close_database, get_engine
- Export session utilities: get_session, get_session_dependency, AsyncSession
- Export base classes: Base, TimestampMixin, SoftDeleteMixin
- Export repository: BaseRepository
- Export transaction: transaction
- Export health: check_database_health, get_database_info
- Export migrations: run_migrations, rollback_migration, create_migration
- Enable clean import syntax: from app.shared.database import ...
Add comprehensive tests for database module structure and imports.

- Add 23 tests covering all database components
- Test utils fallback classes and logger integration
- Test Base model methods (to_dict, __repr__)
- Test TimestampMixin and SoftDeleteMixin attributes
- Test all module imports and public API exports
- Test repository CRUD method existence
- Test migration utility functions
- Verify module structure and interface consistency
Add complete documentation for database module usage and architecture.

- Add Quick Start guide with setup and basic usage
- Document configuration options and environment variables
- Explain Base model and mixin usage patterns
- Document repository pattern with CRUD examples
- Cover session management and dependency injection
- Explain transaction handling and nested transactions
- Document health checks and monitoring
- Cover Alembic migrations and schema management
- Add error handling best practices
- Include testing guidelines and examples
- Add troubleshooting section for common issues
@maltonoloco maltonoloco merged commit 946d66d into OpenTaberna:main Dec 7, 2025
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants