Importing data from an external file is one of the most stressful actions a user can take in personal finance software.
A user exports a CSV statement from their bank or previous expense tracker containing 800 transactions spanning two years. They tap "Import" in your app.
Now consider what happens when something goes wrong:
- What if row 350 contains a corrupted date format like
31/02/2026? - What if the user accidentally imported their spouse's business accounts into their personal ledger?
- What if the user imports the same file twice, creating 800 duplicate entries?
In many applications, the result is an accounting nightmare: 349 rows are inserted into the database, the import crashes on row 350, and the user's ledger is left in a half-corrupted state. The only way to fix it is to manually locate and delete 349 transactions one by one!
During Hardening Sprint H2 (docs/HARDENING_SPRINTS.md:H2, commit fd8e2a9), we introduced a two-tiered safety net: Pre-Import Database Snapshots and 1-Tap Rollbacks.
Here is how FinLedger guarantees that importing external data can never corrupt your ledger.
The Half-Import Disaster
flowchart TD
subgraph HalfImportChaos["The Unprotected Import Anti-Pattern"]
User["User Imports Bank_Statement.csv (800 Rows)"] --> Step1["Rows 1 to 350 Inserted Successfully"]
Step1 --> Error["Row 351: Parsing Error / Invalid Currency Symbol"]
Error --> Crash["Unhandled Exception! Import Process Aborts."]
Crash --> PoisonedDB["The Poisoned Ledger:
1. 350 new rows are mixed into existing database.
2. Account balances permanently altered.
3. No way to identify which transactions came from the import!
4. User forced to wipe app or manually delete 350 rows."]
end
Without transactional boundaries, an interrupted import leaves poisoned data behind.
Even if the import succeeds without technical errors, human error is always possible: the user might realize five minutes later that they selected the wrong CSV file from their Downloads folder.
Tier 1: Atomic Database Transactions (withTransaction)
The first layer of defense is ensuring that an import is strictly all-or-nothing.
In FinLedger's CsvImportEngine.kt, the entire parsing and insertion process is wrapped inside Room's atomic database.withTransaction block:
flowchart TD
Start["User Selects CSV File"] --> TxStart["Open Room withTransaction Block"]
TxStart --> Validate["Parse & Validate All Rows in Memory"]
Validate --> Check{"Did ANY row fail validation
or throw an exception?"}
Check -->|YES: Failure Detected| Rollback["ATOMIC SQLITE ROLLBACK:
All temporary inserts discarded immediately.
ZERO rows committed to disk."]
Check -->|NO: 100% Valid| Commit["Commit All Rows to Disk Atomically:
Account balances updated simultaneously."]
Rollback --> ErrorUI["Display Clean Diagnostic Error:
'Row 351: Invalid date format. Import aborted without changes.'"]
If row 351 fails:
- The SQLite engine triggers an immediate rollback.
- Not a single row from the CSV is written to disk.
- Your existing ledger balances remain 100% untouched.
- The UI displays a human-readable diagnostic error pointing to the exact line number that needs correction.
Tier 2: Pre-Import Snapshots and 1-Tap Undo
Atomicity prevents technical crashes, but what about human error?
What happens when an import is technically valid, but the user imported a 500-row file into the wrong bank account?
In Sprint H2, we introduced Pre-Import Checkpoints:
flowchart TD
subgraph SnapshotPipeline["FinLedger 1-Tap Rollback Architecture"]
UserSelects["User Confirms Valid Import"] --> CreateSnapshot["1. Create Instant Pre-Import Snapshot:
Fast SQLite backup to local sandboxed cache:
checkpoint_pre_import.db"]
CreateSnapshot --> ExecuteImport["2. Execute Atomic Import into Master Ledger"]
ExecuteImport --> Banner["3. Mount Reversible Import Banner on Dashboard:
'Imported 450 transactions from Chase_Nov.csv.'
Action Button: [ Undo & Rollback ]"]
Banner --> UserAction{"User Decides"}
UserAction -->|Dismiss or 24h Passes| Evict["Checkpoint Evicted & Reclaimed"]
UserAction -->|Taps Undo & Rollback| RestoreSnapshot["Restore Master DB from Snapshot:
Database swapped atomically in < 40 ms.
Ledger restored to exact state before import!"]
end
How the Snapshot Operates Under the Hood
- Before committing the import, FinLedger copies SQLite's active write-ahead log (WAL) into a sandboxed cache file:
checkpoint_pre_import.db. On modern Android hardware, this file snapshot completes in under 25 milliseconds. - Once the import completes, a discreet banner appears at the top of the Ledger screen: "Successfully imported 450 transactions from HDFC_Statement.csv. [Undo & Rollback]".
- If the user notices something wrong, tapping "Undo & Rollback" triggers a clean database restoration. The checkpoint database overwrites the master database, and the ledger returns to the exact microsecond before the file was touched.
- If the user is satisfied, dismissing the banner or letting 24 hours elapse automatically purges the snapshot, freeing device storage.
Key Takeaway
Financial software should never put its users in a position where a single misclick or a corrupted file destroys months of careful budgeting.
By combining atomic SQLite transactions with automatic pre-import checkpoints and 1-tap rollbacks, FinLedger ensures that bringing your external financial history into the app is completely fearless.
FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.
Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app

Top comments (0)