$ cat przepiecie-produkcji-dzien.md

PHP to Python migration on live production: switchover day and the first 48 hours

A switchover plan that changed three times in the last two days, 40 minutes of maintenance notice and two days of fixes after moving an eighteen-year-old service from PHP to Python, from Laravel 4.2 to Django. Most of the bugs were a difference between test data and production, or between the code and the server configuration, and four we inherited from the old application.

part 2 · przepiecie 1 2

In part one I described nine weeks of preparation for migrating an eighteen-year-old service from PHP to Python, or more precisely from Laravel 4.2 to Django 5.2. It was a 1:1 port: a rewrite with no improvements along the way. I ended on what surfaced in the last 48 hours before the switchover: the old server’s configuration, which was in no repository.

The service runs in two markets: two countries, two domains, two separate databases and one shared codebase. The main market’s database holds about 800 thousand accounts.

The team was one developer with an AI assistant and, on the client’s side, a tester, a devops available for advice, and the client, who coordinated the decisions.

This post starts two days before the switchover, with a plan that was about to change. Then comes the switchover day itself, Wednesday 28 October 2026, hour by hour, and the two days after it.

This was a client project, so the material is anonymised: no name of the service and no details it could be recognised by. Some numbers are rounded.

A switchover plan that changed three times

We promised the client an hour and a half to two hours of manual work, of which 30–45 minutes would be visible to users. The rest of the day was meant for watching. That promise held. The plan for keeping it changed three times in the last two days.

Version one: a DNS change. The obvious plan: in Cloudflare, point the domains at the new server, that is, change the A records, the entries that say which IP address a domain lives at. The drawback: a change like that spreads across the world at its own pace, so for an unknown stretch of time some users would land on the old server and some on the new one, with both working on the same database.

Version two: the reserved address. The day before the switchover we finally got access to the cloud console, and it turned out the DNS records did not point at the old server but at a reserved address (Reserved IP). That is an address that belongs to the cloud account and is only attached to a machine. The AI assistant noticed it first and prepared the scenario of moving the address on its own. The client’s devops left a hint for the morning that confirmed it: this is the inbound address, you move it, and DNS is left alone.

The switchover then takes one click in the console, and the rollback is the same click in the other direction. We power the old server off but do not delete it. Its cron jobs and queue workers, which we could not reach without sudo, go down with it.

The morning of the switchover. It turned out that three other, forgotten services and an old admin panel sat on the same reserved address. For a moment it looked as if we were going back to DNS. The client settled it in two messages: those services are not needed, just do not delete them. At 08:30 we were back on version two.

The order of steps had been written down the day before in a deployment document, so that in the morning there was nothing to think about, only boxes to tick. First the IP address, then the disk, the cron jobs, the database, the keys, the test, and only at the very end switching the notice off. When the plan changed for the third time at 08:30, we had something to go back to instead of improvising under pressure.

What else was in the plan

The maintenance notice at the edge, not in the application. The edge is the server that takes traffic before the application does. Ours is Caddy, a web server that gets its TLS certificates by itself. It got one flag in its configuration: when the flag is on, everyone sees the maintenance notice, and whoever has a cookie with a secret sees the real application.

That let us walk the most important paths on the production domain and the production database before anyone else saw them. It is a dozen or so lines in a configuration file. The cheapest thing in the whole project, and the only one that let us test production on production.

The photo disk gets moved, not copied. 352 GB of photos sat on a block volume, a network disk that can be detached from one machine and attached to another. The plan: detach it from the old machine, attach it to the new one and set it to mount by itself after every restart. A clone of that disk, made earlier for testing, stayed as a spare.

The database stays the same. The same managed MySQL cluster, only with a new user on a modern authentication method, the one I wrote about in part one. No data migration on switchover day, no schema change.

Nobody gets logged out. The question whether that was possible came up right away, with the assumption that it probably was not. It was. The old application kept “remember me” in a cookie holding an identifier and a token, encrypted with the application key. We carried that key over, so the new application can read the cookie, honours it and opens its own session.

Error reporting from the first minute. The client has their own error tracker, a tool the application sends every exception to. It speaks the Sentry protocol, so hooking up the new application meant swapping one address in the configuration. It is a large part of how we got through the first day.

Two things were missing from the plan. Caddy tries to get its TLS certificates right after it starts, and until the IP address points at the new machine it cannot, so it has to be restarted once the address is moved. And a disk cannot be detached from a machine that is still shutting down. Each of these cost a few minutes and one spike in blood pressure.

Switchover day, hour by hour

The maintenance notice was up for 40 minutes, from 08:35 to 09:15 Polish time, so we kept the promise. The rest of the day was watching the logs and fixing what the tests had not caught.

