Skip to content

fix: Backup database automatically on migration for data safety#162

Merged
DylanDevelops merged 2 commits into
mainfrom
ravel/unify-db-migrations
Jul 21, 2026
Merged

fix: Backup database automatically on migration for data safety#162
DylanDevelops merged 2 commits into
mainfrom
ravel/unify-db-migrations

Conversation

@DylanDevelops

Copy link
Copy Markdown
Owner

Pull Request Checklist

  • I have read and followed the contribution guidelines.
  • My pull request targets the main branch of tmpo.
  • I have tested these changes locally on my machine.

Closes #139

Description

This pull request introduces a more robust and reliable database migration system to handle schema changes for legacy and future versions of the application. It ensures that schema upgrades are tracked with explicit migration keys, creates pre-migration backups for safety, and adds comprehensive tests to cover migration scenarios. The most important changes are summarized below:

Database Migration System Improvements:

  • Added a formal migration system with versioned migration keys, including a new migration (Migration002_EntrySchema) to ensure the time_entries table has the hourly_rate and milestone_name columns and supporting indexes. This replaces the previous ad-hoc approach in Initialize. [1] [2]
  • Implemented the hasPendingMigrations method to detect if any migrations are needed, and the migrateEntrySchema method to bring legacy databases up to date in an idempotent and reliable way.
  • Replaced error-string matching for column existence with a new columnExists function that uses SQLite's PRAGMA table_info for accuracy.

Backup and Migration Safety:

  • Modified Initialize to create a pre-migration backup (with a -premigration suffix) if there are pending migrations, and to automatically remove the backup upon successful migration or retain it if migration fails. This prevents data loss during upgrades. [1] [2]
  • Refactored backup creation logic into a reusable createBackup method that supports custom filename suffixes. [1] [2]

Testing Enhancements:

  • Added comprehensive tests to verify migration behavior, including: ensuring missing columns are added, migrations are idempotent, backups are correctly created/removed, and that fresh or already-migrated databases do not trigger unnecessary backups. [1] [2]

Code Cleanup:

  • Removed legacy error-matching logic for column existence and moved all schema-altering logic into migration functions. [1] [2]
  • Cleaned up and reorganized imports and table creation logic in Initialize. [1] [2]

These changes make schema migrations safer and more maintainable, reduce risk of data loss, and improve test coverage for database upgrades.

@github-actions github-actions Bot added the type: fix A pull request that fixes a bug (fix:) label Jul 21, 2026
@DylanDevelops
DylanDevelops merged commit b2ffafb into main Jul 21, 2026
6 checks passed
@DylanDevelops
DylanDevelops deleted the ravel/unify-db-migrations branch July 21, 2026 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A pull request that fixes a bug (fix:)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify database migration system

1 participant