-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db): add missing constraints and relationships in
TaskJob
table (…
…#1872) Merge pull request #1872 from AntaresSimulatorTeam/feature/perf-db-taskjob-table-constraints
- Loading branch information
Showing
11 changed files
with
453 additions
and
156 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
alembic/versions/782a481f3414_fix_task_job_cascade_delete.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,113 @@ | ||
"""fix-task_job_cascade_delete | ||
Revision ID: 782a481f3414 | ||
Revises: d495746853cc | ||
Create Date: 2023-12-16 14:26:30.035324 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "782a481f3414" | ||
down_revision = "d495746853cc" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# Delete logs of tasks older than one week | ||
op.execute( | ||
""" | ||
DELETE FROM taskjoblog | ||
WHERE task_id IN (SELECT id FROM taskjob WHERE NOW() - creation_date > INTERVAL '1 week'); | ||
""" | ||
) | ||
|
||
# Delete tasks older than one week | ||
op.execute(""" DELETE FROM taskjob WHERE NOW() - creation_date > INTERVAL '1 week'; """) | ||
|
||
# Set the name "Unknown task" to tasks that have no name | ||
op.execute(""" UPDATE taskjob SET name = 'Unknown task' WHERE name IS NULL OR name = ''; """) | ||
|
||
# Attach the user "admin" to tasks that have no user | ||
op.execute(""" UPDATE taskjob SET owner_id = 1 WHERE owner_id NOT IN (SELECT id FROM identities); """) | ||
|
||
# Delete logs of tasks that reference a study that has been deleted | ||
op.execute( | ||
""" | ||
DELETE FROM taskjoblog tjl | ||
WHERE | ||
tjl.task_id IN ( | ||
SELECT | ||
t.id | ||
FROM | ||
taskjob t | ||
WHERE | ||
t.ref_id IS NOT NULL | ||
AND t.ref_id NOT IN (SELECT s.id FROM study s)); | ||
""" | ||
) | ||
|
||
# Delete tasks that reference a study that has been deleted (long query) | ||
op.execute( | ||
""" | ||
DELETE FROM taskjob t | ||
WHERE | ||
t.ref_id IS NOT NULL | ||
AND t.ref_id NOT IN (SELECT s.id FROM study s); | ||
""" | ||
) | ||
|
||
# Delete logs of tasks whose task_id is NULL | ||
op.execute(""" DELETE FROM taskjoblog WHERE task_id IS NULL; """) | ||
|
||
# Set the status "CANCELLED" to tasks whose status is not in the list of possible values | ||
op.execute(""" UPDATE taskjob SET status = 6 WHERE status NOT IN (1, 2, 3, 4, 5, 6); """) | ||
|
||
# Set the type "VARIANT_GENERATION" to tasks whose type is NULL | ||
op.execute(""" UPDATE taskjob SET type = 'VARIANT_GENERATION' WHERE type IS NULL AND name LIKE '%Generation%'; """) | ||
|
||
# Set the type "EXPORT" to tasks whose type is NULL | ||
op.execute(""" UPDATE taskjob SET type = 'EXPORT' WHERE type IS NULL AND name LIKE '%export%'; """) | ||
|
||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("taskjoblog", schema=None) as batch_op: | ||
batch_op.alter_column("task_id", existing_type=sa.VARCHAR(), nullable=False) | ||
batch_op.drop_constraint("fk_log_taskjob_id", type_="foreignkey") | ||
batch_op.create_foreign_key("fk_log_taskjob_id", "taskjob", ["task_id"], ["id"], ondelete="CASCADE") | ||
|
||
with op.batch_alter_table('taskjob', schema=None) as batch_op: | ||
batch_op.alter_column('name', existing_type=sa.VARCHAR(), nullable=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_creation_date'), ['creation_date'], unique=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_name'), ['name'], unique=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_owner_id'), ['owner_id'], unique=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_ref_id'), ['ref_id'], unique=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_status'), ['status'], unique=False) | ||
batch_op.create_index(batch_op.f('ix_taskjob_type'), ['type'], unique=False) | ||
batch_op.create_foreign_key('fk_taskjob_identity_id', 'identities', ['owner_id'], ['id'], ondelete='SET NULL') | ||
batch_op.create_foreign_key('fk_taskjob_study_id', 'study', ['ref_id'], ['id'], ondelete='CASCADE') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('taskjob', schema=None) as batch_op: | ||
batch_op.drop_constraint('fk_taskjob_study_id', type_='foreignkey') | ||
batch_op.drop_constraint('fk_taskjob_identity_id', type_='foreignkey') | ||
batch_op.drop_index(batch_op.f('ix_taskjob_type')) | ||
batch_op.drop_index(batch_op.f('ix_taskjob_status')) | ||
batch_op.drop_index(batch_op.f('ix_taskjob_ref_id')) | ||
batch_op.drop_index(batch_op.f('ix_taskjob_owner_id')) | ||
batch_op.drop_index(batch_op.f('ix_taskjob_name')) | ||
batch_op.drop_index(batch_op.f('ix_taskjob_creation_date')) | ||
batch_op.alter_column('name', existing_type=sa.VARCHAR(), nullable=True) | ||
|
||
with op.batch_alter_table("taskjoblog", schema=None) as batch_op: | ||
batch_op.drop_constraint("fk_log_taskjob_id", type_="foreignkey") | ||
batch_op.create_foreign_key("fk_log_taskjob_id", "taskjob", ["task_id"], ["id"]) | ||
batch_op.alter_column("task_id", existing_type=sa.VARCHAR(), nullable=True) | ||
|
||
# ### end Alembic commands ### |
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.