- Nov 20, 2026
- 4 min read
Migrating 395,931 Rows and a Zigbee Network Without Re-Pairing
The new platform had to start with the old history, not an empty chart. There were two sources: the old dashboard’s Docker volume and a standalone ingester’s database on the host. They had recorded the same MQTT traffic for months, with gaps in different places.
Step 0: copy a live database the right way
Both databases were live, in WAL mode. Copying readings.sqlite with cp is
the classic mistake: transactions that haven’t been checkpointed yet live in
readings.sqlite-wal, and a plain copy can miss them. So the inputs were
consistent snapshots made with SQLite’s own backup:
sqlite3 live.sqlite ".backup 'snapshot.sqlite'"
sqlite3 -readonly snapshot.sqlite 'PRAGMA integrity_check;'
Step 1: a migration that refuses to overwrite
scripts/migrate-history.cjs does four things:
- Creates a new destination and applies the v2 schema.
- Attaches each legacy database read-only.
- Imports everything inside one transaction.
- Refuses to run if the destination already exists.
The last one is deliberate. A migration you can accidentally re-run over live data is a migration that will, one day, be re-run over live data.
npm run migrate:history -- data/readings.sqlite \
imports/docker-volume.sqlite imports/current-host.sqlite
Step 2: what counts as a duplicate
The obvious key, the row id, was useless: each id was only unique within its own file. What both stores shared was the MQTT message itself. So a row is a duplicate when topic, metric and value match within a two-second window.
Result:
| Source | Rows taken |
|---|---|
| Docker-volume snapshot | 395,931 |
| Host snapshot (unique rows only) | 1 |
| Total, June to September | 395,932 |
The second store had recorded almost nothing the first hadn’t. Good to know, and only knowable because the dedup rule was about the data, not the ids.
Step 3: add columns, don’t rewrite history
The legacy readings columns stayed exactly as they were. V2 adds nullable
metadata next to them: device_id, signal, unit, source. Old queries
still work, old rows are mapped to new device ids by config, and
ensureSchema() is additive and idempotent, so the ingester runs it at every
startup safely.
Point-in-time audit the day after, every store checked with integrity_check:
| Store | Rows | Integrity |
|---|---|---|
| v2 primary | 399,815 | ok |
| old Docker volume | 399,069 | ok |
| old host ingester | 318,419 | ok |
The old stores were kept as extra fallback copies.
The Zigbee part: what actually holds a network
Zigbee2MQTT had been running on a different machine with a different broker. If you start it with an empty data folder, it can create a new network identity, and every sensor has to be paired again.
What has to move together:
database.db(the paired devices),coordinator_backup.json(network identity and key),configuration.yamlandstate.json,- and the same physical Sonoff coordinator.
Copying only configuration.yaml isn’t enough. I stopped Zigbee2MQTT on the old
machine, rsynced the whole data folder, and didn’t start the new one until the
copy was complete.
Three more details made it clean:
- No second broker. Zigbee2MQTT is just an MQTT client. The new compose file contains only Zigbee2MQTT, pointed at the existing EMQX through environment variables, which override the stale broker address in the restored config.
- A stable device path. The dongle is mapped by
/dev/serial/by-id/..., not/dev/ttyUSB0, which can change after a reboot. - A guard rail.
make zigbee-uprefuses to start unlessdatabase.dbandcoordinator_backup.jsonexist. A deliberately fresh network needs an explicitALLOW_NEW_ZIGBEE_NETWORK=1.
Zero devices re-paired.
One Git lesson from the same week
The checkout the containers were running from was behind the remote, and dirty
because live database files change constantly. Pulling or resetting there could
have overwritten runtime state. Changes went through a separate Git worktree
based on origin/main instead. Don’t use destructive Git commands to make a
live deployment folder look clean.
Takeaways
- Snapshot with
.backup, nevercp, while SQLite is live. - Deduplicate on what the sources share, not on local ids.
- Additive schemas keep old queries and old rows working.
- A Zigbee network is files plus a dongle. Move all of it, and make starting a fresh network a deliberate act.
- Telemetry history and Zigbee state are the two things you can’t rebuild from source. Back up both, separately.
Next in this series: The Voltage Divider Is Where Software People Get Humble
Part 2 of 8. Start with the series overview.