aeat.adapters.persistence.storage.sql._orm module

Internal SQLAlchemy ORM mapper classes.

Backs the declarative schema consumed by Alembic autogenerate. Intentionally kept out of the adapters.persistence.storage public API: the public surface exposes pydantic v2 records (see adapters.persistence.storage.sql.records) and the per-domain repositories bridge between the ORM rows and the typed records.

class Base(**kwargs)[source]

Bases: DeclarativeBase

Declarative base for every ORM mapper class in this package.

Parameters:

kwargs (Any)

metadata: ClassVar[MetaData]

Refers to the _schema.MetaData collection that will be used for new _schema.Table objects.

registry: ClassVar[_RegistryType]

Refers to the _orm.registry in use where new _orm.Mapper objects will be associated.

class ModeloRow(**kwargs)[source]

Bases: Base

Row in the modelos table.

Variables:
  • id – Surrogate integer primary key.

  • identifier – Stable natural key for the modelo record.

  • name – Human-readable modelo name.

id: Mapped[int]
identifier: Mapped[str]
name: Mapped[str]
class PortalOrmRow(**kwargs)[source]

Bases: Base

Row in the portals table.

Variables:
  • id – Surrogate integer primary key.

  • identifier – Stable natural key (e.g. SEDE_ELECTRONICA_ROOT).

  • base_url – Canonical URL for the portal.

  • auth_method – Authentication method as a short string code.

  • modelo_id – Optional foreign key to ModeloRow.

id: Mapped[int]
identifier: Mapped[str]
base_url: Mapped[str]
auth_method: Mapped[str]
modelo_id: Mapped[int | None]
label: Mapped[str]
modelo: Mapped[ModeloRow | None]
class CorpusArtifactRow(**kwargs)[source]

Bases: Base

Row in the corpus_artifacts table.

Variables:
  • id – Surrogate integer primary key.

  • year – Tax year this artifact belongs to.

  • modelo_id – Foreign key to the owning ModeloRow.

  • file_path – Project-relative path to the on-disk artifact.

  • sha256 – Hex digest of the artifact bytes.

  • source_url – URL the artifact was fetched from.

  • fetched_at – Timestamp when the artifact was fetched (UTC).

id: Mapped[int]
year: Mapped[int]
modelo_id: Mapped[int]
file_path: Mapped[str]
sha256: Mapped[str]
source_url: Mapped[str]
fetched_at: Mapped[datetime]
modelo: Mapped[ModeloRow]
class TransactionDateIndexRow(**kwargs)[source]

Bases: Base

Plaintext routing row: one ledger transaction’s filing date and year.

This table is a derived, rebuildable read-side cache co-written atomically with SecureObjectRow ledger writes (see TransactionCatalogueRepository). It exists purely to let a period-scoped ledger read select the candidate transaction ids for a date range with a plaintext SQL predicate, so only those rows need to be decrypted – never the whole per-bucket catalogue.

The row carries ONLY non-sensitive routing keys: the bucket id, the transaction id, its filing date (value_date or booked_date – the same field every ledger aggregator already filters on), and the filing year the date falls in. No amount, counterparty, description, NIF, or other financial content may ever be added to this table; it is plaintext by design (SensitivityClass CACHE) and correctness never depends on it being present or fresh – a missing or incomplete index falls back to the full encrypted scan.

Variables:
  • id – Surrogate integer primary key.

  • bucket_id – Owning profile bucket, so a shared database never mixes two buckets’ routing rows.

  • transaction_id – The ledger transaction’s stable content-derived id.

  • filing_datevalue_date or booked_date (whichever the ledger aggregation layer would use) as a plain SQL Date.

  • filing_yearfiling_date.year, indexed separately so a year-scoped candidate-id query does not need a date-range predicate at all.

id: Mapped[int]
bucket_id: Mapped[str]
transaction_id: Mapped[str]
filing_date: Mapped[date]
filing_year: Mapped[int]
class SecureObjectRow(**kwargs)[source]

Bases: Base

Encrypted byte-object row for sensitive application payloads.

