Evolving Your Schema with Migrations
Data outlives code, and its shape changes over time. Migrations let you evolve your database safely and repeatably - larzmigrate does it over any database, with drift detection.
Your data lives longer than any single version of your code, and its structure inevitably changes - you add a field, rename a column, restructure. Doing that by hand, differently on each environment, is how data gets broken. Migrations are the disciplined answer: versioned, repeatable changes to your schema. larzmigrate provides them over any DB-API database or larzdb.
Why migrations exist
Migrations turn schema changes into ordered, version-controlled, repeatable steps - so every environment ends up in the same known state.
| Without migrations | With migrations |
|---|---|
| Hand-editing each database | One defined change, applied everywhere |
| Environments drift apart | Dev, staging, prod stay in sync |
| 'What state is prod in?' | A recorded, ordered history |
| Risky, irreversible edits | Reversible up/down steps |
A migration is just a defined change (and its reverse) with an order. Run them and any database catches up to the current schema, predictably.
Up, down, and drift detection
Each migration has an 'up' (apply) and a 'down' (roll back), and larzmigrate tracks what's been applied - detecting if a migration was changed after the fact (drift).
Evolve safely
- Define changes as migrations - not hand-edits per environment.
- Up and down - apply and roll back predictably.
- Drift detection - checksums warn if an applied migration changed.
- Any backend - DB-API databases or larzdb.
๐ฏ Key takeaways
- Data outlives code and its shape changes - migrations make schema changes safe and repeatable.
- A migration is an ordered, version-controlled change (with a reverse) applied to every environment.
- larzmigrate provides up/down migrations with checksum drift detection - warning if an applied migration was edited.
- It works over any DB-API database or larzdb, so the discipline covers embedded and server alike.
Frequently asked
Do I really need migrations for a small project?
Even small projects benefit, because the moment your data structure changes after you have real data, you face the question of how to update it without losing or breaking that data - and doing it ad hoc, differently across your machine and production, is exactly how data gets corrupted or environments drift apart. Migrations make each change a defined, ordered, repeatable step with a recorded history, so any environment can be brought to the current schema reliably. It's a small amount of discipline that prevents a category of painful, sometimes irreversible bugs. For a throwaway script it may be overkill, but for anything with data you care about and will change over time, migrations are worth it early.
What is 'drift' and why does the checksum matter?
Drift is when an already-applied migration gets edited afterward, so the migration file no longer matches what was actually run on your databases - meaning different environments may effectively have different schemas even though they claim the same migration history. This causes maddening 'works here but not there' bugs. larzmigrate records a checksum of each migration when it's applied, so if you later change that migration's contents, it can warn you that what's recorded doesn't match the current file. That guardrail catches a subtle, common mistake - retroactively editing history - and pushes you toward the correct pattern of adding a new migration for further changes rather than altering an applied one.
Related lessons
Injection-Safe Queries with larzsql
SQL injection is one of the oldest and most damaging bugs - and it's still everywhere. larโฆ
Data & Storage with the Larz StackCaching for Speed
The fastest work is the work you don't repeat. Caching stores expensive results so you canโฆ
Building with the Larz StackData & Storage โ Persisting Without a Server
Most apps don't need a database server on day one. This family gives you crash-safe storagโฆ