Planning a data migration: mapping, cleansing, trial runs and checks
A new application is only in use once the data from the old system has arrived in it — complete, correctly mapped, and in a way someone can confirm. That transfer is a project of its own, with a plan of its own.
What you will learn
In short
Plan the migration as a repeatable, automated run, not as a one-off copying exercise. Its basis is a written field mapping that states, for every target field, where the value comes from and what happens to special cases. The run is rehearsed several times on copies of the real data, each time with a reconciliation: how many records arrived, how many were rejected, and do totals and samples match?
Only when a complete trial run goes through without surprises and the business side has checked the result is a cut-over date set. Until the new application has proved itself, the old system remains available for reading — as a way back and for looking things up.
What actually moves
The first question is not a technical one: which data does the new application really need? It helps to distinguish three groups:
Then there are things that are easily forgotten: file attachments, notes in free-text fields, user accounts and permissions, number ranges. If the next invoice number in the new system starts at 1 again, that is an error nobody notices until after the switch.
Article 5 of the GDPR requires personal data to be limited to what is necessary for the purpose (data minimisation). A migration is a good opportunity not to carry over data that is no longer needed in the first place. Which retention obligations apply to your data is a question for your data protection or legal adviser; this article only puts it in context and is not legal advice.
The field mapping
The central document of a migration is the field mapping: a table that records, for each field of the new application, where the value comes from and by which rule it is transferred. How data is organised in tables and relationships is explained in Database and user interface.
| Target field | Source in the legacy system | Rule | Decided by |
|---|---|---|---|
| Customer: country | kunden.land (free text) | “Deutschland” and “D” → DE; unknown values → error list | Sales |
| Customer: legacy number | kunden.nr | carry over unchanged, unique | Project lead |
| Order: status | auftrag.status (digit 1–6) | table of old digit → new state; 5 becomes “cancelled” | Order processing |
| Address: street, house number | kunden.strasse | split where unambiguous; otherwise unchanged into the street field | Sales |
Three rules have proved their worth. First, the old identifier of every record is carried along in the new system. It is the key for reconciliation, repeat runs and queries. Second, every value the rule does not recognise ends up as an entry on an error list rather than being guessed. Third, character encoding, date formats and decimal separators are specified explicitly. Older systems often do not store text in UTF-8, and accented characters are then the first thing to arrive damaged.
Data quality and cleansing: who decides
Every legacy system contains data that does not fit the new application’s rules: duplicate customers, empty mandatory fields, free text where a selection should be, cases in states that should not exist. Technology can find and count these cases. What happens to them is a business decision, and it belongs to the people responsible for the data.
For each group of errors, there are three possible places to cleanse:
What matters is that every cleansing step is recorded. If a customer asks after the switch why their second delivery address is missing, the answer has to be findable.
Repeatable runs and reconciliation
A migration is not run once but many times: in every trial run, after every change to the rules, and on the cut-over date. It therefore has to exist as a program that always produces the same result from the same source data and creates no duplicates on a second run. The following example shows the principle with an in-memory SQLite database:
<?php
declare(strict_types=1);
$db = new PDO('sqlite::memory:', options: [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$db->exec("CREATE TABLE old_customer (nr INTEGER, name TEXT, land TEXT)");
$db->exec("INSERT INTO old_customer VALUES (1,'Meyer GmbH','Deutschland'),(2,'Huber KG','A'),(3,'Test','??')");
$db->exec("CREATE TABLE customer (legacy_nr INTEGER UNIQUE, name TEXT, country CHAR(2))");
// Mapping agreed with the business side; unknown values are rejected, not guessed.
$countries = ['Deutschland' => 'DE', 'D' => 'DE', 'A' => 'AT', 'AT' => 'AT'];
function migrate(PDO $db, array $countries): array
{
$insert = $db->prepare('INSERT INTO customer VALUES (?, ?, ?) ON CONFLICT (legacy_nr) DO NOTHING');
$rejected = [];
$db->beginTransaction();
foreach ($db->query('SELECT nr, name, land FROM old_customer') as $row) {
$land = $row['land'];
$code = $countries[$land] ?? null;
if ($code === null) {
$rejected[] = "nr {$row['nr']}: unknown country '{$land}'";
continue;
}
$insert->execute([$row['nr'], $row['name'], $code]);
}
$db->commit();
return $rejected;
}
migrate($db, $countries);
$rejected = migrate($db, $countries); // second run must not create duplicates
$source = (int) $db->query('SELECT COUNT(*) FROM old_customer')->fetchColumn();
$target = (int) $db->query('SELECT COUNT(*) FROM customer')->fetchColumn();
printf("source %d = target %d + rejected %d: %s\n", $source, $target, count($rejected),
$source === $target + count($rejected) ? 'OK' : 'MISMATCH');
echo implode(PHP_EOL, $rejected), PHP_EOL;source 3 = target 2 + rejected 1: OK and nr 3: unknown country '??'. Thanks to the unique legacy number, the second run creates no duplicates. For trial runs with changed rules, the target table is emptied before each run. The most important line is the reconciliation at the end: every source record has either arrived or is on the error list with a reason.Counting alone is not enough. Reconciliation also covers business totals that both sides can calculate — such as the sum of open items or the number of open orders per status — and samples, in which someone from the business side puts individual records in the old and new systems side by side. How to write queries with parameters safely is covered in Database access with PDO.
Trial runs and test data
A trial run only deserves the name with realistic data in realistic volume. It answers three questions: does the migration run through without errors? How long does it take? And can the business side work with the result in the new application? The third question calls for real processes: continuing work on a migrated order, invoicing an existing customer, comparing a report against last year’s figures.
Because copies of real data are used, the same data protection rules apply as in live operation. Good practice: copy only the data the trial run needs; pseudonymise or anonymise personal fields wherever the check allows; restrict access to the test environment to those involved; delete copies verifiably after the project. This, too, is context, not legal advice.
How to check whether the result matches what was agreed is described in Software acceptance.
A hypothetical example
The trial run that took too long
A weekend has been set aside for switching over an order management system. The first full trial run with a copy of the real data takes nine hours, mainly because of the file attachments, which are copied and checked one by one. Together with reconciliation, samples and a buffer, that does not fit the planned window.
The team separates out the attachments: they are transferred in advance during the week before the cut-over date, and on the day itself only those added since then follow. The second trial run shows that the remaining part fits into the weekend. Without a trial run at realistic volume, this would only have come to light on the night of the switch.
Cut-over date, switchover and the way back
- Set criteria. What has to be met for the switch to go ahead: reconciliation without unexplained differences, samples confirmed, run time within the window.
- Freeze the legacy system. From an agreed point in time, no more data is changed there, so the final run transfers a fixed state.
- Carry out the final run and reconcile it, exactly as rehearsed.
- Approve. A named person decides, based on the criteria, whether the new application goes live.
- Keep the legacy system read-only, at least until the new application has completed a full working cycle, such as a month-end close.
The way back is the part people talk about least. On the cut-over date itself it is simple: the legacy system is reopened and the new application is stopped. It gets difficult once work has been done in the new system, because those entries would have to go back. That is why it is agreed beforehand until when a way back is foreseen, and how data entered up to then is handled — by hand if necessary, using a list from the new system.
If the migration is part of a larger renewal, it often comes at the end of the road, as described in Modernising step by step. We support planning and implementation as part of our PHP modernisation work.
Common misconceptions
Further sources
- Art. 5 GDPR – Principles relating to processing of personal data text of the article, including data minimisation (para. 1(c))
- UPSERT SQLite documentation on ON CONFLICT, available since SQLite 3.24.0
- PHP: Prepared statements and stored procedures prepared statements with PDO
About the author
Fabian Lücker is managing partner of PHP Schmiede KG in Rheine and has worked in software development since 2007. He is responsible for development and design, and writes here about decisions that come up in real projects.
Is a system change coming up?
Describe which system the data comes from and which areas are to move. We will tell you what a trial run for it could look like.