"""delete_subscriptions_duplicates

Revision: 0105
Revision ID: 2272df6f8a44
Revises: 191bac60ca68
Create Date: 2017-09-28 16:53:10.570392

"""

# revision identifiers, used by Alembic.
revision = '2272df6f8a44'
down_revision = '191bac60ca68'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.execute("""
        delete from card_subscriptions where id in (
            select id from (
                SELECT id, ROW_NUMBER() OVER(PARTITION BY user_id, card_id ORDER BY card_id, user_id) AS n
                FROM card_subscriptions
            ) dups
            where dups.n > 1
        )
    """)


def downgrade():
    pass