Domain repositories use this table for financial catalogues and workflow state that must not land as standalone JSON files. The payload column is a SQL BLOB holding the AEAD wire bytes; the repository encrypts and decrypts it explicitly (rather than through a column TypeDecorator) so the row identity (namespace + object_key digest + schema_version) can be bound into the AEAD associated data, making a ciphertext refuse to decrypt under any other row. The remaining fields are routing, revision-lineage, and integrity metadata.

id: Mapped[int]
namespace: Mapped[str]
object_key: Mapped[bytes]
classification: Mapped[str]
schema_version: Mapped[int]
written_at: Mapped[datetime]
revision_id: Mapped[str | None]
previous_revision_id: Mapped[str | None]
revision_ancestor_ids: Mapped[str | None]
previous_payload_hash: Mapped[str | None]
payload_hash: Mapped[str | None]
ciphertext_hash: Mapped[str | None]
revision_written_at: Mapped[datetime | None]
write_provenance: Mapped[str | None]
source_event_id: Mapped[str | None]
conflict_policy: Mapped[str | None]
payload: Mapped[bytes]
class FincaRow(**kwargs)[source]

Bases: Base

Row in the rental_fincas table.

Models one Spanish urban property. The address column is encrypted at rest via EncryptedString because finca addresses identify the contribuyente through the Catastro stable reference and qualify as personal data under GDPR.

Variables:
  • id – Surrogate integer primary key.

  • identifier – Stable natural key for the finca.

  • address – Encrypted street address.

  • valor_catastral_total – Total Catastro value (land + construction).

  • valor_catastral_construccion – Catastro value of the construction component, used as the LIRPF art. 23.1.f amortization basis.

  • valor_catastral_revision_year – Year of the most recent Catastro revision; None when unavailable.

  • coste_adquisicion – Total acquisition cost.

  • coste_adquisicion_construccion – Acquisition cost attributable to the construction component (alternative amortization basis).

  • acquisition_date – Date the property was acquired.

  • disposal_date – Date the property was sold or otherwise disposed of, when applicable.

  • use_type – Closed enum: VIVIENDA_ARRENDADA / VIVIENDA_HABITUAL / OTRO_INMUEBLE_NO_AFECTO / LOCAL_COMERCIAL / VIVIENDA_DESOCUPADA.

  • is_stressed_area – Whether the finca sits in a declared stressed-rent area for LIRPF art. 23.2 tier resolution.

  • schema_version – Per-row schema version; defaults to "1".

id: Mapped[int]
identifier: Mapped[str]
address: Mapped[str]
valor_catastral_total: Mapped[Decimal]
valor_catastral_construccion: Mapped[Decimal]
valor_catastral_revision_year: Mapped[int | None]
coste_adquisicion: Mapped[Decimal]
coste_adquisicion_construccion: Mapped[Decimal]
acquisition_date: Mapped[date]
disposal_date: Mapped[date | None]
use_type: Mapped[str]
is_stressed_area: Mapped[bool]
schema_version: Mapped[str]
class ArrendamientoRow(**kwargs)[source]

Bases: Base

Row in the rental_contracts table.

Per-contract metadata used by the LIRPF art. 23.2 tier resolver. Tenant identifying fields, when added by future schema versions, will use EncryptedString. The current schema models only counts and flags so the row itself is not PII-bearing.

Variables:
  • id – Surrogate integer primary key.

  • finca_id – Foreign key into FincaRow.

  • contract_celebration_date – Date the contract was signed.

  • contract_termination_date – Date the contract terminated, when applicable.

  • tenant_count – Total tenants on the contract.

  • qualifying_co_tenant_count – Subset of tenants that qualify for the LIRPF art. 23.2 reduction.

  • tenant_min_age – Minimum tenant age, when known.

  • tenant_max_age – Maximum tenant age, when known.

  • tenant_is_public_admin – True when the tenant is a public administration body.

  • tenant_is_ley_49_2002_entity_with_social_use – Ley 49/2002 social- use qualifier.

  • tenant_is_imv_beneficiary – Ingreso Mínimo Vital beneficiary flag.

  • dwelling_in_public_program – Public housing program qualifier.

  • prior_contract_last_rent – Last rent under the previous contract, when known.

  • prior_contract_indexation – Indexation factor applied to the previous contract.

  • initial_rent – Initial monthly rent under the new contract.

  • is_first_rental – True when the dwelling has never been rented before.

  • rehabilitation_finished_date – Date a qualifying rehabilitation completed, when applicable.

  • lau_17_6_compliant – True when the contract complies with the Ley de Arrendamientos Urbanos art. 17.6.

  • schema_version – Per-row schema version; defaults to "1".

