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 ef5f9d4
Showing
8 changed files
with
227 additions
and
48 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
114 changes: 114 additions & 0 deletions
114
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,114 @@ | ||
"""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", | ||
) | ||
|
||
# Update restart_number for workflows that have been restarted more than 10 times | ||
# (thus erroneously having the following generation_number), in case some of them | ||
# were created before the limit on 9 restarts was introduced. | ||
op.get_bind().execute( | ||
sa.text( | ||
""" | ||
UPDATE __reana.workflow AS w | ||
SET | ||
generation_number = to_be_updated.new_generation_number, | ||
restart_number = (w.restart_number + (w.generation_number - to_be_updated.new_generation_number) * 10) | ||
FROM ( | ||
SELECT MIN(w1.generation_number) - 1 AS new_generation_number, w1.workspace_path | ||
FROM __reana.workflow w1 | ||
WHERE w1.restart AND w1.restart_number = 0 | ||
GROUP BY w1.workspace_path | ||
) AS to_be_updated | ||
WHERE w.workspace_path = to_be_updated.workspace_path | ||
""" | ||
), | ||
) | ||
|
||
|
||
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.