"""add location

Revision ID: 782a63915495
Revises: bf6b10f0f3e8
Create Date: 2021-06-23 19:21:54.279419

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

# revision identifiers, used by Alembic.
revision = "782a63915495"
down_revision = "bf6b10f0f3e8"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "location",
        sa.Column("id", postgresql.UUID(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("address", sa.String(), nullable=False),
        sa.Column("organisation_id", postgresql.UUID(), nullable=False),
        sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(["organisation_id"], ["organisation.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_location_created_at"), "location", ["created_at"], unique=False)
    op.create_index(op.f("ix_location_name"), "location", ["name"], unique=False)
    op.add_column("person", sa.Column("location_id", postgresql.UUID(), nullable=True))
    op.create_index(op.f("ix_person_location_id"), "person", ["location_id"], unique=False)
    op.create_foreign_key(None, "person", "location", ["location_id"], ["id"], ondelete="SET NULL")
    op.drop_column("person", "location")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "person",
        sa.Column("location", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.drop_constraint(None, "person", type_="foreignkey")
    op.drop_index(op.f("ix_person_location_id"), table_name="person")
    op.drop_column("person", "location_id")
    op.drop_index(op.f("ix_location_name"), table_name="location")
    op.drop_index(op.f("ix_location_created_at"), table_name="location")
    op.drop_table("location")
    # ### end Alembic commands ###
