"""empty message

Revision ID: 6b7016f73688
Revises: a75bd9c8ad3a
Create Date: 2020-10-01 11:09:16.765736

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

from project import dbtypes

# revision identifiers, used by Alembic.
revision = "6b7016f73688"
down_revision = "a75bd9c8ad3a"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint("event_place_id_fkey", "event", type_="foreignkey")
    op.drop_constraint("event_host_id_fkey", "event", type_="foreignkey")
    op.drop_column("event", "host_id")
    op.drop_column("event", "place_id")
    op.drop_table("eventsuggestiondate")
    op.drop_table("place")
    op.drop_table("org_or_adminunit")
    op.drop_table("actor")
    op.drop_table("adminunitorgroles_organizations")
    op.drop_table("adminunitorgrole")
    op.drop_table("adminunitorg")
    op.drop_table("orgmemberroles_members")
    op.drop_table("orgmember")
    op.drop_table("organization")
    op.drop_table("eventsuggestion")
    op.drop_table("orgmemberrole")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "event", sa.Column("place_id", sa.INTEGER(), autoincrement=False, nullable=True)
    )
    op.add_column(
        "event", sa.Column("host_id", sa.INTEGER(), autoincrement=False, nullable=True)
    )
    op.create_foreign_key(
        "event_host_id_fkey", "event", "org_or_adminunit", ["host_id"], ["id"]
    )
    op.create_foreign_key("event_place_id_fkey", "event", "place", ["place_id"], ["id"])
    op.create_table(
        "orgmemberroles_members",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("member_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("role_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["member_id"],
            ["orgmember.id"],
            name="orgmemberroles_members_member_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["role_id"],
            ["orgmemberrole.id"],
            name="orgmemberroles_members_role_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="orgmemberroles_members_pkey"),
    )
    op.create_table(
        "orgmemberrole",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("name", sa.VARCHAR(length=80), autoincrement=False, nullable=True),
        sa.Column(
            "description", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column("permissions", sa.TEXT(), autoincrement=False, nullable=True),
        sa.PrimaryKeyConstraint("id", name="orgmemberrole_pkey"),
        sa.UniqueConstraint("name", name="orgmemberrole_name_key"),
    )
    op.create_table(
        "eventsuggestion",
        sa.Column(
            "created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
        sa.Column(
            "id",
            sa.INTEGER(),
            server_default=sa.text("nextval('eventsuggestion_id_seq'::regclass)"),
            autoincrement=True,
            nullable=False,
        ),
        sa.Column("admin_unit_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column(
            "host_name", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "event_name", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column("description", sa.TEXT(), autoincrement=False, nullable=False),
        sa.Column(
            "place_name", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "place_street", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column(
            "place_postalCode",
            sa.VARCHAR(length=255),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "place_city", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "contact_name", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "contact_email", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "external_link", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column("created_by_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["admin_unit_id"],
            ["adminunit.id"],
            name="eventsuggestion_admin_unit_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["created_by_id"], ["user.id"], name="eventsuggestion_created_by_id_fkey"
        ),
        sa.PrimaryKeyConstraint("id", name="eventsuggestion_pkey"),
        postgresql_ignore_search_path=False,
    )
    op.create_table(
        "orgmember",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("organization_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
            name="orgmember_organization_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"], ["user.id"], name="orgmember_user_id_fkey"
        ),
        sa.PrimaryKeyConstraint("id", name="orgmember_pkey"),
    )
    op.create_table(
        "adminunitorg",
        sa.Column(
            "id",
            sa.INTEGER(),
            server_default=sa.text("nextval('adminunitorg_id_seq'::regclass)"),
            autoincrement=True,
            nullable=False,
        ),
        sa.Column("admin_unit_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column("organization_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["admin_unit_id"], ["adminunit.id"], name="adminunitorg_admin_unit_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
            name="adminunitorg_organization_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="adminunitorg_pkey"),
        postgresql_ignore_search_path=False,
    )
    op.create_table(
        "adminunitorgroles_organizations",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column(
            "admin_unit_org_id", sa.INTEGER(), autoincrement=False, nullable=True
        ),
        sa.Column("role_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["admin_unit_org_id"],
            ["adminunitorg.id"],
            name="adminunitorgroles_organizations_admin_unit_org_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["role_id"],
            ["adminunitorgrole.id"],
            name="adminunitorgroles_organizations_role_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="adminunitorgroles_organizations_pkey"),
    )
    op.create_table(
        "organization",
        sa.Column(
            "created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
        sa.Column(
            "id",
            sa.INTEGER(),
            server_default=sa.text("nextval('organization_id_seq'::regclass)"),
            autoincrement=True,
            nullable=False,
        ),
        sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column(
            "legal_name", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column("location_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("logo_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("url", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("created_by_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("email", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("phone", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("fax", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column(
            "short_name", sa.VARCHAR(length=100), autoincrement=False, nullable=True
        ),
        sa.ForeignKeyConstraint(
            ["created_by_id"], ["user.id"], name="organization_created_by_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["location_id"], ["location.id"], name="organization_location_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["logo_id"], ["image.id"], name="organization_logo_id_fkey"
        ),
        sa.PrimaryKeyConstraint("id", name="organization_pkey"),
        sa.UniqueConstraint("name", name="organization_name_key"),
        sa.UniqueConstraint("short_name", name="organization_short_name_key"),
        postgresql_ignore_search_path=False,
    )
    op.create_table(
        "adminunitorgrole",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("name", sa.VARCHAR(length=80), autoincrement=False, nullable=True),
        sa.Column(
            "description", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column("permissions", sa.TEXT(), autoincrement=False, nullable=True),
        sa.PrimaryKeyConstraint("id", name="adminunitorgrole_pkey"),
        sa.UniqueConstraint("name", name="adminunitorgrole_name_key"),
    )
    op.create_table(
        "actor",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column("organization_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("admin_unit_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["admin_unit_id"], ["adminunit.id"], name="actor_admin_unit_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"], ["organization.id"], name="actor_organization_id_fkey"
        ),
        sa.ForeignKeyConstraint(["user_id"], ["user.id"], name="actor_user_id_fkey"),
        sa.PrimaryKeyConstraint("id", name="actor_pkey"),
        sa.UniqueConstraint(
            "user_id",
            "organization_id",
            "admin_unit_id",
            name="actor_user_id_organization_id_admin_unit_id_key",
        ),
    )
    op.create_table(
        "org_or_adminunit",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("organization_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("admin_unit_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.CheckConstraint(
            "NOT ((organization_id IS NULL) AND (admin_unit_id IS NULL))",
            name="org_or_adminunit_check",
        ),
        sa.ForeignKeyConstraint(
            ["admin_unit_id"],
            ["adminunit.id"],
            name="org_or_adminunit_admin_unit_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
            name="org_or_adminunit_organization_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="org_or_adminunit_pkey"),
        sa.UniqueConstraint(
            "organization_id",
            "admin_unit_id",
            name="org_or_adminunit_organization_id_admin_unit_id_key",
        ),
    )
    op.create_table(
        "place",
        sa.Column(
            "created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column("location_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("photo_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column("url", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("description", sa.TEXT(), autoincrement=False, nullable=True),
        sa.Column("created_by_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["created_by_id"], ["user.id"], name="place_created_by_id_fkey"
        ),
        sa.ForeignKeyConstraint(
            ["location_id"], ["location.id"], name="place_location_id_fkey"
        ),
        sa.ForeignKeyConstraint(["photo_id"], ["image.id"], name="place_photo_id_fkey"),
        sa.PrimaryKeyConstraint("id", name="place_pkey"),
    )
    op.create_table(
        "eventsuggestiondate",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column(
            "event_suggestion_id", sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "start",
            postgresql.TIMESTAMP(timezone=True),
            autoincrement=False,
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["event_suggestion_id"],
            ["eventsuggestion.id"],
            name="eventsuggestiondate_event_suggestion_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="eventsuggestiondate_pkey"),
    )
    # ### end Alembic commands ###
