From 36fb3259b9b98f7811fd236df45d916aea10f5d3 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Thu, 16 May 2024 20:43:03 +1200 Subject: [PATCH] poll database migration #181 --- app/models.py | 22 ++++++++ migrations/versions/92bdb39f6c72_polls.py | 68 +++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 migrations/versions/92bdb39f6c72_polls.py diff --git a/app/models.py b/app/models.py index c21f849f..29e63ba7 100644 --- a/app/models.py +++ b/app/models.py @@ -1369,6 +1369,28 @@ class NotificationSubscription(db.Model): created_at = db.Column(db.DateTime, default=utcnow) # Perhaps very old subscriptions can be automatically deleted +class Poll(db.Model): + post_id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True) + end_poll = db.Column(db.DateTime) + local_only = db.Column(db.Boolean) + latest_vote = db.Column(db.DateTime) + + +class PollChoice(db.Model): + id = db.Column(db.Integer, primary_key=True) + post_id = db.Column(db.Integer, db.ForeignKey('post.id'), index=True) + choice_text = db.Column(db.String(200)) + sort_order = db.Column(db.Integer) + num_votes = db.Column(db.Integer, default=0) + + +class PollChoiceVote(db.Model): + choice_id = db.Column(db.Integer, db.ForeignKey('poll_choice.id'), primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True) + post_id = db.Column(db.Integer, db.ForeignKey('post.id'), index=True) + created_at = db.Column(db.DateTime, default=utcnow) + + class IpBan(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(50), index=True) diff --git a/migrations/versions/92bdb39f6c72_polls.py b/migrations/versions/92bdb39f6c72_polls.py new file mode 100644 index 00000000..7cfd764c --- /dev/null +++ b/migrations/versions/92bdb39f6c72_polls.py @@ -0,0 +1,68 @@ +"""polls + +Revision ID: 92bdb39f6c72 +Revises: 9752fb47d7a6 +Create Date: 2024-05-16 20:42:05.491951 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '92bdb39f6c72' +down_revision = '9752fb47d7a6' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('poll', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('end_poll', sa.DateTime(), nullable=True), + sa.Column('local_only', sa.Boolean(), nullable=True), + sa.Column('latest_vote', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], ), + sa.PrimaryKeyConstraint('post_id') + ) + op.create_table('poll_choice', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('post_id', sa.Integer(), nullable=True), + sa.Column('choice_text', sa.String(length=200), nullable=True), + sa.Column('sort_order', sa.Integer(), nullable=True), + sa.Column('num_votes', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], ), + sa.PrimaryKeyConstraint('id') + ) + with op.batch_alter_table('poll_choice', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_poll_choice_post_id'), ['post_id'], unique=False) + + op.create_table('poll_choice_vote', + sa.Column('choice_id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('post_id', sa.Integer(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['choice_id'], ['poll_choice.id'], ), + sa.ForeignKeyConstraint(['post_id'], ['post.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('choice_id', 'user_id') + ) + with op.batch_alter_table('poll_choice_vote', schema=None) as batch_op: + batch_op.create_index(batch_op.f('ix_poll_choice_vote_post_id'), ['post_id'], unique=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('poll_choice_vote', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_poll_choice_vote_post_id')) + + op.drop_table('poll_choice_vote') + with op.batch_alter_table('poll_choice', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_poll_choice_post_id')) + + op.drop_table('poll_choice') + op.drop_table('poll') + # ### end Alembic commands ###