Lease Ledger Migration Verification Script
Generate an automated Python validation script to reconcile rent roll records and financial ledger integrity across database migrations.
Use when migrating property management databases, tenant ledgers, or commercial leases between legacy and cloud systems. It creates test fixtures, discrepancy audit queries, and verification assertions.
Role: Lead Database Data Engineer specializing in commercial real estate financial data migrations.
Context
- Legacy source database connection: {{source_erp_database}}
- Destination tenant table: {{target_tenant_table}}
- Destination accounting ledger table: {{ledger_entry_table}}
- Maximum allowed financial rounding delta: {{discrepancy_tolerance_amount}}
- Verification cursor batch size: {{validation_batch_size}}
- Output log format: {{reporting_output_format}}
Task
Write a robust Python validation script that extracts financial balances, recurring lease escalations, and security deposits from {{source_erp_database}}, comparing them against {{target_tenant_table}} and {{ledger_entry_table}} to assert 100% data fidelity post-migration.
Method
- Initialize database connection pools with read-only credentials for source and target databases.
- Formulate parameterized aggregation queries extracting total balance, prepaid rent, and active lease count grouped by property ID.
- Execute chunked validation reads using {{validation_batch_size}} to manage memory consumption.
- Compare ledger sums per lease, flagging discrepancies that exceed {{discrepancy_tolerance_amount}}.
- Validate foreign key consistency between {{target_tenant_table}} and {{ledger_entry_table}} records.
- Generate an automated summary matrix tracking matched records, unmatched IDs, and variance magnitudes.
- Format discrepancy logs and summary data according to {{reporting_output_format}}.
Constraints
- MUST run purely read-only queries with zero mutations on source or destination tables.
- MUST NOT load entire financial ledgers into memory at once; streaming cursors must be used.
- Currency computations MUST use Python Decimal types rather than standard IEEE floating-point.
- Script execution must exit with status code 1 if any discrepancy surpasses {{discrepancy_tolerance_amount}}.
Output format
A standalone Python 3 script containing:
- Imports, CLI Arguments, and Connection Setup (max 30 lines)
- SQL Reconciliation Queries and Aggregation Logic (max 50 lines)
- Batch Execution Engine and Assertions (max 60 lines)
- Report Exporter in {{reporting_output_format}} (max 40 lines)
Self-review
- Ensure floating-point arithmetic is completely replaced with Python's Decimal class.
- Confirm that database connection handlers properly close cursors and pools on SIGINT or unhandled exceptions.
- Check that discrepancy reporting surfaces exact property IDs, tenant IDs, and ledger line numbers.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.