Time Step What happened
08:30 Client’s decision The forgotten services on the same address stay, nobody deletes them. We go back to the reserved-address plan
08:35 Maintenance notice The flag set in Caddy on the new machine, configuration reloaded. Whoever reaches it sees the maintenance notice
08:38 Address moved One click in the cloud console. Traffic arrives at the new machine
08:40 Certificates Caddy had tried to get certificates before the address pointed at the machine, and remembered the failures. A restart, four certificates in a minute
08:45 Old server off Powered off, not deleted. The old application’s cron jobs and queue workers stopped
08:50 Photo disk Detaching failed because the old machine was still in the “shutting down” state. A second try a minute later, attached to the new machine, mounted, an entry in fstab
08:55 Environment The new application’s configuration switched from test to production: database, keys, mailing provider, cron jobs on, the password from the testing period removed. Copies of both versions kept side by side
09:00 Test on production With the cookie that bypasses the notice: login, an entry, photos from the moved disk, the admin panel
09:15 Notice off Caddy reloaded, users see the new application
09:30 First report The carousel of featured entries showed one entry instead of nine. The “at least three photos” threshold from the testing period did not fit production, where featured entries have one or two photos each. One constant and a cache rebuild
10:00 Tester on production A walk through the screens on live production
11:00 First error from the error tracker A URL with ?filter1=2;filter2=61, with a semicolon where the & should be. The old application turned “2;filter2=61” into the number 2, the way MySQL does. The new one threw an exception. Now it does what the old one did
12:00 A deploy with a 502 The first deploy of fixes recreated both containers at once, and for a dozen or so seconds users saw a 502 error. The tester noticed. Since then Caddy waits up to 60 seconds for a container that is starting, and the script recreates the containers one at a time
13:00 A question about the campaign tracker It turned out the client’s team had been using a new ad campaign tracker for two weeks. The notifications to it were written and deployed the same afternoon, from a specification pasted into the chat
15:00 Rejected payment notifications Two causes at once. When we fetched the operator’s certificate, Cloudflare rejected our request (403), and the operator’s certificate file turned out to be a chain of two certificates, not one. The operator kept resending the same notification, so after the fix the transaction booked itself
16:00 Disk The photo volume grown from 400 to 500 GB without unmounting, on a live filesystem
17:00 Review of 404s The new access log: 90 thousand requests an hour on the main market. Every 404 was an icon, a scanner looking for WordPress, or one font that was missing on the old server too. Not a single missing address with content

The server had headroom all day: the load was 1–2 on 4 vCPUs, and the processor was idle 73 percent of the time. The new machine is also about 40 percent cheaper than the old one.

Pages answered in 70–130 ms on the server and in 200–350 ms through Cloudflare.

What I will remember most from that day: almost every bug was a difference between test data and production data, or between what is in the code and what was in the server configuration.

The first 48 hours after the switchover

In the two days after the switchover we fixed fourteen things. Five are in the hourly table above and nine in the table below, where day 1 is the switchover day. None of them stopped the service: registrations and purchases ran the whole time at the level from before the switchover.

When Symptom Cause What we did
Day 1, afternoon Searching for a phrase with a hyphen ends in a 500 error MySQL full-text search treats the hyphen as an operator. The old application had the same bug, only silently Operators stripped from the phrase
Day 1, evening Users get a 403 when logging in Category pages are HTML files written once a day, at midnight. Anyone logging in from such a page sent a CSRF token, the form’s protection, that was many hours old. The old PHP did not check the token on that path A script on every page refreshes the token from the cookie
Day 1, evening “Google 400 error” in the error tracker A user clicked “deny” in the Google sign-in consent window. Not a bug, just noise Handled quietly, without a report
Day 1, evening The client landed on a URL with a strange campaign identifier and asked whether it looked dangerous The new campaign tracker checks links by passing its own identifier. On top of that, the old landing-page scripts left values in cookies encrypted with a different key, and three accounts registered with such a value in place of the identifier Validation of campaign identifiers
Day 1, evening A message draft comes back in a loop, reported as a “cache problem” The draft in the session was not cleared when a user without a purchased service landed on the purchase page. An inherited bug The draft is cleared on the redirect
Day 2, morning Entry text shows która instead of the Polish word “która” The database holds HTML entities from years ago. Laravel leaves a ready entity alone, Django encodes its & once more A filter in six templates and a review of all text columns
Day 2, morning Emails are missing the photo and the unsubscribe link A double slash in the photo path and two imports from modules that never got written, in a branch of code with no tests Fixes, and a test that renders all 20 emails on both markets and checks the links, photos and footer
Day 2, morning Wrong spacing in the category grid on the home page The old template opened a new row every three categories, the port rendered them all in one One loop fixed in the template
Day 2, morning An “Incorrect padding” entry in the error tracker My own diagnostic script, run in the production container with the tracker’s address active I now run one-off scripts with the tracker’s address empty. That one is on me

