Skip to content

Refactor postgres base repository with error constants extraction - #9

Merged
RD2W merged 4 commits into
devfrom
feature/sql-db-study
Nov 30, 2025
Merged

Refactor postgres base repository with error constants extraction#9
RD2W merged 4 commits into
devfrom
feature/sql-db-study

Conversation

@RD2W

@RD2W RD2W commented Nov 28, 2025

Copy link
Copy Markdown
Owner

This PR includes several improvements to the database layer:

  1. Adds database cleanup functionality with rollback support for safer operations
  2. Refactors the postgres repository to implement a common base repository structure with transaction support
  3. Extracts error constants to a separate errors package for better organization and maintainability

RD2W added 4 commits November 26, 2025 10:45
…ion support

Key Changes:

1. Common Base Repository Structure:
- Created `internal/repository/postgres/base_repository.go` with shared functionality for all repositories
- Implemented `BaseRepository` struct with common methods: `WithContext()`, `SetTransaction()`, `ClearTransaction()`, `Exec()`, `QueryRow()`, and `Query()`
- Refactored both `PostgresNoteRepository` and `PostgresUserRepository` to embed the base repository
- Centralized context management with configurable timeouts

2. Enhanced Error Handling System:
- Extended `internal/repository/postgres/errors/errors.go` with structured error hierarchy
- Added `ErrorCode` type and `RepositoryError` struct with error codes, messages, and entity information
- Implemented specific error creation functions (e.g., `NewNoteNotFoundError`, `NewUserCreationError`)
- Maintained backward compatibility by preserving original error variables

3. Repository Options Pattern:
- Created `internal/repository/postgres/repository_options.go` with `RepositoryOption` type
- Added `WithTimeout` option for configurable timeouts
- Implemented `NewPostgresNoteRepositoryWithOpts` and `NewPostgresUserRepositoryWithOpts` for advanced configuration
- Preserved original constructors for backward compatibility

4. Transaction Support:
- Added `internal/repository/postgres/transaction_manager.go` with `TransactionManager` interface
- Implemented `PostgresTransactionManager` with `WithinTransaction` method
- Enhanced base repository to support transaction operations
- Added methods to set and clear transactions on repository instances

5. Repository Refactoring:
- Updated `note_repository.go` and `user_repository.go` to use base repository methods
- Replaced direct database calls with base repository methods (`Exec`, `QueryRow`, `Query`)
- Added proper error wrapping using enhanced error system
- Implemented proper PostgreSQL error handling (e.g., checking for `pgx.ErrNoRows`)

6. Backward Compatibility:
- All existing interfaces remain unchanged
- Original constructor signatures preserved (`NewPostgresNoteRepository`, `NewPostgresUserRepository`)
- All existing tests continue to pass without modification
- Applications (web-server, grpc-server) continue to work without changes
- Benchmark tests remain functional
@RD2W
RD2W merged commit 4a314b0 into dev Nov 30, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant