-
Notifications
You must be signed in to change notification settings - Fork 8
Implementation of Database Migrations & Automated Backups #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markoceri
wants to merge
56
commits into
dev
Choose a base branch
from
sqlalchemy-and-elembic
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ller domain with imperative mapping
…HomeLoadsProfile with custom serialization
…tory and ORM mappings
…sitory and ORM mappings
…nitor with ORM mappings
…icies persistence
… proper registration
… for value object conversions
…ySource and EnergyMonitor
…in ExternalService
…s script for Alembic setup checks
…ation in energy and miner tables
…e field to miners
…ations across multiple tables
…ase initialization instructions for SQLAlchemy
…iguration details
…migration handling
…l, and documentation improvements
…ructions and migration enforcement details
…update related references
…ergyMonitor and EnergySource
…gration tests for SQLAlchemy event listeners and migration functionality
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request upgrades the data management layer of the application by implementing a robust migration system. By transitioning from manual SQLite schema changes to a structured system using SQLAlchemy ORM with Alembic migrations, this approach allows for safe database evolution without the risk of data loss.
Key Drivers
Previously, changing any entity property required manual SQL intervention, which often led to data being wiped if the schema became incompatible. To support the long-term growth of the project and protect existing data, I have implemented an automated migration workflow that:
Key Improvements
1. Data Preservation Through Alembic Migrations
I integrated Alembic to handle schema evolutions automatically. The system now:
Reference: See
docs/ALEMBIC_MIGRATIONS.mdfor complete migration system documentation.2. Automated Backups for Safety
As an extra layer of security, I introduced an automatic backup routine that:
dbname_backup_YYYYMMDD_HHMMSS.dbBACKUP_BEFORE_MIGRATIONenvironment variableConfiguration:
Reference: See Startup Workflow section in the migration guide.
3. Standardized Access with SQLAlchemy ORM
By using SQLAlchemy with imperative mapping, I have:
Technical Details:
@dataclassobjectsReference: See DDD Principles section and Migration Example.
4. Future-Proofing and Developer Experience
This setup removes the "fear of updating" and enables:
Developer Workflow:
Reference: See Practical Example for complete step-by-step workflow.
Key Updates
1. SQLAlchemy Implementation
Replaced raw SQLite queries with SQLAlchemy models:
MinerStatusType,EnergySourceType)Files Changed:
edge_mining/adapters/domain/*/tables.py- Table definitions and mappingsedge_mining/adapters/domain/*/repositories.py- SQLAlchemy repositoriesedge_mining/adapters/infrastructure/persistence/sqlalchemy/- Infrastructure layer2. Alembic Initialization
Initialized Alembic for version control of database schema:
alembic.iniwith proper paths and settingsalembic/env.pythat integrates with SQLAlchemy metadatascript.py.mako)3. Pre-Migration Backup Hook
Added automatic database backup before migrations:
BaseSQLAlchemyRepository.initialize_database()4. Automatic Migration on Startup
Updated deployment and startup scripts:
bootstrap.pyto useinitialize_database()methodscripts/migrate.pyfor manual migration managementCLI Commands:
Testing
Comprehensive test coverage added:
Unit Tests (42 tests)
tests/unit/adapters/domain/energy/test_tables_event_listeners.pyIntegration Tests (34 tests)
tests/integration/adapters/persistence/test_sqlalchemy_energy_repositories.py(21 tests)tests/integration/adapters/persistence/test_alembic_migrations.py(9 tests)tests/integration/adapters/persistence/test_e2e_persistence.py(8 tests)Run tests:
Documentation
Complete documentation has been added:
docs/ALEMBIC_MIGRATIONS.mddocs/MIGRATION_EXAMPLE.mdConfiguration
Environment Variables
Add to your
.envfile:Settings
Configuration in
edge_mining/shared/settings/settings.py:Migration from Previous Version
For existing installations:
Automatic Migration (Recommended):
The system will automatically:
Note: If your database is already up-to-date, no migrations will be applied and no backup will be created.
Manual Migration (Development Only):
If you need to manage migrations manually for development purposes:
alembic revision --autogenerate -m "Initial schema with all tables"For production use, always rely on automatic migrations that run on application startup.
Rollback Plan
If issues arise after deployment:
Benefits Summary
Next Steps
After merging:
docs/MIGRATION_EXAMPLE.md)This PR represents a major infrastructure improvement that will enable rapid, safe evolution of the data model while maintaining architectural integrity and data safety.