auto-rollback based on git history#93
Open
xshiart wants to merge 1 commit into
Open
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
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
Adds two linked capabilities to
jetbase upgrade:git_commit_hashcolumn onjetbase_migrations.--auto-rollback) - when an already-applied migration's file is missing locally (e.g. after checking out an older branch/release), the upgrade no longer just errors out. Instead it rolls the database back to the latest applied migration whose file still exists, then re-runs the upgrade from that baseline. The rollback SQL for the missing files is recovered read-only from the recorded commit viagit show- the working tree andHEADare never touched.Motivation
Previously, running
upgradewhen a migrated version's file was absent raisedFileNotFoundErrorand stopped, leaving the operator to manually restore files or hand-fix the migrations table. This is common when switching between branches/releases whosemigrations/directories diverge. With the recorded commit hash, jetbase can self-heal: roll back to a state that matches the current files and continue.What changed
git_commit_hashcolumnCREATE TABLEstatement for all six dialects (Postgres, SQLite, MySQL, Snowflake, Databricks, ClickHouse).run_migrationrecordsget_current_commit_hash()on every versioned / first-time-repeatable insert;INSERTstatements updated for the default, ClickHouse, and Databricks dialects.ensure_git_commit_hash_column()(ALTER TABLE ADD COLUMN, guarded by SQLAlchemy reflection) run at the start of each upgrade.Automatic safe rollback
jetbase/engine/git.py- read-only git helpers (get_current_commit_hash,get_repo_root,read_file_at_commit) usingsubprocess, no new dependencies.jetbase/commands/safe_rollback.py-attempt_safe_rollback()determines the baseline, recovers missing files' rollback SQL from the recorded commit, and rolls back under the migration lock.upgrade_cmdgains anauto_rollbackparam and a--auto-rollbackCLI flag; on a missing-file error it attempts the rollback, then re-validates and re-upgrades from the new baseline.MissingMigrationFileError(subclassesFileNotFoundErrorfor backward compatibility) distinguishes the missing-applied-file case so the recovery flow targets it precisely.Graceful fallback (preserves old behaviour)
If the repo isn't a git checkout, the latest applied migration has no recorded hash, the missing files aren't a contiguous trailing run, or a file can't be recovered from git,
attempt_safe_rollback()returnsFalseand the original error is re-raised.Scope notes