Add db migration and DNS dmarc entries

This commit is contained in:
2026-05-20 13:20:58 -03:00
parent 636d3b73cb
commit e57df39562
19 changed files with 850 additions and 12 deletions
+55
View File
@@ -0,0 +1,55 @@
from __future__ import annotations
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from app.config import get_settings
from app.db import Base
from app import models # noqa: F401
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _database_url() -> str:
return get_settings().app.database_url
def run_migrations_offline() -> None:
context.configure(
url=_database_url(),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
section = config.get_section(config.config_ini_section, {})
section["sqlalchemy.url"] = _database_url()
connectable = engine_from_config(
section,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+24
View File
@@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,66 @@
"""add domain dns snapshots
Revision ID: 20260520_0001
Revises:
Create Date: 2026-05-20 00:00:00.000000+00:00
"""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
revision = "20260520_0001"
down_revision = None
branch_labels = None
depends_on = None
def _domain_dns_snapshots() -> sa.Table:
return sa.Table(
"domain_dns_snapshots",
sa.MetaData(),
sa.Column("id", sa.Integer(), primary_key=True),
sa.Column("domain", sa.String(length=255), nullable=False),
sa.Column("checked_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("dmarc_record", sa.Text(), nullable=True),
sa.Column("dmarc_policy_p", sa.String(length=40), nullable=True),
sa.Column("dmarc_policy_sp", sa.String(length=40), nullable=True),
sa.Column("dmarc_policy_pct", sa.Integer(), nullable=True),
sa.Column("dmarc_adkim", sa.String(length=20), nullable=True),
sa.Column("dmarc_aspf", sa.String(length=20), nullable=True),
sa.Column("dmarc_fo", sa.String(length=80), nullable=True),
sa.Column("dmarc_rua", sa.Text(), nullable=True),
sa.Column("dmarc_ruf", sa.Text(), nullable=True),
sa.Column("spf_record", sa.Text(), nullable=True),
sa.Column("spf_all", sa.String(length=20), nullable=True),
sa.Column("spf_includes_json", sa.Text(), nullable=False, server_default="[]"),
sa.Column("dkim_records_json", sa.Text(), nullable=False, server_default="[]"),
sa.Column("mx_records_json", sa.Text(), nullable=False, server_default="[]"),
sa.Column("errors_json", sa.Text(), nullable=False, server_default="[]"),
)
def upgrade() -> None:
bind = op.get_bind()
table = _domain_dns_snapshots()
table.create(bind=bind, checkfirst=True)
op.create_index(
op.f("ix_domain_dns_snapshots_checked_at"),
"domain_dns_snapshots",
["checked_at"],
unique=False,
if_not_exists=True,
)
op.create_index(
op.f("ix_domain_dns_snapshots_domain"),
"domain_dns_snapshots",
["domain"],
unique=False,
if_not_exists=True,
)
def downgrade() -> None:
op.drop_index(op.f("ix_domain_dns_snapshots_domain"), table_name="domain_dns_snapshots", if_exists=True)
op.drop_index(op.f("ix_domain_dns_snapshots_checked_at"), table_name="domain_dns_snapshots", if_exists=True)
_domain_dns_snapshots().drop(bind=op.get_bind(), checkfirst=True)