On day three, already past those 48 hours, three more things came up. The four-year-old bug was among them.

When Symptom Cause What we did
Day 3, morning The contact form “does not reach” the client’s inbox The mailing provider had kept that address on a blocklist since August 2022, after a single hard bounce, an email rejected for good. Four years of contact-form emails dropped silently. The old application sent them the same way Entry removed in the provider’s panel, a test email arrived in 45 seconds
Day 3, morning Statistics in the admin panel end with the status “failed”, with no trace in the logs The amount was a Decimal, which cannot be written to JSON directly, and the thread swallowed the exception Serialization and error logging
Day 3, later The same statistics: “Data too long for column” The result was 78 KB and the column holds 64 KB. The old application wrote 63 KB, so it only just fit. On top of that, the port also counted deleted accounts, which the old application skipped Deleted accounts skipped as in the old application, more compact JSON and a bigger column

Of those seventeen fixes, four we inherited from the old application. Three things are worth taking from the whole list.

You will not find the history of the data by clicking alone. HTML entities in the content, featured entries with a single photo, URL parameters like ?filter1=2;filter2=61: these are traces of eighteen years of production, and no seed, a data set generated for testing, has them. We had had a staging server on a copy of the live database for a month, and the tester clicked through it every day. These things got through anyway.

If I were doing this again, reviewing production data for odd values would be a separate item on the checklist, and I would do it with database queries, not only by clicking through the interface. I would look for HTML entities in content, unusual parameters in the access logs and numeric types in the results of background jobs.

And the test that renders every email template on real data I would write in the second week of the project, not after the switchover.

A new error tool shows old errors. The hyphen in search, the draft loop, the blocklist at the mailing provider and a statistics column filled almost to the brim. The old application did not report errors, so nobody saw these things. After the switchover they are finally visible.

A good share of the fixes from those three days started with an entry in the error tracker, not with a user’s report. So turn error reporting on before the switchover, and warn the client that the first week will look worse than it is.

A swallowed exception comes back. The job that computes the statistics caught every exception and only marked itself as failed, without a word in the logs. The first fix added error logging, and thanks to that we saw the second cause, the overflowing column, in the error tracker a minute after the deploy, instead of guessing.

What I would tell someone who has this ahead of them

The 1:1 port paid off, though it hurt. Every “we will fix this while we are at it” we put off until after the switchover. Because of that, every report after the switchover came down to one question: did the old application do the same? If it did, it was not a regression and we could put the fix off knowingly. If it did not, the fix was local, because we knew where to look.

Sort out access a week earlier. We got the cloud console and DNS the day before the switchover. Had we had them a week earlier, the reserved address and the forgotten services on it would have come out calmly, not at the last minute.

A deploy without downtime is a feature of its own. The first deploy of fixes gave a dozen or so seconds of 502 errors, because the script recreated both containers at once. Nobody noticed before the switchover, because on the staging server it bothered no one.

And one thing about working with an AI assistant, since the whole project was built that way. What helped most was not the writing of code, but that the assistant could answer the question “did the old application do this too?” in a few minutes, with both codebases and the logs in front of it. Decisions and clicking in the consoles stayed with the people, and that was a good place for the line.

Numbers

What How much
From the first commit to the switchover 9 weeks, 154 commits
Python code about 25.5 thousand lines in 207 files
HTML templates (pages and emails) 161
Automated tests 289
Database tables carried over with no schema change 53
Photos on the disk 352 GB
Interruption visible to users 40 minutes
Registrations and purchases on switchover day in line with the days before
Cron emails on the first morning about 3500 on the main market, about 150 on the other
New machine 4 vCPUs, about 40 percent cheaper than the old one, load 1–2
Page response time 70–130 ms on the server, 200–350 ms through Cloudflare
Fixes in the first 48 hours 14
Fixes on day three 3
Bugs inherited from the old application 4 of those 17, one of them four years old
$ prev
Migrating an eighteen-year-old service from PHP to Python, part 1: nine weeks before day zero

An eighteen-year-old PHP service, 800 thousand accounts, a framework unsupported for years and a database the cloud provider was about to force-upgrade. We rewrote it in Django and switched production over. Users saw a 40-minute maintenance notice and nobody was logged out. This part is about what we did in the nine weeks before, and why most of that work was not writing code.

← read
↑