"""relocate vote decisions into TUVote Revision ID: 84cd90073dde Revises: d64e5571bc8d Create Date: 2022-03-07 18:02:28.791103 This migration allows us to give Trusted Users the ability to modify a vote they made on a proposal. Previously, the decision was tracked purely through TU_VoteInfo records, which removes tracking of what decisions users made. This blocks us from being able to modify votes, because we can't update the TU_VoteInfo Yes, No or Abstain columns properly. """ import sqlalchemy as sa from alembic import op from sqlalchemy.dialects.mysql import TINYINT from aurweb import db, logging, time from aurweb.models import TUVoteInfo logger = logging.get_logger("alembic") # revision identifiers, used by Alembic. revision = '84cd90073dde' down_revision = 'd64e5571bc8d' branch_labels = None depends_on = None TABLE = "TU_Votes" def upgrade(): decision = sa.Column("Decision", TINYINT(unsigned=True)) op.add_column(TABLE, decision) # For each proposal which is running at the time this migration # is applied, eradicate all related votes. We will restart the votes. # In addition, reset the Submitted and End columns based on the current # timestamp; essentially resetting the proposal's state. utcnow = time.utcnow() running_proposals = db.query(TUVoteInfo).filter(TUVoteInfo.End > utcnow) with db.begin(): for proposal in running_proposals: logger.info(f"Resetting proposal with ID {proposal.ID}: " "Yes = 0, No = 0, Abstain = 0, deleted vote records") # Reset the Submitted and End columns. length = proposal.End - proposal.Submitted proposal.Submitted = utcnow proposal.End = utcnow + length proposal.Yes = proposal.No = proposal.Abstain = 0 db.delete_all(proposal.tu_votes) logger.info(f"Proposal time range was reset: Submitted = " f"{proposal.Submitted} and End = {proposal.End}") def downgrade(): op.drop_column(TABLE, "Decision")