"""cleanup_orphan_views

Revision ID: 0246
Revises: 0245
Create Date: 2022-10-13 17:40:33.206604

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


# revision identifiers, used by Alembic.
revision = '0246'
down_revision = '0245'
branch_labels = None
depends_on = None


def upgrade():
    op.execute("""
        DELETE FROM views WHERE project_id IS NOT NULL
        AND card_ref NOT IN (SELECT ref FROM cards WHERE cards.project_id = views.project_id AND (parent_id IS NULL OR board_created))
    """)


def downgrade():
    pass
