fossasia/open-event-orga-server

View on GitHub
migrations/versions/rev-2021-04-10-19:57:27-a02007782ba2_restructure_notification_model.py

Summary

Maintainability
C
1 day
Test Coverage
"""restructure notification model

Revision ID: a02007782ba2
Revises: 28de2911c790
Create Date: 2021-04-10 19:57:27.679882

"""

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'a02007782ba2'
down_revision = '28de2911c790'


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('delete from notifications')
    op.create_table(
        'notification_content',
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('modified_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('type', sa.String(), nullable=False),
        sa.Column('target_type', sa.Unicode(length=255), nullable=True),
        sa.Column('target_id', sa.Integer(), nullable=True),
        sa.Column('target_action', sa.String(), nullable=True),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_table(
        'notification_actors',
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('modified_at', sa.DateTime(timezone=True), nullable=True),
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('actor_id', sa.Integer(), nullable=False),
        sa.Column('content_id', sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(['actor_id'], ['users.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(
            ['content_id'], ['notification_content.id'], ondelete='CASCADE'
        ),
        sa.PrimaryKeyConstraint('id'),
    )
    op.add_column('notifications', sa.Column('content_id', sa.Integer(), nullable=False))
    op.add_column(
        'notifications',
        sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
    )
    op.add_column(
        'notifications',
        sa.Column('modified_at', sa.DateTime(timezone=True), nullable=True),
    )
    op.alter_column(
        'notifications',
        'is_read',
        existing_type=sa.BOOLEAN(),
        nullable=False,
        server_default='False',
    )
    op.alter_column(
        'notifications', 'user_id', existing_type=sa.INTEGER(), nullable=False
    )
    op.create_foreign_key(
        'notification_content_id_fkey',
        'notifications',
        'notification_content',
        ['content_id'],
        ['id'],
        ondelete='CASCADE',
    )
    op.drop_column('notifications', 'received_at')
    op.drop_column('notifications', 'deleted_at')
    op.drop_column('notifications', 'message')
    op.drop_column('notifications', 'title')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        'notifications',
        sa.Column('title', sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        'notifications',
        sa.Column('message', sa.TEXT(), autoincrement=False, nullable=True),
    )
    op.add_column(
        'notifications',
        sa.Column(
            'deleted_at',
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
    )
    op.add_column(
        'notifications',
        sa.Column(
            'received_at',
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=True,
        ),
    )
    op.drop_constraint(
        'notification_content_id_fkey', 'notifications', type_='foreignkey'
    )
    op.alter_column('notifications', 'user_id', existing_type=sa.INTEGER(), nullable=True)
    op.alter_column('notifications', 'is_read', existing_type=sa.BOOLEAN(), nullable=True)
    op.drop_column('notifications', 'modified_at')
    op.drop_column('notifications', 'created_at')
    op.drop_column('notifications', 'content_id')
    op.drop_table('notification_actors')
    op.drop_table('notification_content')
    # ### end Alembic commands ###