"""file storage backend Revision ID: 3d4e5f6a7b8c Revises: 2c3d4e5f6a7b Create Date: 2026-06-12 00:00:00.000000 """ from __future__ import annotations from typing import Sequence, Union from alembic import op import sqlalchemy as sa revision: str = "3d4e5f6a7b8c" down_revision: Union[str, None] = "2c3d4e5f6a7b" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.create_table( "file_blobs", sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("storage_backend", sa.String(length=50), nullable=False), sa.Column("storage_bucket", sa.String(length=255), nullable=True), sa.Column("storage_key", sa.String(length=1000), nullable=False), sa.Column("checksum_sha256", sa.String(length=64), nullable=False), sa.Column("size_bytes", sa.Integer(), nullable=False), sa.Column("content_type", sa.String(length=255), nullable=True), sa.Column("ref_count", sa.Integer(), nullable=False, server_default="1"), sa.Column("retained_until", sa.DateTime(timezone=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("tenant_id", "checksum_sha256", "size_bytes", name="uq_file_blobs_tenant_checksum_size"), ) op.create_index(op.f("ix_file_blobs_tenant_id"), "file_blobs", ["tenant_id"]) op.create_index(op.f("ix_file_blobs_checksum_sha256"), "file_blobs", ["checksum_sha256"]) op.create_table( "file_assets", sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("owner_type", sa.String(length=20), nullable=False), sa.Column("owner_user_id", sa.String(length=36), nullable=True), sa.Column("owner_group_id", sa.String(length=36), nullable=True), sa.Column("current_version_id", sa.String(length=36), nullable=True), sa.Column("display_path", sa.String(length=1000), nullable=False), sa.Column("filename", sa.String(length=500), nullable=False), sa.Column("description", sa.Text(), nullable=True), sa.Column("created_by_user_id", sa.String(length=36), nullable=True), sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True), sa.Column("metadata", sa.JSON(), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["owner_group_id"], ["groups.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["owner_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), ) for col in ["tenant_id", "owner_type", "owner_user_id", "owner_group_id", "current_version_id", "display_path", "filename", "created_by_user_id", "deleted_at"]: op.create_index(op.f(f"ix_file_assets_{col}"), "file_assets", [col]) op.create_table( "file_versions", sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("file_asset_id", sa.String(length=36), nullable=False), sa.Column("blob_id", sa.String(length=36), nullable=False), sa.Column("version_number", sa.Integer(), nullable=False), sa.Column("filename_at_upload", sa.String(length=500), nullable=False), sa.Column("display_path_at_upload", sa.String(length=1000), nullable=False), sa.Column("content_type", sa.String(length=255), nullable=True), sa.Column("size_bytes", sa.Integer(), nullable=False), sa.Column("checksum_sha256", sa.String(length=64), nullable=False), sa.Column("created_by_user_id", sa.String(length=36), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["blob_id"], ["file_blobs.id"], ondelete="RESTRICT"), sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["file_asset_id"], ["file_assets.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("file_asset_id", "version_number", name="uq_file_versions_asset_number"), ) for col in ["tenant_id", "file_asset_id", "blob_id", "checksum_sha256", "created_by_user_id"]: op.create_index(op.f(f"ix_file_versions_{col}"), "file_versions", [col]) op.create_table( "file_shares", sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("file_asset_id", sa.String(length=36), nullable=False), sa.Column("target_type", sa.String(length=20), nullable=False), sa.Column("target_id", sa.String(length=36), nullable=False), sa.Column("permission", sa.String(length=20), nullable=False, server_default="read"), sa.Column("created_by_user_id", sa.String(length=36), nullable=True), sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["file_asset_id"], ["file_assets.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("file_asset_id", "target_type", "target_id", "revoked_at", name="uq_file_shares_active_target"), ) for col in ["tenant_id", "file_asset_id", "target_type", "target_id", "created_by_user_id", "revoked_at"]: op.create_index(op.f(f"ix_file_shares_{col}"), "file_shares", [col]) op.create_table( "campaign_attachment_uses", sa.Column("id", sa.String(length=36), nullable=False), sa.Column("tenant_id", sa.String(length=36), nullable=False), sa.Column("campaign_id", sa.String(length=36), nullable=False), sa.Column("campaign_version_id", sa.String(length=36), nullable=False), sa.Column("campaign_job_id", sa.String(length=36), nullable=True), sa.Column("entry_index", sa.Integer(), nullable=True), sa.Column("entry_id", sa.String(length=255), nullable=True), sa.Column("file_asset_id", sa.String(length=36), nullable=False), sa.Column("file_version_id", sa.String(length=36), nullable=False), sa.Column("file_blob_id", sa.String(length=36), nullable=False), sa.Column("filename_used", sa.String(length=500), nullable=False), sa.Column("checksum_sha256", sa.String(length=64), nullable=False), sa.Column("size_bytes", sa.Integer(), nullable=False), sa.Column("content_type", sa.String(length=255), nullable=True), sa.Column("use_stage", sa.String(length=20), nullable=False, server_default="built"), sa.Column("used_at", sa.DateTime(timezone=True), nullable=True), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["campaign_id"], ["campaigns.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["campaign_job_id"], ["campaign_jobs.id"], ondelete="SET NULL"), sa.ForeignKeyConstraint(["campaign_version_id"], ["campaign_versions.id"], ondelete="CASCADE"), sa.ForeignKeyConstraint(["file_asset_id"], ["file_assets.id"], ondelete="RESTRICT"), sa.ForeignKeyConstraint(["file_blob_id"], ["file_blobs.id"], ondelete="RESTRICT"), sa.ForeignKeyConstraint(["file_version_id"], ["file_versions.id"], ondelete="RESTRICT"), sa.ForeignKeyConstraint(["tenant_id"], ["tenants.id"], ondelete="CASCADE"), sa.PrimaryKeyConstraint("id"), sa.UniqueConstraint("campaign_job_id", "file_version_id", "filename_used", "use_stage", name="uq_campaign_attachment_uses_job_file_stage"), ) for col in ["tenant_id", "campaign_id", "campaign_version_id", "campaign_job_id", "entry_id", "file_asset_id", "file_version_id", "file_blob_id", "use_stage", "used_at"]: op.create_index(op.f(f"ix_campaign_attachment_uses_{col}"), "campaign_attachment_uses", [col]) def downgrade() -> None: op.drop_table("campaign_attachment_uses") op.drop_table("file_shares") op.drop_table("file_versions") op.drop_table("file_assets") op.drop_table("file_blobs")