def upgrade()

in src/dispatch/database/revisions/tenant/versions/2021-11-18_ce5c4ac967d8.py [0:0]


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("incident", Column("total_cost", Numeric(), nullable=True))
    op.add_column("incident", Column("participants_team", String(), nullable=True))
    op.add_column("incident", Column("participants_location", String(), nullable=True))
    op.add_column("incident", Column("commanders_location", String(), nullable=True))
    op.add_column("incident", Column("reporters_location", String(), nullable=True))
    op.add_column("incident", Column("commander_id", Integer(), nullable=True))
    op.add_column("incident", Column("reporter_id", Integer(), nullable=True))
    op.add_column("incident", Column("liaison_id", Integer(), nullable=True))
    op.add_column("incident", Column("scribe_id", Integer(), nullable=True))
    op.add_column("incident", Column("incident_document_id", Integer(), nullable=True))
    op.add_column("incident", Column("incident_review_document_id", Integer(), nullable=True))
    op.add_column("incident", Column("tactical_group_id", Integer(), nullable=True))
    op.add_column("incident", Column("notifications_group_id", Integer(), nullable=True))
    op.create_foreign_key(None, "incident", "document", ["incident_document_id"], ["id"])
    op.create_foreign_key(None, "incident", "participant", ["reporter_id"], ["id"])
    op.create_foreign_key(None, "incident", "participant", ["scribe_id"], ["id"])
    op.create_foreign_key(None, "incident", "participant", ["commander_id"], ["id"])
    op.create_foreign_key(None, "incident", "participant", ["liaison_id"], ["id"])
    op.create_foreign_key(None, "incident", "document", ["incident_review_document_id"], ["id"])
    op.create_foreign_key(None, "incident", "group", ["tactical_group_id"], ["id"])
    op.create_foreign_key(None, "incident", "group", ["notifications_group_id"], ["id"])

    print("Starting data migration...")

    bind = op.get_bind()
    session = Session(bind=bind)

    incidents = session.query(Incident).all()

    for incident in incidents:
        # we set the total cost
        cost = 0
        for c in incident.incident_costs:
            cost += c.amount
        incident.total_cost = cost

        # we set the participants team, and participants, commanders, and reporters location
        incident.participants_team = Counter(p.team for p in incident.participants).most_common(1)[
            0
        ][0]
        incident.participants_location = Counter(
            p.location for p in incident.participants
        ).most_common(1)[0][0]

        commander = get_current_participant(
            incident.participants, ParticipantRoleType.incident_commander
        )
        if commander:
            incident.commander_id = commander.id
            incident.commanders_location = commander.location

        reporter = get_current_participant(incident.participants, ParticipantRoleType.reporter)
        if reporter:
            incident.reporter_id = reporter.id
            incident.reporters_location = reporter.location

        liaison = get_current_participant(incident.participants, ParticipantRoleType.liaison)
        if liaison:
            incident.liaison_id = liaison.id

        scribe = get_current_participant(incident.participants, ParticipantRoleType.scribe)
        if scribe:
            incident.scribe_id = scribe.id

        # we set the incident document and post-incident review document foreign keys
        incident_document = get_current_document(incident.documents, DocumentResourceTypes.incident)
        if incident_document:
            incident.incident_document_id = incident_document.id

        incident_review_document = get_current_document(
            incident.documents, DocumentResourceTypes.review
        )
        if incident_review_document:
            incident.incident_review_document_id = incident_review_document.id

        # we set the tactical and notifications foreign keys
        tactical_group = get_current_group(incident.groups, "tactical-group")
        if tactical_group:
            incident.tactical_group_id = tactical_group.id

        notifications_group = get_current_group(incident.groups, "notifications-group")
        if notifications_group:
            incident.notifications_group_id = notifications_group.id

    session.commit()

    print("Data migration completed.")