"""Use TEXT everywhere

Revision ID: 3214530ea234
Revises: dfc8cd8487fd
Create Date: 2021-05-30 15:55:11.441482

"""

from collections.abc import Sequence

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

# revision identifiers, used by Alembic.
revision: str = "3214530ea234"
down_revision: str | None = "dfc8cd8487fd"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column("discounts", "currencies", existing_type=sa.VARCHAR(length=10000), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "discounts",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('discounts_id_seq'::regclass)"),
    )
    op.alter_column("discounts", "name", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("discounts", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("discountsxproducts", "discount_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("discountsxproducts", "product_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("invoices", "buyer_email", existing_type=sa.VARCHAR(length=10000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("invoices", "discount", existing_type=sa.INTEGER(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "invoices",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('invoices_id_seq'::regclass)"),
    )
    op.alter_column(
        "invoices", "paid_currency", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True
    )
    op.alter_column("invoices", "status", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=False)
    op.alter_column("invoices", "store_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("invoices", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.drop_constraint("invoices_store_id_stores_fkey", "invoices", type_="foreignkey")
    op.create_foreign_key(
        op.f("invoices_store_id_stores_fkey"),
        "invoices",
        "stores",
        ["store_id"],
        ["id"],
        ondelete="SET NULL",
        initially="DEFERRED",
        deferrable=True,
    )
    op.alter_column(
        "notifications",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('notifications_id_seq'::regclass)"),
    )
    op.alter_column("notifications", "name", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "notifications", "provider", existing_type=sa.VARCHAR(length=10000), type_=sa.Text(), existing_nullable=True
    )
    op.alter_column("notifications", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "notificationsxstores", "notification_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True
    )
    op.alter_column("notificationsxstores", "store_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "paymentmethods", "currency", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True
    )
    op.alter_column(
        "paymentmethods",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('paymentmethods_id_seq'::regclass)"),
    )
    op.alter_column("paymentmethods", "invoice_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("paymentmethods", "discount", existing_type=sa.INTEGER(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "products", "download_url", existing_type=sa.VARCHAR(length=100000), type_=sa.Text(), existing_nullable=True
    )
    op.alter_column(
        "products",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('products_id_seq'::regclass)"),
    )
    op.alter_column("products", "image", existing_type=sa.VARCHAR(length=100000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("products", "name", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("products", "status", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=False)
    op.alter_column("products", "store_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("products", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.drop_constraint("products_store_id_stores_fkey", "products", type_="foreignkey")
    op.create_foreign_key(
        op.f("products_store_id_stores_fkey"),
        "products",
        "stores",
        ["store_id"],
        ["id"],
        ondelete="SET NULL",
        initially="DEFERRED",
        deferrable=True,
    )
    op.alter_column("productsxinvoices", "invoice_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("productsxinvoices", "product_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "settings",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('settings_id_seq'::regclass)"),
    )
    op.alter_column("stores", "email", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("stores", "email_host", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("stores", "email_password", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("stores", "email_user", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "stores",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('stores_id_seq'::regclass)"),
    )
    op.alter_column("stores", "name", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("stores", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "templates",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('templates_id_seq'::regclass)"),
    )
    op.alter_column("templates", "name", existing_type=sa.VARCHAR(length=100000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("templates", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("tokens", "app_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("tokens", "id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=False)
    op.alter_column(
        "tokens",
        "permissions",
        existing_type=postgresql.ARRAY(sa.VARCHAR()),
        type_=postgresql.ARRAY(sa.Text()),
        existing_nullable=True,
    )
    op.alter_column("tokens", "redirect_url", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("tokens", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("users", "email", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("users", "hashed_password", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "users",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('users_id_seq'::regclass)"),
    )
    op.alter_column("wallets", "currency", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column(
        "wallets",
        "id",
        existing_type=sa.VARCHAR(),
        type_=sa.Text(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('wallets_id_seq'::regclass)"),
    )
    op.alter_column("wallets", "name", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("wallets", "user_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("wallets", "xpub", existing_type=sa.VARCHAR(length=1000), type_=sa.Text(), existing_nullable=True)
    op.alter_column("walletsxstores", "store_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.alter_column("walletsxstores", "wallet_id", existing_type=sa.VARCHAR(), type_=sa.Text(), existing_nullable=True)
    op.add_column(
        "paymentmethods",
        sa.Column(
            "created",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.func.clock_timestamp(),  # to differ in context of current transaction
        ),
    )
    # To not alter current payment methods order
    op.execute("update paymentmethods set created=clock_timestamp() - interval '1 second' * (1e7-id::integer)")
    op.alter_column("paymentmethods", "created", server_default=None)
    # ### end Alembic commands ###


def int_alter_column(table, column, **kwargs):
    op.alter_column(table, column, postgresql_using=f"{column}::integer", **kwargs)


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column("walletsxstores", "wallet_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("walletsxstores", "store_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("wallets", "xpub", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column("wallets", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("wallets", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "wallets",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('wallets_id_seq'::regclass)"),
    )
    op.alter_column("wallets", "currency", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "users",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('users_id_seq'::regclass)"),
    )
    op.alter_column("users", "hashed_password", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("users", "email", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("tokens", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("tokens", "redirect_url", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column(
        "tokens",
        "permissions",
        existing_type=postgresql.ARRAY(sa.Text()),
        type_=postgresql.ARRAY(sa.VARCHAR()),
        existing_nullable=True,
    )
    op.alter_column("tokens", "id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=False)
    op.alter_column("tokens", "app_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("templates", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("templates", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=100000), existing_nullable=True)
    op.alter_column(
        "templates",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('templates_id_seq'::regclass)"),
    )
    op.alter_column("stores", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("stores", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "stores",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('stores_id_seq'::regclass)"),
    )
    op.alter_column("stores", "email_user", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column("stores", "email_password", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column("stores", "email_host", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column("stores", "email", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "settings",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('settings_id_seq'::regclass)"),
    )
    op.alter_column("productsxinvoices", "product_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("productsxinvoices", "invoice_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.drop_constraint(op.f("products_store_id_stores_fkey"), "products", type_="foreignkey")
    op.create_foreign_key("products_store_id_stores_fkey", "products", "stores", ["store_id"], ["id"], ondelete="SET NULL")
    op.alter_column("products", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("products", "store_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("products", "status", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=False)
    op.alter_column("products", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column("products", "image", existing_type=sa.Text(), type_=sa.VARCHAR(length=100000), existing_nullable=True)
    op.alter_column(
        "products",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('products_id_seq'::regclass)"),
    )
    op.alter_column(
        "products", "download_url", existing_type=sa.Text(), type_=sa.VARCHAR(length=100000), existing_nullable=True
    )
    op.alter_column("paymentmethods", "invoice_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column(
        "paymentmethods",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('paymentmethods_id_seq'::regclass)"),
    )
    op.alter_column(
        "paymentmethods", "currency", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True
    )
    int_alter_column("paymentmethods", "discount", existing_type=sa.Text(), type_=sa.INTEGER(), existing_nullable=True)
    op.alter_column("notificationsxstores", "store_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column(
        "notificationsxstores", "notification_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True
    )
    op.alter_column("notifications", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column(
        "notifications", "provider", existing_type=sa.Text(), type_=sa.VARCHAR(length=10000), existing_nullable=True
    )
    op.alter_column("notifications", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "notifications",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('notifications_id_seq'::regclass)"),
    )
    op.drop_constraint(op.f("invoices_store_id_stores_fkey"), "invoices", type_="foreignkey")
    op.create_foreign_key("invoices_store_id_stores_fkey", "invoices", "stores", ["store_id"], ["id"], ondelete="SET NULL")
    op.alter_column("invoices", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("invoices", "store_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("invoices", "status", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=False)
    op.alter_column(
        "invoices", "paid_currency", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True
    )
    op.alter_column(
        "invoices",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('invoices_id_seq'::regclass)"),
    )
    int_alter_column("invoices", "discount", existing_type=sa.Text(), type_=sa.INTEGER(), existing_nullable=True)
    op.alter_column("invoices", "buyer_email", existing_type=sa.Text(), type_=sa.VARCHAR(length=10000), existing_nullable=True)
    op.alter_column("discountsxproducts", "product_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("discountsxproducts", "discount_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("discounts", "user_id", existing_type=sa.Text(), type_=sa.VARCHAR(), existing_nullable=True)
    op.alter_column("discounts", "name", existing_type=sa.Text(), type_=sa.VARCHAR(length=1000), existing_nullable=True)
    op.alter_column(
        "discounts",
        "id",
        existing_type=sa.Text(),
        type_=sa.VARCHAR(),
        existing_nullable=False,
        existing_server_default=sa.text("nextval('discounts_id_seq'::regclass)"),
    )
    op.alter_column("discounts", "currencies", existing_type=sa.Text(), type_=sa.VARCHAR(length=10000), existing_nullable=True)
    op.drop_column("paymentmethods", "created")
    # ### end Alembic commands ###