id: Mapped[int]
finca_id: Mapped[int]
contract_celebration_date: Mapped[date]
contract_termination_date: Mapped[date | None]
tenant_count: Mapped[int]
qualifying_co_tenant_count: Mapped[int]
tenant_min_age: Mapped[int | None]
tenant_max_age: Mapped[int | None]
tenant_is_public_admin: Mapped[bool]
tenant_is_ley_49_2002_entity_with_social_use: Mapped[bool]
tenant_is_imv_beneficiary: Mapped[bool]
dwelling_in_public_program: Mapped[bool]
prior_contract_last_rent: Mapped[Decimal | None]
prior_contract_indexation: Mapped[Decimal | None]
initial_rent: Mapped[Decimal]
is_first_rental: Mapped[bool]
rehabilitation_finished_date: Mapped[date | None]
lau_17_6_compliant: Mapped[bool]
schema_version: Mapped[str]
finca: Mapped[FincaRow]
class FincaRendimientoRecordRow(**kwargs)[source]

Bases: Base

Row in the rental_income_records table.

Per-contract per-period gross-rent ledger. The (contract_id, period_year) tuple is unique so each contract surfaces a single income record per ejercicio.

Variables:
  • id – Surrogate integer primary key.

  • contract_id – Foreign key into ArrendamientoRow.

  • period_year – Tax year the income belongs to.

  • gross_rent_received – Gross rent received during the period.

  • dias_alquilados – Days the property was actually rented during the period (0..366).

  • schema_version – Per-row schema version; defaults to "1".

id: Mapped[int]
contract_id: Mapped[int]
period_year: Mapped[int]
gross_rent_received: Mapped[Decimal]
dias_alquilados: Mapped[int]
schema_version: Mapped[str]
contract: Mapped[ArrendamientoRow]
class FincaGastoRow(**kwargs)[source]

Bases: Base

Row in the rental_expenses table.

Per-finca per-period categorised expense surface for the LIRPF art. 23.1 deductible-gasto rollup.

Variables:
  • id – Surrogate integer primary key.

  • finca_id – Foreign key into FincaRow.

  • period_year – Tax year the expense belongs to.

  • category – One of the closed expense categories (FINANCIACION_INTERESES, CONSERVACION_REPARACION, IBI_TRIBUTOS_NO_ESTATALES, COMUNIDAD, SEGUROS, SUMINISTROS, ADMINISTRACION_PORTERIA_VIGILANCIA, FORMALIZACION_CONTRATO, DEFENSA_JURIDICA, SALDOS_DUDOSO_COBRO, OTROS).

  • amount – Expense amount.

  • schema_version – Per-row schema version; defaults to "1".

id: Mapped[int]
finca_id: Mapped[int]
period_year: Mapped[int]
category: Mapped[str]
amount: Mapped[Decimal]
schema_version: Mapped[str]
finca: Mapped[FincaRow]
class FincaAmortizacionLedgerRow(**kwargs)[source]

Bases: Base

Row in the rental_amortization_ledger table.

Per-finca per-period art. 23.1.f amortización 3 % accrual with cumulative-through-year tracking. The (finca_id, period_year) tuple is unique so the ledger has one canonical entry per finca per ejercicio.

id: Mapped[int]
finca_id: Mapped[int]
period_year: Mapped[int]
dias_alquilados: Mapped[int]
basis_used: Mapped[Decimal]
amortization_amount: Mapped[Decimal]
cumulative_amortization_through_year: Mapped[Decimal]
schema_version: Mapped[str]
finca: Mapped[FincaRow]
metadata

Alembic target_metadata for autogenerate.