Moving from Laravel to Go without a single database migration

This note covers moving from Laravel to Go without moving a single byte of data. When you move a live product's backend from one language to another, the scariest part isn't the code, it's the data. If the new version wants its own schema, you need a migration night that's hard to undo, and if something goes wrong halfway, both versions break.
When moving from Laravel to Go at Sima, we set one hard rule: Go writes exactly what Laravel wrote. The same tables and columns, the same JSON shapes, the same file paths, the same session and Redis keys, the same password hashes. As a result, both versions ran side by side on one database for a while, and the final switch needed no data migration at all.
It sounds simple on paper, but the devil is in the bytes. These are the traps we fell into.
1. The JSON Laravel writes isn't the JSON Go writes
Laravel's model casts (array, json) use json_encode without flags. That means it turns / into \/ and writes every non-ASCII character, all Persian letters included, as \uXXXX:
// PHP: json_encode(['title' => 'سلام', 'url' => '/a'])
{"title":"سلام","url":"\/a"}
// Go: json.Marshal(map[string]string{"title": "سلام", "url": "/a"})
{"title":"سلام","url":"/a"}
For a jsonb column Postgres sees both as the same. But json columns keep the bytes as they are. Then string comparison, content hashes, diffs between versions and any code working on raw text fall apart. Go's encoding/json has one more difference: it escapes <, > and &, and PHP doesn't.
Our fix was a small package rebuilding json_encode with all its flags (JSONEncode, JSONEncodeUnicode and the rest). Wherever Go writes a column Laravel filled through a cast, it uses that.
2. Key order
A PHP array keeps insertion order, but a Go map has no order and json.Marshal sorts the keys. So settings Laravel stored as {"b":1,"a":2} became {"a":2,"b":1} after one save from Go. Same meaning, but it breaks diffs and comparison tests, and sometimes display order. Where order mattered, we built an Ordered type that behaves like a PHP array.
3. A cache filled by serialize()
Laravel stores cache values in Redis with serialize(), like s:5:"hello"; or a:2:{...}. If both versions share a cache, or the cache shouldn't be rebuilt after the switch, Go has to read and write that format. We wrote a PHPSerialize covering only what was actually used: scalars, lists and maps. We left objects alone.
4. Sessions and passwords
A user who signed in through Laravel and whose next request reaches Go must not be signed out. So Go reads the same cookie and the same session keys. Laravel's web guard keeps the user under a key like login_web_<sha1 of the class name>, so Go keeps that exact string.
Password hashes are bcrypt with PHP's $2y$ prefix. The golang.org/x/crypto/bcrypt package accepts that prefix without fuss, so no user had to change a password. There was one footnote for new hashes: Go makes them with $2a$, so we switch the prefix back to $2y$ to keep the column uniform, and so Laravel can still read them if we ever roll back.
5. Dates: Carbon isn't time
This one involved money. Carbon's addMonthsNoOverflow takes January 31st plus one month to the end of February. Go's t.AddDate(0, 1, 0) normalises the date and lands on March 2nd or 3rd. For subscription renewals, that's a few days' difference. So we rewrote the few Carbon functions actually in use, exactly.
How we made sure: the twin
Our main tool was to run Laravel and Go side by side on a copy of the database, send both the same requests, and compare two things: the HTTP response and the rows each one wrote. Every difference was either a bug or a difference we accepted deliberately and recorded. Nearly every trap above was found this way, not in code review.
Summary: Laravel to Go
- Define the data contract at the byte level, not at the level of "meaning".
jsonversusjsonbshows the difference perfectly. - Every implicit behaviour of the old framework, such as casts, serialize, guards and Carbon, is part of the contract and has to be rewritten explicitly.
- Twin comparison on real traffic finds more bugs than any checklist.
- The reward is a reversible switch. If the new version misbehaves, you route traffic back and the data is untouched.
Two of the problems after moving from Laravel to Go got their own notes: a deadlock between an advisory lock and the pool and GOMAXPROCS in a container. For PostgreSQL we used pgx.
Comments
Sign in to leave a comment.
Sign inLoading…



