mirror of
https://github.com/escalante29/WealthySmart.git
synced 2026-07-17 10:28:47 +02:00
Adopt Alembic: autogenerated baseline, stamp-or-upgrade startup
The baseline (c3da001a0eb3) was autogenerated against an empty Postgres and schema-diffed against the live dev schema until column-identical (two model fixes fell out: deferred_to_next_cycle now declares its server_default, usersettings.data is JSONB on Postgres to match the live type). Startup replaces init_db/run_migrations with run_alembic_upgrade(): pre-Alembic databases that already match the baseline are adopted via stamp; fresh databases build from the migration. The section is serialized with a pg advisory lock because prod uvicorn runs 2 workers, each executing the lifespan. Verified: adoption + idempotent re-run on the dev DB, fresh 13-table build on an empty DB, full container boot, 55/55 tests. (ARCH-02, BE-19) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1
backend/alembic/README
Normal file
1
backend/alembic/README
Normal file
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
49
backend/alembic/env.py
Normal file
49
backend/alembic/env.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
from sqlmodel import SQLModel
|
||||
|
||||
from app.config import settings
|
||||
import app.models.models # noqa: F401 — register all tables on the metadata
|
||||
|
||||
config = context.config
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Single source of truth for the DB URL: the app settings.
|
||||
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
|
||||
|
||||
target_metadata = SQLModel.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=config.get_main_option("sqlalchemy.url"),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
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()
|
||||
29
backend/alembic/script.py.mako
Normal file
29
backend/alembic/script.py.mako
Normal file
@@ -0,0 +1,29 @@
|
||||
"""${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
|
||||
import sqlmodel
|
||||
${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"}
|
||||
221
backend/alembic/versions/c3da001a0eb3_baseline.py
Normal file
221
backend/alembic/versions/c3da001a0eb3_baseline.py
Normal file
@@ -0,0 +1,221 @@
|
||||
"""baseline
|
||||
|
||||
Revision ID: c3da001a0eb3
|
||||
Revises:
|
||||
Create Date: 2026-06-09 20:00:18.301439
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlmodel
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'c3da001a0eb3'
|
||||
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('account',
|
||||
sa.Column('bank', sa.Enum('BAC', 'BCR', 'DAVIVIENDA', 'FCL', 'ROP', 'VOL', 'MEMP', 'MPAT', 'MORTGAGE', name='bank'), nullable=False),
|
||||
sa.Column('currency', sa.Enum('CRC', 'USD', 'EUR', 'BTC', 'XMR', name='currency'), nullable=False),
|
||||
sa.Column('label', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('balance', sa.Float(), nullable=False),
|
||||
sa.Column('account_type', sa.Enum('BANK', 'PENSION', 'CRYPTO', 'SAVINGS', 'LIABILITY', name='accounttype'), nullable=False),
|
||||
sa.Column('next_payment', sa.Float(), nullable=True),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('apitoken',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('token_hash', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('expires_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_apitoken_token_hash'), 'apitoken', ['token_hash'], unique=False)
|
||||
op.create_table('balanceoverride',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('year', sa.Integer(), nullable=False),
|
||||
sa.Column('month', sa.Integer(), nullable=False),
|
||||
sa.Column('override_balance', sa.Float(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('year', 'month')
|
||||
)
|
||||
op.create_table('category',
|
||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('icon', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('auto_match_patterns', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_category_name'), 'category', ['name'], unique=True)
|
||||
op.create_table('exchangerate',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('date', sa.DateTime(), nullable=False),
|
||||
sa.Column('buy_rate', sa.Float(), nullable=False),
|
||||
sa.Column('sell_rate', sa.Float(), nullable=False),
|
||||
sa.Column('fetched_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('municipalreceipt',
|
||||
sa.Column('receipt_date', sa.Date(), nullable=False),
|
||||
sa.Column('due_date', sa.Date(), nullable=False),
|
||||
sa.Column('period', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('account', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('finca', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('holder_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('holder_cedula', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('holder_address', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('subtotal', sa.Float(), nullable=False),
|
||||
sa.Column('interests', sa.Float(), nullable=False),
|
||||
sa.Column('iva', sa.Float(), nullable=False),
|
||||
sa.Column('total', sa.Float(), nullable=False),
|
||||
sa.Column('raw_charges', sa.JSON(), server_default='[]', nullable=False),
|
||||
sa.Column('source_filename', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('account', 'period')
|
||||
)
|
||||
op.create_table('pensionsnapshot',
|
||||
sa.Column('fund', sa.Enum('BAC', 'BCR', 'DAVIVIENDA', 'FCL', 'ROP', 'VOL', 'MEMP', 'MPAT', 'MORTGAGE', name='bank'), nullable=False),
|
||||
sa.Column('contract_number', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('period_start', sa.Date(), nullable=False),
|
||||
sa.Column('period_end', sa.Date(), nullable=False),
|
||||
sa.Column('saldo_anterior', sa.Float(), nullable=False),
|
||||
sa.Column('aportes', sa.Float(), nullable=False),
|
||||
sa.Column('rendimientos', sa.Float(), nullable=False),
|
||||
sa.Column('retiros', sa.Float(), nullable=False),
|
||||
sa.Column('traslados', sa.Float(), nullable=False),
|
||||
sa.Column('comision', sa.Float(), nullable=False),
|
||||
sa.Column('correccion', sa.Float(), nullable=False),
|
||||
sa.Column('bonificacion', sa.Float(), nullable=False),
|
||||
sa.Column('saldo_final', sa.Float(), nullable=False),
|
||||
sa.Column('source_filename', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('fund', 'period_start', 'period_end')
|
||||
)
|
||||
op.create_table('pushsubscription',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('endpoint', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('p256dh', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('auth', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('endpoint')
|
||||
)
|
||||
op.create_table('savingsaccrual',
|
||||
sa.Column('year', sa.Integer(), nullable=False),
|
||||
sa.Column('month', sa.Integer(), nullable=False),
|
||||
sa.Column('memp_amount', sa.Float(), nullable=False),
|
||||
sa.Column('mpat_amount', sa.Float(), nullable=False),
|
||||
sa.Column('trigger_transaction_id', sa.Integer(), nullable=True),
|
||||
sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('applied_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('year', 'month')
|
||||
)
|
||||
op.create_table('usersettings',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('key', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('data', sa.JSON().with_variant(postgresql.JSONB(astext_type=sa.Text()), 'postgresql'), server_default='{}', nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_usersettings_key'), 'usersettings', ['key'], unique=True)
|
||||
op.create_table('recurringitem',
|
||||
sa.Column('name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('amount', sa.Float(), nullable=False),
|
||||
sa.Column('currency', sa.Enum('CRC', 'USD', 'EUR', 'BTC', 'XMR', name='currency'), nullable=False),
|
||||
sa.Column('item_type', sa.Enum('INCOME', 'EXPENSE', 'SAVINGS', name='recurringitemtype'), nullable=False),
|
||||
sa.Column('frequency', sa.Enum('WEEKLY', 'MONTHLY', 'QUARTERLY', 'BIANNUAL', 'YEARLY', name='recurringfrequency'), nullable=False),
|
||||
sa.Column('day_of_month', sa.Integer(), nullable=True),
|
||||
sa.Column('month_of_year', sa.Integer(), nullable=True),
|
||||
sa.Column('override_amounts', sa.JSON(), nullable=True),
|
||||
sa.Column('category_id', sa.Integer(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_table('transaction',
|
||||
sa.Column('amount', sa.Float(), nullable=False),
|
||||
sa.Column('currency', sa.Enum('CRC', 'USD', 'EUR', 'BTC', 'XMR', name='currency'), nullable=False),
|
||||
sa.Column('merchant', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('city', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('date', sa.DateTime(), nullable=False),
|
||||
sa.Column('card_type', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('card_last4', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('authorization_code', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('reference', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('transaction_type', sa.Enum('COMPRA', 'DEVOLUCION', 'DEPOSITO', 'SALARY', name='transactiontype'), nullable=False),
|
||||
sa.Column('source', sa.Enum('CREDIT_CARD', 'CASH', 'TRANSFER', name='transactionsource'), nullable=False),
|
||||
sa.Column('bank', sa.Enum('BAC', 'BCR', 'DAVIVIENDA', 'FCL', 'ROP', 'VOL', 'MEMP', 'MPAT', 'MORTGAGE', name='bank'), nullable=False),
|
||||
sa.Column('notes', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
|
||||
sa.Column('category_id', sa.Integer(), nullable=True),
|
||||
sa.Column('deferred_to_next_cycle', sa.Boolean(), server_default='false', nullable=False),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['category_id'], ['category.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_transaction_reference'), 'transaction', ['reference'], unique=False)
|
||||
op.create_table('watermeterreading',
|
||||
sa.Column('meter_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('period', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('reading_previous', sa.Float(), nullable=False),
|
||||
sa.Column('reading_current', sa.Float(), nullable=False),
|
||||
sa.Column('consumption_m3', sa.Float(), nullable=False),
|
||||
sa.Column('agua_potable', sa.Float(), nullable=False),
|
||||
sa.Column('serv_ambientales', sa.Float(), nullable=False),
|
||||
sa.Column('alcant_sanitario', sa.Float(), nullable=False),
|
||||
sa.Column('iva', sa.Float(), nullable=False),
|
||||
sa.Column('is_historical', sa.Boolean(), nullable=False),
|
||||
sa.Column('receipt_id', sa.Integer(), nullable=True),
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['receipt_id'], ['municipalreceipt.id'], ),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('meter_id', 'period', 'is_historical')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('watermeterreading')
|
||||
op.drop_index(op.f('ix_transaction_reference'), table_name='transaction')
|
||||
op.drop_table('transaction')
|
||||
op.drop_table('recurringitem')
|
||||
op.drop_index(op.f('ix_usersettings_key'), table_name='usersettings')
|
||||
op.drop_table('usersettings')
|
||||
op.drop_table('savingsaccrual')
|
||||
op.drop_table('pushsubscription')
|
||||
op.drop_table('pensionsnapshot')
|
||||
op.drop_table('municipalreceipt')
|
||||
op.drop_table('exchangerate')
|
||||
op.drop_index(op.f('ix_category_name'), table_name='category')
|
||||
op.drop_table('category')
|
||||
op.drop_table('balanceoverride')
|
||||
op.drop_index(op.f('ix_apitoken_token_hash'), table_name='apitoken')
|
||||
op.drop_table('apitoken')
|
||||
op.drop_table('account')
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user