chore: introduce alembic

This commit is contained in:
Leon
2025-07-17 17:25:06 +02:00
parent 1d2da3baab
commit 8f3634d0dc
9 changed files with 416 additions and 2 deletions

1
backend/alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

79
backend/alembic/env.py Normal file
View File

@@ -0,0 +1,79 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
from app.core.database import Base
from app.models import newsletters, entries, settings # noqa
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_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()

View File

@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,85 @@
"""Initial migration
Revision ID: fb190ac6937f
Revises:
Create Date: 2025-07-17 15:33:43.435587
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'fb190ac6937f'
down_revision: Union[str, Sequence[str], None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('newsletters',
sa.Column('id', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('move_to_folder', sa.String(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('extract_content', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_newsletters_id'), 'newsletters', ['id'], unique=False)
op.create_table('settings',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('imap_server', sa.String(), nullable=True),
sa.Column('imap_username', sa.String(), nullable=True),
sa.Column('imap_password', sa.String(), nullable=True),
sa.Column('search_folder', sa.String(), nullable=True),
sa.Column('move_to_folder', sa.String(), nullable=True),
sa.Column('mark_as_read', sa.Boolean(), nullable=True),
sa.Column('email_check_interval', sa.Integer(), nullable=True),
sa.Column('auto_add_new_senders', sa.Boolean(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_settings_id'), 'settings', ['id'], unique=False)
op.create_index(op.f('ix_settings_imap_server'), 'settings', ['imap_server'], unique=False)
op.create_table('entries',
sa.Column('id', sa.String(), nullable=False),
sa.Column('newsletter_id', sa.String(), nullable=True),
sa.Column('subject', sa.String(), nullable=True),
sa.Column('body', sa.Text(), nullable=True),
sa.Column('received_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('message_id', sa.String(), nullable=False),
sa.ForeignKeyConstraint(['newsletter_id'], ['newsletters.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_entries_id'), 'entries', ['id'], unique=False)
op.create_index(op.f('ix_entries_message_id'), 'entries', ['message_id'], unique=True)
op.create_table('senders',
sa.Column('id', sa.String(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('newsletter_id', sa.String(), nullable=False),
sa.ForeignKeyConstraint(['newsletter_id'], ['newsletters.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_senders_email'), 'senders', ['email'], unique=True)
op.create_index(op.f('ix_senders_id'), 'senders', ['id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_senders_id'), table_name='senders')
op.drop_index(op.f('ix_senders_email'), table_name='senders')
op.drop_table('senders')
op.drop_index(op.f('ix_entries_message_id'), table_name='entries')
op.drop_index(op.f('ix_entries_id'), table_name='entries')
op.drop_table('entries')
op.drop_index(op.f('ix_settings_imap_server'), table_name='settings')
op.drop_index(op.f('ix_settings_id'), table_name='settings')
op.drop_table('settings')
op.drop_index(op.f('ix_newsletters_id'), table_name='newsletters')
op.drop_table('newsletters')
# ### end Alembic commands ###