Files
forwarder/migrations/versions/001_initial_schema.py
T

160 lines
8.0 KiB
Python

"""Initial schema
Revision ID: 001_initial
Revises:
Create Date: 2026-06-30
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
revision: str = "001_initial"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("username", sa.String(length=128), nullable=False),
sa.Column("password_hash", sa.String(length=255), nullable=False),
sa.Column("is_admin", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("username"),
)
op.create_table(
"accounts",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("enabled", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("sync_interval_minutes", sa.Integer(), nullable=False, server_default=sa.text("15")),
sa.Column("imap_host", sa.String(length=255), nullable=False),
sa.Column("imap_port", sa.Integer(), nullable=False, server_default=sa.text("993")),
sa.Column("imap_user", sa.String(length=255), nullable=False),
sa.Column("imap_password_encrypted", sa.Text(), nullable=False),
sa.Column("smtp_host", sa.String(length=255), nullable=False),
sa.Column("smtp_port", sa.Integer(), nullable=False, server_default=sa.text("587")),
sa.Column("smtp_user", sa.String(length=255), nullable=False),
sa.Column("smtp_password_encrypted", sa.Text(), nullable=False),
sa.Column("smtp_use_tls", sa.Boolean(), nullable=False, server_default=sa.text("1")),
sa.Column("destination_email", sa.String(length=255), nullable=False),
sa.Column("include_all_attachments", sa.Boolean(), nullable=False, server_default=sa.text("0")),
sa.Column("allowed_extensions", sa.JSON(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"user_accounts",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.ForeignKeyConstraint(["user_id"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"messages",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("message_key", sa.String(length=512), nullable=False),
sa.Column("folder", sa.String(length=512), nullable=False),
sa.Column("uid", sa.String(length=64), nullable=False),
sa.Column("subject", sa.String(length=1024), nullable=False, server_default=""),
sa.Column("from_addr", sa.String(length=512), nullable=False, server_default=""),
sa.Column("date", sa.String(length=128), nullable=False, server_default=""),
sa.Column(
"status",
sa.Enum("pending", "forwarded", "failed", "skipped", "dry_run", name="messagestatus"),
nullable=False,
),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("account_id", "message_key", name="uq_account_message_key"),
)
op.create_index("ix_messages_account_id", "messages", ["account_id"])
op.create_index("ix_messages_status", "messages", ["status"])
op.create_table(
"forward_attempts",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("message_id", sa.Integer(), nullable=False),
sa.Column("success", sa.Boolean(), nullable=False),
sa.Column("dry_run", sa.Boolean(), nullable=False, server_default=sa.text("0")),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.ForeignKeyConstraint(["message_id"], ["messages.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_forward_attempts_message_id", "forward_attempts", ["message_id"])
op.create_table(
"sync_runs",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column(
"status",
sa.Enum("running", "completed", "failed", name="syncrunstatus"),
nullable=False,
),
sa.Column("dry_run", sa.Boolean(), nullable=False, server_default=sa.text("0")),
sa.Column("max_emails", sa.Integer(), nullable=True),
sa.Column("messages_scanned", sa.Integer(), nullable=False, server_default=sa.text("0")),
sa.Column("messages_matched", sa.Integer(), nullable=False, server_default=sa.text("0")),
sa.Column("messages_forwarded", sa.Integer(), nullable=False, server_default=sa.text("0")),
sa.Column("messages_skipped", sa.Integer(), nullable=False, server_default=sa.text("0")),
sa.Column("messages_failed", sa.Integer(), nullable=False, server_default=sa.text("0")),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("started_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_sync_runs_account_id", "sync_runs", ["account_id"])
op.create_table(
"connection_tests",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("account_id", sa.Integer(), nullable=True),
sa.Column(
"test_type",
sa.Enum("imap", "smtp", "send", name="connectiontesttype"),
nullable=False,
),
sa.Column("success", sa.Boolean(), nullable=False),
sa.Column("message", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_connection_tests_account_id", "connection_tests", ["account_id"])
def downgrade() -> None:
op.drop_index("ix_connection_tests_account_id", table_name="connection_tests")
op.drop_table("connection_tests")
op.drop_index("ix_sync_runs_account_id", table_name="sync_runs")
op.drop_table("sync_runs")
op.drop_index("ix_forward_attempts_message_id", table_name="forward_attempts")
op.drop_table("forward_attempts")
op.drop_index("ix_messages_status", table_name="messages")
op.drop_index("ix_messages_account_id", table_name="messages")
op.drop_table("messages")
op.drop_table("user_accounts")
op.drop_table("accounts")
op.drop_table("users")
op.execute("DROP TYPE IF EXISTS connectiontesttype")
op.execute("DROP TYPE IF EXISTS syncrunstatus")
op.execute("DROP TYPE IF EXISTS messagestatus")