"""project_last_activity_at

Revision: 0206
Revision ID: 826f4548b733
Revises: 90dacf065be7
Create Date: 2021-03-16 16:01:11.767794

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


# revision identifiers, used by Alembic.
revision = '826f4548b733'
down_revision = '90dacf065be7'
branch_labels = None
depends_on = None


def upgrade():
    op.execute("""
        WITH last_activities AS (
            select project_id, max(created_at) last_activity_at from activities where project_id is not null and created_at is not null group by project_id
        )
        update projects set last_activity_at = last_activities.last_activity_at from last_activities where last_activities.project_id = projects.id;
    """)


def downgrade():
    pass
