-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: auto-downgrading revision #4678
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
base: main
Are you sure you want to change the base?
feat: auto-downgrading revision #4678
Conversation
@EnotShow is attempting to deploy a commit to the KeepHQ Team on Vercel. A member of the Team first needs to authorize it. |
hey @EnotShow - thanks for this PR! as this PR introduces some major changes to how Keep works, I thought I would add some e2e tests for it (mainly install Keep, do some migration, then downgrade). wdyt? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comments
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4678 +/- ##
==========================================
- Coverage 46.19% 46.03% -0.16%
==========================================
Files 165 165
Lines 17495 17564 +69
==========================================
+ Hits 8081 8086 +5
- Misses 9414 9478 +64 β View full report in Codecov by Sentry. π New features to boost your workflow:
|
β¦EnotShow/keep into feature/auto-downgrade-revision
β¦EnotShow/keep into feature/auto-downgrade-revision
Hey @shahargl! Thx for your review! I made all the required changes, so it's ready for re-review :) |
Signed-off-by: Ihor <88674695+EnotShow@users.noreply.github.com>
@EnotShow let tests pass and if they will I'll review |
Signed-off-by: Ihor Korolenko <88674695+korolenkowork@users.noreply.github.com>
π¨ BugBot couldn't runPull requests from forked repositories are not yet supported (requestId: serverGenReqId_48ddba26-49ed-4dcf-a98c-b6051afed68d). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Migration Function Fails with Incomplete Cleanup
The copy_migrations
function has insufficient error handling and an incomplete cleanup mechanism. If os.makedirs
fails, the function continues execution, causing subsequent file operations to fail. Furthermore, the cleanup logic only removes files and symlinks, leaving subdirectories. This incomplete cleanup, coupled with clearing the destination before verifying the source, risks data loss of previous backups if the subsequent copy operation fails or conflicts arise.
keep/api/core/db_on_start.py#L183-L204
keep/keep/api/core/db_on_start.py
Lines 183 to 204 in 71e81be
# Ensure destination exists | |
try: | |
os.makedirs(local_migrations_path, exist_ok=True) | |
except Exception as e: | |
logger.error(f"Failed to create local migrations folder with error: {e}") | |
# Clear previous versioned migrations to ensure only migrations relevant to the current version are present | |
for filename in os.listdir(local_migrations_path): | |
file_path = os.path.join(local_migrations_path, filename) | |
if os.path.isfile(file_path) or os.path.islink(file_path): | |
os.remove(file_path) | |
# Alembic needs the full migration history to safely perform a downgrade to earlier versions | |
# Copy new migrations | |
for item in os.listdir(source_versions_path): | |
src = os.path.join(source_versions_path, item) | |
dst = os.path.join(local_migrations_path, item) | |
if os.path.isdir(src): | |
shutil.copytree(src, dst, dirs_exist_ok=True) | |
else: | |
shutil.copy(src, dst) |
Bug: Environment Variable Name Discrepancy
Environment variable name mismatch: The docker-compose
files set MIGRATION_PATH
(singular), but the Python code in keep/api/core/db_on_start.py
expects MIGRATIONS_PATH
(plural). This causes the application to ignore the intended /tmp/migrations
path and fall back to the default /tmp/keep/migrations
for database migrations.
tests/e2e_tests/docker-compose-e2e-postgres.yml#L41-L42
keep/tests/e2e_tests/docker-compose-e2e-postgres.yml
Lines 41 to 42 in 71e81be
- SECRET_MANAGER_DIRECTORY=/app | |
- MIGRATION_PATH=/tmp/migrations |
tests/e2e_tests/docker-compose-e2e-postgres-migrations.yml#L40-L41
keep/tests/e2e_tests/docker-compose-e2e-postgres-migrations.yml
Lines 40 to 41 in 71e81be
- SECRET_MANAGER_DIRECTORY=/app | |
- MIGRATION_PATH=/tmp/migrations |
Bug: Database Migration Error Handling Flaws
The migrate_db
function incorrectly assumes any exception during alembic.command.upgrade()
indicates the database needs a downgrade, potentially triggering inappropriate downgrade attempts for unrelated issues (e.g., connectivity, permissions, syntax errors). Additionally, copy_migrations
is unconditionally called at the end of migrate_db
, which can corrupt the local migration backup if a downgrade operation failed, preventing future successful downgrades.
keep/api/core/db_on_start.py#L275-L289
keep/keep/api/core/db_on_start.py
Lines 275 to 289 in 71e81be
logger.info("Running migrations...") | |
try: | |
alembic.command.upgrade(config, "head") | |
except Exception as e: | |
logger.error(f"{e} it's seems like Keep was rolled back to a previous version") | |
if not os.getenv("ALLOW_DB_DOWNGRADE", "false") == "true": | |
logger.error(f"ALLOW_DB_DOWNGRADE is not set to true, but the database schema ({current_revision}) doesn't match application version ({expected_revision})") | |
raise RuntimeError("Database downgrade is not allowed") | |
logger.info("Downgrading database schema...") | |
downgrade_db(config, expected_revision, local_migrations_path, app_migrations_path) | |
# Copy migrations to local folder for safe downgrade purposes | |
copy_migrations(app_migrations_path, local_migrations_path) |
Bug: Missing Step ID Causes Empty Revision Output
The workflow references steps.get_revision.outputs.revision
to set the WORKFLOW_REVISION
environment variable. However, no step in the workflow has the ID get_revision
. This prevents the revision
output from being captured, resulting in an empty WORKFLOW_REVISION
and breaking the intended revision comparison logic.
.github/workflows/run-migrations-e2e-tests.yml#L335-L336
keep/.github/workflows/run-migrations-e2e-tests.yml
Lines 335 to 336 in 71e81be
env: | |
WORKFLOW_REVISION: ${{ steps.get_revision.outputs.revision }} |
.github/workflows/run-migrations-e2e-tests.yml#L479-L480
keep/.github/workflows/run-migrations-e2e-tests.yml
Lines 479 to 480 in 71e81be
env: | |
WORKFLOW_REVISION: ${{ steps.get_revision.outputs.revision }} |
Was this report helpful? Give feedback by reacting with π or π
Hey @korolenkowork, honestly it looks good but it requires some big effort for me to review itβs ready. This one touches the main flow so Iβm bit worried. |
Closes #4665
π Description
This PR introduces automatic database downgrading support when rolling back KeepHQ to a previous version.
β Checks
βΉ Additional Information