"""v0.4.0.

This here was in fact run on 2023-04-18, replacing all
historical migrations up to this point.

PR: https://github.com/laminlabs/laminhub-rest/pull/175

Revision ID: 8d91d067cc7d
Create Date: 2023-04-18 17:59:29.112311

"""
import sqlalchemy as sa  # noqa
import sqlmodel  # noqa
from alembic import op

# revision identifiers, used by Alembic.
revision = "8d91d067cc7d"
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "account",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=True),
        sa.Column("lnid", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("handle", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("bio", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("website", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("github_handle", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("twitter_handle", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("linkedin_handle", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("avatar_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"], ["auth.users.id"], name=op.f("fk_account_user_id_users")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_account")),
    )
    op.create_index(
        op.f("ix_account_created_at"), "account", ["created_at"], unique=False
    )
    op.create_index(op.f("ix_account_handle"), "account", ["handle"], unique=True)
    op.create_index(op.f("ix_account_lnid"), "account", ["lnid"], unique=True)
    op.create_index(op.f("ix_account_name"), "account", ["name"], unique=False)
    op.create_index(
        op.f("ix_account_updated_at"), "account", ["updated_at"], unique=False
    )
    op.create_index(op.f("ix_account_user_id"), "account", ["user_id"], unique=False)
    op.create_table(
        "organization",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.ForeignKeyConstraint(
            ["id"], ["account.id"], name=op.f("fk_organization_id_account")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_organization")),
    )
    op.create_table(
        "storage",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("account_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("root", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("type", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("region", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"], ["account.id"], name=op.f("fk_storage_account_id_account")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_storage")),
    )
    op.create_index(
        op.f("ix_storage_account_id"), "storage", ["account_id"], unique=False
    )
    op.create_index(
        op.f("ix_storage_created_at"), "storage", ["created_at"], unique=False
    )
    op.create_index(op.f("ix_storage_root"), "storage", ["root"], unique=True)
    op.create_index(
        op.f("ix_storage_updated_at"), "storage", ["updated_at"], unique=False
    )
    op.create_table(
        "version_cbwk",
        sa.Column("v", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("migration", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["user_id"], ["account.lnid"], name=op.f("fk_version_cbwk_user_id_account")
        ),
        sa.PrimaryKeyConstraint("v", name=op.f("pk_version_cbwk")),
    )
    op.create_index(
        op.f("ix_version_cbwk_created_at"), "version_cbwk", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_version_cbwk_user_id"), "version_cbwk", ["user_id"], unique=False
    )
    op.create_table(
        "instance",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("account_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("storage_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("db", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("schema_str", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("public", sa.Boolean(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"], ["account.id"], name=op.f("fk_instance_account_id_account")
        ),
        sa.ForeignKeyConstraint(
            ["storage_id"], ["storage.id"], name=op.f("fk_instance_storage_id_storage")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_instance")),
        sa.UniqueConstraint("account_id", "name", name=op.f("uq_instance_account_id")),
        sa.UniqueConstraint("db", name=op.f("uq_instance_db")),
    )
    op.create_index(
        op.f("ix_instance_account_id"), "instance", ["account_id"], unique=False
    )
    op.create_index(
        op.f("ix_instance_created_at"), "instance", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_instance_storage_id"), "instance", ["storage_id"], unique=False
    )
    op.create_index(
        op.f("ix_instance_updated_at"), "instance", ["updated_at"], unique=False
    )
    op.create_table(
        "account_instance",
        sa.Column("account_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("instance_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("permission", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["account.id"],
            name=op.f("fk_account_instance_account_id_account"),
        ),
        sa.ForeignKeyConstraint(
            ["instance_id"],
            ["instance.id"],
            name=op.f("fk_account_instance_instance_id_instance"),
        ),
        sa.PrimaryKeyConstraint(
            "account_id", "instance_id", name=op.f("pk_account_instance")
        ),
    )
    op.create_index(
        op.f("ix_account_instance_account_id"),
        "account_instance",
        ["account_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_created_at"),
        "account_instance",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_instance_id"),
        "account_instance",
        ["instance_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_updated_at"),
        "account_instance",
        ["updated_at"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_account_instance_updated_at"), table_name="account_instance")
    op.drop_index(
        op.f("ix_account_instance_instance_id"), table_name="account_instance"
    )
    op.drop_index(op.f("ix_account_instance_created_at"), table_name="account_instance")
    op.drop_index(op.f("ix_account_instance_account_id"), table_name="account_instance")
    op.drop_table("account_instance")
    op.drop_index(op.f("ix_instance_updated_at"), table_name="instance")
    op.drop_index(op.f("ix_instance_storage_id"), table_name="instance")
    op.drop_index(op.f("ix_instance_created_at"), table_name="instance")
    op.drop_index(op.f("ix_instance_account_id"), table_name="instance")
    op.drop_table("instance")
    op.drop_index(op.f("ix_version_cbwk_user_id"), table_name="version_cbwk")
    op.drop_index(op.f("ix_version_cbwk_created_at"), table_name="version_cbwk")
    op.drop_table("version_cbwk")
    op.drop_index(op.f("ix_storage_updated_at"), table_name="storage")
    op.drop_index(op.f("ix_storage_root"), table_name="storage")
    op.drop_index(op.f("ix_storage_created_at"), table_name="storage")
    op.drop_index(op.f("ix_storage_account_id"), table_name="storage")
    op.drop_table("storage")
    op.drop_table("organization")
    op.drop_index(op.f("ix_account_user_id"), table_name="account")
    op.drop_index(op.f("ix_account_updated_at"), table_name="account")
    op.drop_index(op.f("ix_account_name"), table_name="account")
    op.drop_index(op.f("ix_account_lnid"), table_name="account")
    op.drop_index(op.f("ix_account_handle"), table_name="account")
    op.drop_index(op.f("ix_account_created_at"), table_name="account")
    op.drop_table("account")
    # ### end Alembic commands ###
