Your Database Is the Real System: Changing It Without Losing Data

Solution Architect

3 Dec 20267 min read

Your Database Is the Real System: Changing It Without Losing Data

Code can be rewritten; years of orders and customer data cannot. How expand-and-contract changes, tested restores and reconciliation make data migration safe.

When people talk about modernising a Legacy PHP application, they usually mean the code. Code matters, but it can be replaced. If a rebuilt checkout page is wrong, you fix it and release again.

The database is different. It holds every order, customer account, invoice and stock movement, often going back many years, and your accountants, auditors and customers rely on that history. If a record is lost or altered during a data migration, there may be no copy to rebuild it from, and the loss becomes a financial, regulatory and reputational problem, not just a technical one.

In many organisations, the database is the real system and the application is just how people reach it. Sooner or later, modernising the application means changing the database too.

The decision you're facing

A database is organised by its schema: the tables and fields that decide where each piece of information lives. Legacy PHP systems that have grown over a decade often have schemas that made sense once and no longer do. One field may cram several facts together, or a customer's details may be copied into three tables that have drifted apart.

A new system usually wants a cleaner structure. The decision is how to get there while the business keeps running. The tempting answer is to design the ideal schema, run a one-off conversion over a weekend and switch on Monday.

Pramod Sadalage and Martin Fowler explain why this is hard. In their article on evolutionary database design they note that, in the traditional way of working, "changing a schema after deployment resulted in painful data migration problems". They also point to the complication that matters most for older systems: when many applications and reports read the same database, "These problems grow horns and big sharp teeth when you have a shared database, which may have many applications and reports using it."

That describes a typical Legacy PHP database: besides the main application, there may be finance reports, a nightly export to an ERP and a spreadsheet someone in sales connects to directly.

What goes wrong when it's done carelessly

These are potential outcomes, not documented incidents, unless stated.

  • Records go missing without anyone noticing. A conversion that cannot handle an unusual value, such as an old order with a blank field, may skip that row. Nothing crashes; the gap surfaces weeks later in an audit.
  • Records change meaning. Dates shift by a time zone, amounts lose decimal places, or two similar customers are merged into one. The totals look plausible, which hides the damage.
  • Reports break or, worse, keep running with wrong numbers. A renamed field stops a report outright. A field that still exists but no longer holds what the report expects is more dangerous, because nobody is warned.
  • The old and new applications disagree. While both run, a customer may see one balance in the old portal and another in the new one, and nobody can say which is right.
  • There is no way back. If the old structure is dropped on cutover night and a problem appears on Tuesday, going back means restoring a backup and losing everything entered since.

Even that fallback assumes the backup works. In January 2017, an engineer at GitLab accidentally deleted data from its primary production database. GitLab's post-mortem explains that when the team looked for its regular backups, "the process of both finding and using backups failed completely". The backup job had been failing, and the email alerts about it were being rejected, so nobody knew. GitLab recovered from a snapshot taken about six hours earlier and estimated that roughly 5,000 projects, 5,000 comments and 700 new user accounts were affected by the lost changes. This was not a migration, but it shows the gap between taking backups and being able to restore them.

The defensive approach to data migration

Changing a database safely rests on three ideas.

1. Change the structure in additive, reversible steps. The pattern is called parallel change, also known as expand and contract. In an article on Martin Fowler's site, Danilo Sato describes it as a way "to implement backward-incompatible changes to an interface in a safe manner, by breaking the change into three distinct phases: expand, migrate, and contract." He notes that most database changes of this kind follow it.

In plain terms:

  • Expand. Add the new structure alongside the old one. Nothing is removed, so everything that reads the database keeps working.
  • Migrate. Convert the data into the new structure, then move applications, reports and exports across one by one.
  • Contract. Only when nothing uses the old structure, remove it.

Sadalage and Fowler call the middle stretch a transition phase: "a period of time when the database supports both the old access pattern and the new ones simultaneously." They are candid that it adds complexity, and that ending it can take a couple of months in some organisations and years in others. The payoff is that, until the contract step, going back usually means pointing the application at the old structure again, not restoring last night's backup. Sato's article puts it simply: the pattern "lowers the risk of change by allowing you to migrate clients and to test the new version incrementally."

2. Rely only on backups you have actually restored. An untested backup is a hope, not a safety net. Before any significant structural change, restore a recent backup into a separate environment, confirm it is complete, and note how long it took: that is how long your business would wait in a real emergency. After its outage, GitLab's own list of improvements included automated testing of database backup recovery and assigning an owner for data durability.

3. Prove the data matches with reconciliation counts. "It looks fine" is not evidence. Before and after each step, compare measures the business understands: customers, orders per month, total invoiced per year, stock per warehouse. If the old and new structures disagree, stop and find out why. Agree in advance who signs off the numbers, ideally someone in finance or operations as well as the engineers.

Where Node.js fits, and where it doesn't

Node.js can be a good home for a rebuilt application, depending on what it does, and a well-run Node.js project can track database changes as part of normal releases rather than as manual one-off scripts.

But no language protects your data. Parallel change, tested restores and reconciliation are disciplines, not framework features. A Node.js system that drops an old table on day one is as risky as a PHP one. The new stack brings its own responsibilities: database access controls, care with the libraries that talk to it, and monitoring someone actually watches.

There will also be a period when the old PHP application and the new system both use the same data. Keeping two separate databases in step during that time is a hard problem of its own and deserves its own plan.

What this means for your organisation

Ask these questions of your own system:

  • Do you know every application, report, export and person that reads your database directly, not just through the main application?
  • When did you last restore a backup into a separate environment, and how long did it take?
  • Who would notice if 2% of last year's orders disappeared, and how?
  • Which business totals would your finance team accept as proof that the data is complete?
  • For each planned structural change, what is the way back, and on what date does it stop being available?
  • Who owns your data's safety during the project?

Recommended next step

Before anyone redesigns your schema, map who uses your data: every table that matters to the business, which applications, reports and people read or write it, and which totals would prove it is intact. Then restore your latest backup somewhere safe and time it. Together, these tell you how risky a structural change is and whether your safety net is real. Our Free Security and Modernisation Review can help you start: it looks at what your application depends on, where it is exposed, and the safest route to a secure, modern platform.

Free Security and Modernisation Review

Sketchmonk is a security-led software engineering company. Our engineers have built software for a national defence research lab, global cyber security vendors and security-first product companies, with 16+ years of engineering behind them. In a free Security and Modernisation Review, we look at the application you run today: where it is exposed, what it depends on, and the safest route to a secure, modern platform. Want to see how we have handled problems like yours? Ask for a case study in the same email. There's no obligation to proceed.

Request your free Security and Modernisation Review

Build with us. Deploy with us. Win with us

We don't deliver projects and walk away. We build with you, inside your mission.

Book a strategic consultation