In my last post I said the database behind Koshly, my offline expense tracker, was ready for sync. A reader showed me it wasn’t.
Prikshit pointed out in the comments that I was using updatedAt to decide which edit wins. Phone clocks drift. If my phone is five minutes ahead, my old edit can beat your new one. Deletes have the same problem: a phone that was offline for a week can bring a deleted transaction back.
Prikshit was right. So I fixed the schema, and I thought this was a good time to show the whole thing: every table in Koshly, how they connect, and why I made each choice.
The whole database
Koshly has 15 tables in one SQLite file, built with Drift. Here are the ones that point to each other:
![]()
The other six (seen_messages, import_rules, upcoming_payments, notification_log, notifications, user_events) and app_settings stand on their own, with no foreign keys. I’ll get to them below.
I think of the tables in four groups:
- Your money: transactions, accounts, categories, budgets, people
- History: budget versions, person aliases
- SMS auto-import: imports, seen messages, rules, the notification log, upcoming payments
- The app itself: notifications, user events, settings
Your money
These are the five tables that would go to a server if Koshly ever syncs. Each one has a number id for joins inside the phone, and a uuid that would be the same on every phone.
transactions is the centre. Every row points to a category, and maybe to an account and a person. A transfer between your own accounts uses two account columns: accountId is where the money left, toAccountId is where it went. Money you gave to or got from a friend points to people.
Amounts are saved in paise as an int. ₹120.50 is 12050. Never a double, so nothing is ever off by a paisa.
Types like expense or income are saved as text, "expense", not as a number. If I ever reorder the enum in code, the old rows still mean the same thing.
Nothing gets lost
A finance app can’t lose history. If you delete the “Food” category, your old food transactions still need a category. So almost nothing in Koshly is really deleted:
- Accounts, categories and people are hidden. They have an
isActiveflag. Turn it off and they’re gone from the screens, but old transactions still point to them. - Budgets are archived.
archivedAtgets a date, and the budget stops showing. - Budget limits are never changed. When you change a limit from ₹10,000 to ₹12,000, Koshly adds a new row to
budget_versionswith the date it starts. Old months are still judged against ₹10,000.
| |
What you owe a friend is never saved anywhere. It’s added up from your gave and got transactions every time. A number that’s calculated can’t go wrong. Account balances are still saved, and that’s on my list to fix before sync.
The SMS tables
Koshly reads your bank SMS and adds payments for you. That needs more tables than the money itself.
One payment often sends two or three alerts: one from the bank, one from the UPI app. sms_imports keeps one row per real payment and merges the alerts into it.
If you delete a transaction Koshly added, the import stays but its transactionId becomes null. That way, if a late SMS for the same payment comes in, Koshly knows you already deleted it and doesn’t add it back.
seen_messages keeps a fingerprint of every SMS already imported. The same SMS can arrive twice: once when it comes in, and again when Koshly reads the inbox. But two metro tickets for the same amount have the same text too, so a match only counts if they arrived close together.
import_rules remembers your corrections. If you change “PAYTM*SWIGGY” to Food once, the next one goes to Food on its own. The rule points to the category by its uuid, not its id, so it would still work on another phone.
upcoming_payments holds EMIs and bills that reminder SMS say are coming, so they show up in the Upcoming tab.
The app’s own tables
notifications keeps every notification Koshly showed. Each one has a unique key, like budget:3:2026-11-01:80 for “budget 3 hit 80% this month”. The app and the background job both try to show alerts, and sometimes at the same moment. The unique key means only one of them wins.
user_events notes when you open the app, so the daily summary comes at a time you usually check. It never has amounts or names, and it never leaves the phone.
14 versions
Koshly’s database is on version 14. Every time I added a table or a column, the version went up and the app ran an upgrade step on the next launch.
But I learned the version number can lie. While building auto-import, my dev builds used versions 4 to 7 for other changes, and I renumbered them later. A backup from one of those builds says “I’m on version 7” but doesn’t have the version 7 tables. So the upgrade skips it, and the app crashes on a missing table.
So now, every time the database opens, Koshly checks what’s really in the file against what the code expects, and adds anything missing. On a healthy file, that’s a few quick reads.
One more lesson: right after an update, the app and a background job can open the database at the same moment. Both try to add the same index, and one fails. Now every index is created with IF NOT EXISTS.
The fix from the comments
Back to Prikshit’s comments. The five money tables now have two new columns:
| |
hlc is a hybrid logical clock. It starts from the phone’s time, but every time a phone sees a change from another phone, it moves its own clock past it. So even if my clock is wrong, my next edit is always “after” anything I’ve already seen. When two phones edit the same transaction, the higher hlc wins, not the later updatedAt.
I thought about a simple version counter first. But as Prikshit said in a second comment, a counter only tells you two phones disagree. You still need another rule to pick the winner. An HLC does both.
deletedAt keeps a deleted row as a marker instead of removing it. A delete is just another edit: it gets a new hlc and goes through the same comparison. So a phone that was offline for a week and still has the old row can’t bring it back.
This also means I don’t need a separate “not synced” flag. Any row with an hlc newer than the last one sent to the server hasn’t been sent yet.
Right now these columns are only in the schema. Nothing writes to them yet. That comes with sync. But adding a column is easy today, and much harder once people have years of data.
This isn’t bulletproof. If two phones change different fields of the same transaction, one change still loses. And account balances are running totals, so they’ll need a different fix than a clock. But Koshly doesn’t sync yet, and I’ll build it when people ask for it. Until then, there’s time to get these right.
Where it is now
15 tables, one file, on your phone. Most of what’s in there is not your money but the work around it: reading SMS, remembering corrections, not saying the same thing twice.
And thanks to Prikshit’s comments, the sync plan is better than the one I wrote last week. If you see something else I got wrong, tell me.
If you want to see the app these tables run, try Koshly on Google Play.
