Нет описания

875edc4adb40_migrate_task_assignee.py 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Modifying case tasks to remove assignee id for instead, adding a table named task_assignee
  2. Revision ID: 875edc4adb40
  3. Revises: fcc375ed37d1
  4. Create Date: 2022-07-17 14:57:22.809977
  5. """
  6. from alembic import op
  7. from sqlalchemy import text
  8. from app.alembic.alembic_utils import _has_table
  9. from app.alembic.alembic_utils import _table_has_column
  10. # revision identifiers, used by Alembic.
  11. revision = '875edc4adb40'
  12. down_revision = 'fcc375ed37d1'
  13. branch_labels = None
  14. depends_on = None
  15. def upgrade():
  16. conn = op.get_bind()
  17. # Get all users with their roles
  18. if _has_table("case_tasks"):
  19. if _table_has_column("case_tasks", "task_assignee_id"):
  20. res = conn.execute(text("select id, task_assignee_id from case_tasks"))
  21. results_tasks = res.fetchall()
  22. for task in results_tasks:
  23. task_id = task[0]
  24. user_id = task[1]
  25. if not user_id:
  26. user_id = 1
  27. # Migrate assignees to task_assignee
  28. conn.execute(text(f"insert into task_assignee (user_id, task_id) values ({user_id}, {task_id}) "
  29. f"on conflict do nothing;"))
  30. op.drop_column(
  31. table_name='case_tasks',
  32. column_name='task_assignee_id'
  33. )
  34. def downgrade():
  35. pass