<gh_stars>1-10
"""add members_projects table to manage multiple member in one project

Revision ID: 82e322010fe1
Revises: <PASSWORD>
Create Date: 2021-02-22 10:27:58.415333

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "82e322010fe1"
down_revision = "<PASSWORD>"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "members_projects",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("member_id", sa.Integer(), nullable=True),
        sa.Column("project_id", sa.Integer(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["member_id"],
            ["members.id"],
        ),
        sa.ForeignKeyConstraint(["project_id"], ["projects.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "member_id",
            "project_id",
            name="members_projects_member_id_project_id_uniq_const",
        ),
    )
    op.add_column("projects", sa.Column("created_by_id", sa.Integer(), nullable=False))
    op.add_column(
        "projects", sa.Column("last_updated_by_id", sa.Integer(), nullable=True)
    )
    op.alter_column(
        "projects", "name", existing_type=sa.VARCHAR(length=255), nullable=True
    )
    op.drop_constraint("projects_member_id_fkey", "projects", type_="foreignkey")
    op.create_foreign_key(None, "projects", "members", ["last_updated_by_id"], ["id"])
    op.create_foreign_key(None, "projects", "members", ["created_by_id"], ["id"])
    op.drop_column("projects", "member_id")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "projects",
        sa.Column("member_id", sa.INTEGER(), autoincrement=False, nullable=True),
    )
    op.drop_constraint(None, "projects", type_="foreignkey")
    op.drop_constraint(None, "projects", type_="foreignkey")
    op.create_foreign_key(
        "projects_member_id_fkey",
        "projects",
        "members",
        ["member_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.alter_column(
        "projects", "name", existing_type=sa.VARCHAR(length=255), nullable=False
    )
    op.drop_column("projects", "last_updated_by_id")
    op.drop_column("projects", "created_by_id")
    op.drop_table("members_projects")
    # ### end Alembic commands ###
