Migrating a login system when the usernames look like ๐๐๐๐
I have moved a platform between clouds and rewritten its backend in another language. Neither was the hard one. The hard one was moving its database, because of what people had been allowed to call themselves.
How it got that way
When the platform started, the CTO chose Firebase Realtime Database for everything. It stores whatever you give it. Nobody added rules on top, so a username could be anything at all:
| Username | What is wrong with it |
|---|---|
xy |
Two characters |
r@m@! |
Special characters |
๐๐๐๐ |
Styled Unicode letters that only look like โramaโ |
The first two are examples of the kind. The last one is real.
Why PostgreSQL makes it a problem
A relational database is where you finally get to say what a username is: how long, which characters, and that no two people share one. The moment you switch those rules on, the existing users break them.
That leaves two bad options:
- Keep the rules, and reject the old accounts. Those people can no longer log in.
- Drop the rules, and import the mess. You have moved databases and fixed nothing.
And the one requirement that did not bend: every existing user had to keep logging in.
What we shipped
The CTO wanted every user to log in exactly as before, with the username they already had. So the old database stayed, as a fallback for login:
- Look for the user in PostgreSQL.
- If they are not there, look in Firebase.
- Log them in either way.
It worked, and nobody was locked out. It also meant the migration never finished:
- Two databases to run and pay for, with no date for switching the old one off.
- Two answers to โwho is this user?โ, which every later feature has to deal with.
- The bad usernames are still there. They moved nowhere and were fixed by nothing.
I built it, and I think it was the wrong call.
What I would do as the CTO
Clean the usernames once, then help people find their new one:
- Clean up every username by rule, and keep a record of what each one became.
- Run PostgreSQL only, with validation switched on.
- Add a โForgot my username?โ step to login: the user enters their email.
- Email them their cleaned-up username.
Some people would have to look up their username once. In exchange there is one database, the rules hold for everyone, and the migration is over.
What I took from it
Validation is cheapest on the day you create the field.
Every month without a rule adds accounts that will break it later. By the time the rule is obviously needed, enforcing it means choosing which users to inconvenience.
I would rather inconvenience a few people once than carry two databases for good.