"""Initial migration

Revision ID: 26ccc98e38f1
Revises:
Create Date: 2024-06-06 10:59:25.903538

"""

import enum

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy_utils.types.ts_vector import TSVectorType

from polar.enums import Platforms
from polar.kit.extensions.sqlalchemy import StringEnum
from polar.models import Account


# This enum has been removed from the codebase since this migration was created
# Creating it here to avoid breaking the migration
class ReferenceType(str, enum.Enum):
    # external_id is a pull_requests.id UUID
    PULL_REQUEST = "pull_request"

    # external_id is a HREF
    EXTERNAL_GITHUB_PULL_REQUEST = "external_github_pull_request"

    # external_id is a SHA1
    EXTERNAL_GITHUB_COMMIT = "external_github_commit"


# revision identifiers, used by Alembic.
revision = "26ccc98e38f1"
down_revision = None
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute("CREATE EXTENSION IF NOT EXISTS citext")

    op.create_table(
        "accounts",
        sa.Column("account_type", sa.String(length=255), nullable=False),
        sa.Column("admin_id", sa.UUID(), nullable=False),
        sa.Column("stripe_id", sa.String(length=100), nullable=True),
        sa.Column("open_collective_slug", sa.String(length=255), nullable=True),
        sa.Column("email", sa.String(length=254), nullable=True),
        sa.Column("country", sa.String(length=2), nullable=False),
        sa.Column("currency", sa.String(length=3), nullable=True),
        sa.Column("is_details_submitted", sa.Boolean(), nullable=False),
        sa.Column("is_charges_enabled", sa.Boolean(), nullable=False),
        sa.Column("is_payouts_enabled", sa.Boolean(), nullable=False),
        sa.Column("processor_fees_applicable", sa.Boolean(), nullable=False),
        sa.Column("platform_pledge_fee_percent", sa.Integer(), nullable=True),
        sa.Column("platform_subscription_fee_percent", sa.Integer(), nullable=True),
        sa.Column("business_type", sa.String(length=255), nullable=True),
        sa.Column("status", StringEnum(Account.Status), nullable=False),
        sa.Column("next_review_threshold", sa.Integer(), nullable=True),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["admin_id"],
            ["users.id"],
            name=op.f("accounts_admin_id_fkey"),
            use_alter=True,
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("accounts_pkey")),
    )
    op.create_index(
        op.f("ix_accounts_created_at"), "accounts", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_accounts_deleted_at"), "accounts", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_accounts_modified_at"), "accounts", ["modified_at"], unique=False
    )
    op.create_table(
        "organizations",
        sa.Column(
            "platform",
            StringEnum(Platforms),
            nullable=False,
        ),
        sa.Column("name", postgresql.CITEXT(), nullable=False),
        sa.Column("external_id", sa.BigInteger(), nullable=False),
        sa.Column("avatar_url", sa.String(), nullable=False),
        sa.Column("is_personal", sa.Boolean(), nullable=False),
        sa.Column("account_id", sa.UUID(), nullable=True),
        sa.Column("installation_id", sa.Integer(), nullable=True),
        sa.Column(
            "installation_created_at", sa.TIMESTAMP(timezone=True), nullable=True
        ),
        sa.Column(
            "installation_updated_at", sa.TIMESTAMP(timezone=True), nullable=True
        ),
        sa.Column(
            "installation_suspended_at", sa.TIMESTAMP(timezone=True), nullable=True
        ),
        sa.Column("installation_suspended_by", sa.Integer(), nullable=True),
        sa.Column("installation_suspender", sa.UUID(), nullable=True),
        sa.Column(
            "installation_permissions",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column("pledge_badge_show_amount", sa.Boolean(), nullable=False),
        sa.Column("pledge_minimum_amount", sa.Integer(), nullable=False),
        sa.Column("default_badge_custom_content", sa.String(), nullable=True),
        sa.Column("default_upfront_split_to_contributors", sa.Integer(), nullable=True),
        sa.Column("onboarded_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("blocked_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("created_from_user_maintainer_upgrade", sa.Boolean(), nullable=False),
        sa.Column("is_teams_enabled", sa.Boolean(), nullable=False),
        sa.Column("stripe_customer_id", sa.String(length=50), nullable=True),
        sa.Column("billing_email", sa.String(length=120), nullable=True),
        sa.Column("total_monthly_spending_limit", sa.Integer(), nullable=True),
        sa.Column("per_user_monthly_spending_limit", sa.Integer(), nullable=True),
        sa.Column("custom_domain", sa.String(), nullable=True),
        sa.Column(
            "profile_settings", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("donations_enabled", sa.Boolean(), nullable=False),
        sa.Column("public_donation_timestamps", sa.Boolean(), nullable=False),
        sa.Column(
            "feature_settings", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("bio", sa.String(), nullable=True),
        sa.Column("pretty_name", sa.String(), nullable=True),
        sa.Column("company", sa.String(), nullable=True),
        sa.Column("blog", sa.String(), nullable=True),
        sa.Column("location", sa.String(), nullable=True),
        sa.Column("email", sa.String(), nullable=True),
        sa.Column("twitter_username", sa.String(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.id"],
            name=op.f("organizations_account_id_fkey"),
            ondelete="set null",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("organizations_pkey")),
        sa.UniqueConstraint(
            "custom_domain", name=op.f("organizations_custom_domain_key")
        ),
        sa.UniqueConstraint("external_id", name=op.f("organizations_external_id_key")),
        sa.UniqueConstraint("external_id", name=op.f("organizations_external_id_key")),
        sa.UniqueConstraint(
            "installation_id", name=op.f("organizations_installation_id_key")
        ),
        sa.UniqueConstraint(
            "installation_id", name=op.f("organizations_installation_id_key")
        ),
        sa.UniqueConstraint("name", name=op.f("organizations_name_key")),
        sa.UniqueConstraint("name", name=op.f("organizations_name_key")),
        sa.UniqueConstraint(
            "stripe_customer_id", name=op.f("organizations_stripe_customer_id_key")
        ),
    )
    op.create_index(
        op.f("ix_organizations_created_at"),
        "organizations",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organizations_deleted_at"),
        "organizations",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_organizations_modified_at"),
        "organizations",
        ["modified_at"],
        unique=False,
    )
    op.create_table(
        "users",
        sa.Column("username", sa.String(length=50), nullable=False),
        sa.Column("email", sa.String(length=320), nullable=False),
        sa.Column("email_verified", sa.Boolean(), nullable=False),
        sa.Column("avatar_url", sa.String(length=1024), nullable=True),
        sa.Column("profile", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("account_id", sa.UUID(), nullable=True),
        sa.Column("invite_only_approved", sa.Boolean(), nullable=False),
        sa.Column("accepted_terms_of_service", sa.Boolean(), nullable=False),
        sa.Column("last_seen_at_extension", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("last_version_extension", sa.String(length=50), nullable=True),
        sa.Column("email_newsletters_and_changelogs", sa.Boolean(), nullable=False),
        sa.Column("email_promotions_and_events", sa.Boolean(), nullable=False),
        sa.Column("stripe_customer_id", sa.String(), nullable=True),
        sa.Column("blocked_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["accounts.id"],
            name=op.f("users_account_id_fkey"),
            ondelete="set null",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("users_pkey")),
        sa.UniqueConstraint(
            "stripe_customer_id", name=op.f("users_stripe_customer_id_key")
        ),
        sa.UniqueConstraint("username", name=op.f("users_username_key")),
    )
    op.create_index(op.f("ix_users_created_at"), "users", ["created_at"], unique=False)
    op.create_index(op.f("ix_users_deleted_at"), "users", ["deleted_at"], unique=False)
    op.create_index(
        "ix_users_email_case_insensitive",
        "users",
        [sa.text("lower(email)")],
        unique=True,
    )
    op.create_index(
        op.f("ix_users_modified_at"), "users", ["modified_at"], unique=False
    )

    op.create_foreign_key(
        op.f("accounts_admin_id_fkey"),
        "accounts",
        "users",
        ["admin_id"],
        ["id"],
        use_alter=True,
    )

    op.create_table(
        "advertisement_campaigns",
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("views", sa.Integer(), nullable=False),
        sa.Column("clicks", sa.Integer(), nullable=False),
        sa.Column("image_url", sa.String(), nullable=False),
        sa.Column("image_url_dark", sa.String(), nullable=True),
        sa.Column("text", sa.String(), nullable=False),
        sa.Column("link_url", sa.String(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.id"], name=op.f("advertisement_campaigns_user_id_fkey")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("advertisement_campaigns_pkey")),
    )
    op.create_index(
        op.f("ix_advertisement_campaigns_created_at"),
        "advertisement_campaigns",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_advertisement_campaigns_deleted_at"),
        "advertisement_campaigns",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_advertisement_campaigns_modified_at"),
        "advertisement_campaigns",
        ["modified_at"],
        unique=False,
    )
    op.create_table(
        "article_subscriptions",
        sa.Column("paid_subscriber", sa.Boolean(), nullable=False),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("emails_unsubscribed_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("article_subscriptions_organization_id_fkey"),
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("article_subscriptions_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("article_subscriptions_pkey")),
        sa.UniqueConstraint(
            "organization_id",
            "user_id",
            name=op.f("article_subscriptions_organization_id_user_id_key"),
        ),
    )
    op.create_index(
        op.f("ix_article_subscriptions_created_at"),
        "article_subscriptions",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_article_subscriptions_deleted_at"),
        "article_subscriptions",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_article_subscriptions_modified_at"),
        "article_subscriptions",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_article_subscriptions_organization_id"),
        "article_subscriptions",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_article_subscriptions_user_id"),
        "article_subscriptions",
        ["user_id"],
        unique=False,
    )
    op.create_table(
        "articles",
        sa.Column("slug", sa.String(length=255), nullable=False),
        sa.Column("title", sa.String(length=255), nullable=False),
        sa.Column("body", sa.String(), nullable=False),
        sa.Column("created_by", sa.UUID(), nullable=False),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("published_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("byline", sa.String(), nullable=False),
        sa.Column(
            "visibility",
            sa.String(),
            nullable=False,
        ),
        sa.Column("paid_subscribers_only", sa.Boolean(), nullable=False),
        sa.Column(
            "paid_subscribers_only_ends_at", sa.TIMESTAMP(timezone=True), nullable=True
        ),
        sa.Column("notifications_sent_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("notify_subscribers", sa.Boolean(), nullable=False),
        sa.Column("email_sent_to_count", sa.Integer(), nullable=True),
        sa.Column("email_open_count", sa.Integer(), nullable=False),
        sa.Column("web_view_count", sa.Integer(), nullable=False),
        sa.Column("is_pinned", sa.Boolean(), nullable=False),
        sa.Column("og_image_url", sa.String(), nullable=True),
        sa.Column("og_description", sa.String(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["created_by"], ["users.id"], name=op.f("articles_created_by_fkey")
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("articles_organization_id_fkey"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("articles_pkey")),
        sa.UniqueConstraint(
            "organization_id", "slug", name=op.f("articles_organization_id_slug_key")
        ),
    )
    op.create_index(
        op.f("ix_articles_created_at"), "articles", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_articles_deleted_at"), "articles", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_articles_modified_at"), "articles", ["modified_at"], unique=False
    )
    op.create_table(
        "benefits",
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("is_tax_applicable", sa.Boolean(), nullable=False),
        sa.Column("selectable", sa.Boolean(), nullable=False),
        sa.Column("deletable", sa.Boolean(), nullable=False),
        sa.Column(
            "properties", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("benefits_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("benefits_pkey")),
    )
    op.create_index(
        op.f("ix_benefits_created_at"), "benefits", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_benefits_deleted_at"), "benefits", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_benefits_modified_at"), "benefits", ["modified_at"], unique=False
    )
    op.create_index(op.f("ix_benefits_type"), "benefits", ["type"], unique=False)
    op.create_table(
        "files",
        sa.Column("organization_id", sa.UUID(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("version", sa.String(), nullable=True),
        sa.Column("path", sa.String(), nullable=False),
        sa.Column("mime_type", sa.String(), nullable=False),
        sa.Column("size", sa.Integer(), nullable=False),
        sa.Column("service", sa.String(), nullable=False),
        sa.Column("last_modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("storage_version", sa.String(), nullable=True),
        sa.Column("checksum_etag", sa.String(), nullable=True),
        sa.Column("checksum_sha256_base64", sa.String(), nullable=True),
        sa.Column("checksum_sha256_hex", sa.String(), nullable=True),
        sa.Column("is_uploaded", sa.Boolean(), nullable=False),
        sa.Column("is_enabled", sa.Boolean(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("files_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("files_pkey")),
    )
    op.create_index(op.f("ix_files_created_at"), "files", ["created_at"], unique=False)
    op.create_index(op.f("ix_files_deleted_at"), "files", ["deleted_at"], unique=False)
    op.create_index(
        op.f("ix_files_modified_at"), "files", ["modified_at"], unique=False
    )
    op.create_index(
        op.f("ix_files_organization_id"), "files", ["organization_id"], unique=False
    )
    op.create_table(
        "invites",
        sa.Column("created_by", sa.UUID(), nullable=False),
        sa.Column("claimed_by", sa.UUID(), nullable=True),
        sa.Column("code", sa.String(), nullable=False),
        sa.Column("note", sa.String(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["claimed_by"], ["users.id"], name=op.f("invites_claimed_by_fkey")
        ),
        sa.ForeignKeyConstraint(
            ["created_by"], ["users.id"], name=op.f("invites_created_by_fkey")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("invites_pkey")),
    )
    op.create_index(op.f("ix_invites_code"), "invites", ["code"], unique=True)
    op.create_index(
        op.f("ix_invites_created_at"), "invites", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_invites_deleted_at"), "invites", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_invites_modified_at"), "invites", ["modified_at"], unique=False
    )
    op.create_table(
        "magic_links",
        sa.Column("token_hash", sa.String(), nullable=False),
        sa.Column("expires_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("user_email", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.Column("source", sa.String(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("magic_links_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("magic_links_pkey")),
    )
    op.create_index(
        op.f("ix_magic_links_created_at"), "magic_links", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_magic_links_deleted_at"), "magic_links", ["deleted_at"], unique=False
    )
    op.create_index(
        op.f("ix_magic_links_modified_at"), "magic_links", ["modified_at"], unique=False
    )
    op.create_index(
        op.f("ix_magic_links_token_hash"), "magic_links", ["token_hash"], unique=False
    )
    op.create_table(
        "oauth2_authorization_codes",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("sub_type", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.Column("organization_id", sa.UUID(), nullable=True),
        sa.Column("code", sa.String(length=120), nullable=False),
        sa.Column("client_id", sa.String(length=48), nullable=True),
        sa.Column("redirect_uri", sa.Text(), nullable=True),
        sa.Column("response_type", sa.Text(), nullable=True),
        sa.Column("scope", sa.Text(), nullable=True),
        sa.Column("nonce", sa.Text(), nullable=True),
        sa.Column("auth_time", sa.Integer(), nullable=False),
        sa.Column("code_challenge", sa.Text(), nullable=True),
        sa.Column("code_challenge_method", sa.String(length=48), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("oauth2_authorization_codes_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("oauth2_authorization_codes_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("oauth2_authorization_codes_pkey")),
        sa.UniqueConstraint("code", name=op.f("oauth2_authorization_codes_code_key")),
    )
    op.create_index(
        op.f("ix_oauth2_authorization_codes_created_at"),
        "oauth2_authorization_codes",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_authorization_codes_deleted_at"),
        "oauth2_authorization_codes",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_authorization_codes_modified_at"),
        "oauth2_authorization_codes",
        ["modified_at"],
        unique=False,
    )
    op.create_table(
        "oauth2_clients",
        sa.Column("registration_access_token", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("client_id", sa.String(length=48), nullable=True),
        sa.Column("client_secret", sa.String(length=120), nullable=True),
        sa.Column("client_id_issued_at", sa.Integer(), nullable=False),
        sa.Column("client_secret_expires_at", sa.Integer(), nullable=False),
        sa.Column("client_metadata", sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"], ["users.id"], name=op.f("oauth2_clients_user_id_fkey")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("oauth2_clients_pkey")),
        sa.UniqueConstraint("client_id", name=op.f("oauth2_clients_client_id_key")),
    )
    op.create_index(
        op.f("ix_oauth2_clients_client_id"),
        "oauth2_clients",
        ["client_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_clients_created_at"),
        "oauth2_clients",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_clients_deleted_at"),
        "oauth2_clients",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_clients_modified_at"),
        "oauth2_clients",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_clients_registration_access_token"),
        "oauth2_clients",
        ["registration_access_token"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_clients_user_id"), "oauth2_clients", ["user_id"], unique=False
    )
    op.create_table(
        "oauth2_grants",
        sa.Column("client_id", sa.String(length=48), nullable=False),
        sa.Column("scope", sa.Text(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.Column("organization_id", sa.UUID(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("oauth2_grants_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("oauth2_grants_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("oauth2_grants_pkey")),
        sa.UniqueConstraint(
            "client_id",
            "organization_id",
            name=op.f("oauth2_grants_client_id_organization_id_key"),
        ),
        sa.UniqueConstraint(
            "client_id", "user_id", name=op.f("oauth2_grants_client_id_user_id_key")
        ),
    )
    op.create_index(
        op.f("ix_oauth2_grants_client_id"), "oauth2_grants", ["client_id"], unique=False
    )
    op.create_index(
        op.f("ix_oauth2_grants_created_at"),
        "oauth2_grants",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_grants_deleted_at"),
        "oauth2_grants",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_grants_modified_at"),
        "oauth2_grants",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_grants_organization_id"),
        "oauth2_grants",
        ["organization_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_grants_user_id"), "oauth2_grants", ["user_id"], unique=False
    )
    op.create_table(
        "oauth2_tokens",
        sa.Column("nonce", sa.String(), nullable=True),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("client_id", sa.String(length=48), nullable=True),
        sa.Column("token_type", sa.String(length=40), nullable=True),
        sa.Column("access_token", sa.String(length=255), nullable=False),
        sa.Column("refresh_token", sa.String(length=255), nullable=True),
        sa.Column("scope", sa.Text(), nullable=True),
        sa.Column("issued_at", sa.Integer(), nullable=False),
        sa.Column("access_token_revoked_at", sa.Integer(), nullable=False),
        sa.Column("refresh_token_revoked_at", sa.Integer(), nullable=False),
        sa.Column("expires_in", sa.Integer(), nullable=False),
        sa.Column("sub_type", sa.String(), nullable=False),
        sa.Column("user_id", sa.UUID(), nullable=True),
        sa.Column("organization_id", sa.UUID(), nullable=True),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organizations.id"],
            name=op.f("oauth2_tokens_organization_id_fkey"),
            ondelete="cascade",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("oauth2_tokens_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("oauth2_tokens_pkey")),
        sa.UniqueConstraint(
            "access_token", name=op.f("oauth2_tokens_access_token_key")
        ),
    )
    op.create_index(
        op.f("ix_oauth2_tokens_created_at"),
        "oauth2_tokens",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_tokens_deleted_at"),
        "oauth2_tokens",
        ["deleted_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_tokens_modified_at"),
        "oauth2_tokens",
        ["modified_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_oauth2_tokens_nonce"), "oauth2_tokens", ["nonce"], unique=False
    )
    op.create_index(
        op.f("ix_oauth2_tokens_refresh_token"),
        "oauth2_tokens",
        ["refresh_token"],
        unique=False,
    )
    op.create_table(
        "oauth_accounts",
        sa.Column("platform", sa.String(length=32), nullable=False),
        sa.Column("access_token", sa.String(length=1024), nullable=False),
        sa.Column("expires_at", sa.Integer(), nullable=True),
        sa.Column("refresh_token", sa.String(length=1024), nullable=True),
        sa.Column("account_id", sa.String(length=320), nullable=False),
        sa.Column("account_email", sa.String(length=320), nullable=False),
        sa.Column("account_username", sa.String(length=320), nullable=True),
        sa.Column("user_id", sa.UUID(), nullable=False),
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False),
        sa.Column("modified_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["users.id"],
            name=op.f("oauth_accounts_user_id_fkey"),
            ondelete="cascade",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("oauth_accounts_pkey")),
        sa.UniqueConstraint(
            "platform",
            "account_id",
            name=op.f("oauth_accounts_platform_account_id_key"),
        ),
    )
    op.create_index(
        "idx_user_id_platform", "oauth_accounts", ["user_id", "platform"], unique=False
    )
    op.create_index(
        op.f("ix_oauth_accounts_created_at"),
        "oauth_accounts",
        ["created_at"],
        unique=False,
    )
