migrations/versions/a43b9748ceee_.py (59 lines of code) (raw):
"""empty message
Revision ID: a43b9748ceee
Revises: 0eeaa5aed53b
Create Date: 2019-06-12 12:50:15.809839
"""
from alembic import op
from backend.models.postgis.statuses import TaskStatus
# revision identifiers, used by Alembic.
revision = "a43b9748ceee"
down_revision = "0eeaa5aed53b"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"idx_task_validation_mapper_status_composite",
table_name="task_invalidation_history",
)
op.create_index(
"idx_task_validation_mapper_status_composite",
"task_invalidation_history",
["invalidator_id", "is_closed"],
unique=False,
)
# ### end Alembic commands ###
# Recalculate tasks stats based on the task states in projects
conn = op.get_bind()
projects = conn.execute("select id from projects")
print("Recalculating projects' tasks stats... can take a bit")
for project_id in projects:
_set_project_counters_from_task_states(project_id[0])
def _set_project_counters_from_task_states(project_id: int):
# Obtain and process information of the project's tasks' statuses
conn = op.get_bind()
tasks_statuses = conn.execute(
"SELECT task_status, count(task_status) FROM tasks WHERE project_id={0} GROUP BY task_status"
"".format(project_id)
)
tasks_statuses = [r for r in tasks_statuses]
# Set values to the stats in the database
tasks_mapped = sum([n for s, n in tasks_statuses if s == TaskStatus.MAPPED.value])
tasks_validated = sum(
[n for s, n in tasks_statuses if s == TaskStatus.VALIDATED.value]
)
tasks_bad_imagery = sum(
[n for s, n in tasks_statuses if s == TaskStatus.BADIMAGERY.value]
)
query = (
"UPDATE projects "
+ "SET tasks_mapped ="
+ str(tasks_mapped)
+ ", tasks_validated="
+ str(tasks_validated)
+ ", tasks_bad_imagery="
+ str(tasks_bad_imagery)
+ " where id = "
+ str(project_id)
)
op.execute(query)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"idx_task_validation_mapper_status_composite",
table_name="task_invalidation_history",
)
op.create_index(
"idx_task_validation_mapper_status_composite",
"task_invalidation_history",
["mapper_id", "is_closed"],
unique=False,
)
# ### end Alembic commands ###