feat: Datenmodell (SQLAlchemy) und Alembic-Migration

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 17:09:02 +02:00
parent 53ffd65104
commit d37cb230c4
10 changed files with 633 additions and 0 deletions

1
finance/alembic/README Normal file
View File

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

84
finance/alembic/env.py Normal file
View File

@@ -0,0 +1,84 @@
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 myapp import mymodel
# target_metadata = mymodel.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.
from app.config import get_settings
from app.db import Base
import app.models # noqa: F401
config.set_main_option("sqlalchemy.url", get_settings().database_url)
target_metadata = Base.metadata
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,177 @@
"""initial schema
Revision ID: b2b1f5a18a74
Revises:
Create Date: 2026-07-17 17:08:30.212347
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b2b1f5a18a74'
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('accounts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('bank', sa.String(length=50), nullable=False),
sa.Column('iban', sa.String(length=34), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('type', sa.String(length=20), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('iban')
)
op.create_table('categories',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('loans',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('principal', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('annual_rate_pct', sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column('term_months', sa.Integer(), nullable=False),
sa.Column('payout_date', sa.Date(), nullable=False),
sa.Column('repayment_type', sa.String(length=20), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('scenarios',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('description', sa.Text(), nullable=False),
sa.Column('include_recurring', sa.Boolean(), nullable=False),
sa.Column('include_planned', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('category_rules',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('pattern', sa.String(length=200), nullable=False),
sa.Column('category_id', sa.Integer(), nullable=False),
sa.Column('priority', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('planned_items',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('amount', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('due', sa.Date(), nullable=False),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('projection_points',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('scenario_id', sa.Integer(), nullable=False),
sa.Column('day', sa.Date(), nullable=False),
sa.Column('balance', sa.Numeric(precision=12, scale=2), nullable=False),
sa.ForeignKeyConstraint(['scenario_id'], ['scenarios.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_projection_points_scenario_id'), 'projection_points', ['scenario_id'], unique=False)
op.create_table('projection_results',
sa.Column('scenario_id', sa.Integer(), nullable=False),
sa.Column('computed_at', sa.DateTime(), nullable=False),
sa.Column('low_point_date', sa.Date(), nullable=False),
sa.Column('low_point_balance', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('below_zero_date', sa.Date(), nullable=True),
sa.Column('below_threshold_date', sa.Date(), nullable=True),
sa.ForeignKeyConstraint(['scenario_id'], ['scenarios.id'], ),
sa.PrimaryKeyConstraint('scenario_id')
)
op.create_table('recurring_items',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=200), nullable=False),
sa.Column('amount', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('rhythm', sa.String(length=20), nullable=False),
sa.Column('due_day', sa.Integer(), nullable=False),
sa.Column('start_date', sa.Date(), nullable=True),
sa.Column('end_date', sa.Date(), nullable=True),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('scenario_loans',
sa.Column('scenario_id', sa.Integer(), nullable=False),
sa.Column('loan_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['loan_id'], ['loans.id'], ),
sa.ForeignKeyConstraint(['scenario_id'], ['scenarios.id'], ),
sa.PrimaryKeyConstraint('scenario_id', 'loan_id')
)
op.create_table('scenario_modifiers',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('scenario_id', sa.Integer(), nullable=False),
sa.Column('target_type', sa.String(length=20), nullable=False),
sa.Column('target_id', sa.Integer(), nullable=False),
sa.Column('kind', sa.String(length=20), nullable=False),
sa.Column('value', sa.Numeric(precision=12, scale=2), nullable=False),
sa.ForeignKeyConstraint(['scenario_id'], ['scenarios.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('statements',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('filename', sa.String(length=255), nullable=False),
sa.Column('bank', sa.String(length=50), nullable=False),
sa.Column('account_id', sa.Integer(), nullable=True),
sa.Column('period_start', sa.Date(), nullable=True),
sa.Column('period_end', sa.Date(), nullable=True),
sa.Column('opening_balance', sa.Numeric(precision=12, scale=2), nullable=True),
sa.Column('closing_balance', sa.Numeric(precision=12, scale=2), nullable=True),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('error_message', sa.Text(), nullable=True),
sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('transactions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('account_id', sa.Integer(), nullable=False),
sa.Column('statement_id', sa.Integer(), nullable=True),
sa.Column('booking_date', sa.Date(), nullable=False),
sa.Column('value_date', sa.Date(), nullable=True),
sa.Column('amount', sa.Numeric(precision=12, scale=2), nullable=False),
sa.Column('purpose', sa.Text(), nullable=False),
sa.Column('counterparty', sa.String(length=200), nullable=False),
sa.Column('category_id', sa.Integer(), nullable=True),
sa.Column('status', sa.String(length=20), nullable=False),
sa.Column('dedup_hash', sa.String(length=64), nullable=False),
sa.Column('is_duplicate', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['account_id'], ['accounts.id'], ),
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
sa.ForeignKeyConstraint(['statement_id'], ['statements.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_transactions_dedup_hash'), 'transactions', ['dedup_hash'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_transactions_dedup_hash'), table_name='transactions')
op.drop_table('transactions')
op.drop_table('statements')
op.drop_table('scenario_modifiers')
op.drop_table('scenario_loans')
op.drop_table('recurring_items')
op.drop_table('projection_results')
op.drop_index(op.f('ix_projection_points_scenario_id'), table_name='projection_points')
op.drop_table('projection_points')
op.drop_table('planned_items')
op.drop_table('category_rules')
op.drop_table('scenarios')
op.drop_table('loans')
op.drop_table('categories')
op.drop_table('accounts')
# ### end Alembic commands ###