forked from reanahub/reana-db
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
models: replace workflow.run_number with generation and restart number
Change the workflow table to split the run_number into two integers: one referring to the generation of the workflows, and the other one referring to the restart number, thus removing the limit of 9 restarts. Closes reanahub#186.
- Loading branch information
1 parent
6271ecd
commit 1432ec4
Showing
7 changed files
with
206 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
93 changes: 93 additions & 0 deletions
93
reana_db/alembic/versions/20231002_1208_b85c3e601de4_separate_run_and_restart_number.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""Separate run number into generation and restart number. | ||
Revision ID: b85c3e601de4 | ||
Revises: 377cfbfccf75 | ||
Create Date: 2023-10-02 12:08:18.292490 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "b85c3e601de4" | ||
down_revision = "377cfbfccf75" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
"""Upgrade to b85c3e601de4 revision.""" | ||
# Add new columns (generation_number, restart_number) | ||
op.add_column( | ||
"workflow", sa.Column("generation_number", sa.Integer()), schema="__reana" | ||
) | ||
op.add_column( | ||
"workflow", | ||
sa.Column("restart_number", sa.Integer(), default=0), | ||
schema="__reana", | ||
) | ||
|
||
# Data migration (split run_number into generation_number and restart_number) | ||
op.get_bind().execute( | ||
sa.text( | ||
"UPDATE __reana.workflow" | ||
" SET generation_number = FLOOR(run_number), " | ||
" restart_number = (run_number - FLOOR(run_number)) * 10" | ||
), | ||
) | ||
|
||
# Delete old constraint | ||
op.drop_constraint("_user_workflow_run_uc", "workflow", schema="__reana") | ||
|
||
# Drop old run_number column | ||
op.drop_column("workflow", "run_number", schema="__reana") | ||
|
||
# Add new constraint (the primary key is not run_number anymore, but with generation and restart number | ||
op.create_unique_constraint( | ||
"_user_workflow_run_uc", | ||
"workflow", | ||
["name", "owner_id", "generation_number", "restart_number"], | ||
schema="__reana", | ||
) | ||
|
||
|
||
def downgrade(): | ||
"""Downgrade to 377cfbfccf75 revision.""" | ||
# Revert constraint | ||
op.drop_constraint("_user_workflow_run_uc", "workflow", schema="__reana") | ||
|
||
# Add old run_number column back | ||
op.add_column("workflow", sa.Column("run_number", sa.Float()), schema="__reana") | ||
|
||
# Check that there are no workflows discarded more than 10 times | ||
# This is because of the way the info about restarts is stored in | ||
# the run_number column (see https://github.com/reanahub/reana-db/issues/186) | ||
restarted_ten_times = ( | ||
op.get_bind() | ||
.execute("SELECT COUNT(*) FROM __reana.workflow WHERE restart_number >= 10") | ||
.fetchone()[0] | ||
) | ||
if restarted_ten_times != 0: | ||
raise ValueError( | ||
"Cannot migrate database because some workflows have been restarted 10 or more times," | ||
" and the previous database revision only supports up to 9 restarts." | ||
" If you want to downgrade, you should manually delete them." | ||
) | ||
|
||
# Data migration (combine generation_number and restart_number back to run_number) | ||
op.get_bind().execute( | ||
"UPDATE __reana.workflow SET run_number=generation_number+(restart_number * 1.0 /10)" | ||
) | ||
|
||
# Drop new columns | ||
op.drop_column("workflow", "generation_number", schema="__reana") | ||
op.drop_column("workflow", "restart_number", schema="__reana") | ||
|
||
# Restore old constraint | ||
op.create_unique_constraint( | ||
"_user_workflow_run_uc", | ||
"workflow", | ||
["name", "owner_id", "run_number"], | ||
schema="__reana", | ||
) |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.