ArticleslgStudy

computer science

Schema migration

Schema migration is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Schema migration rather than just read about it. In short: In software engineering, a schema migration (also database migration, database change management) refers to the management of version-controlled, incremental and sometimes reversible changes to relational database schemas. A schema migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version.

Key takeaways

  • Schema migration belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Schema migration to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Schema migration from memory before moving on to harder problems.

Reference excerpt

In software engineering, a schema migration (also database migration, database change management) refers to the management of version-controlled, incremental and sometimes reversible changes to relational database schemas. A schema migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version. Migrations are performed programmatically by using a schema migration tool. When invoked with a specified desired schema version, the tool automates the successive application or reversal of an appropriate sequence of schema changes until it is brought to the desired state. Most schema migration tools aim to minimize the impact of schema changes on any existing data in the database. Despite this, preservation of data in general is not guaranteed because schema changes such as the deletion of a database column can destroy data (i.e. all values stored under that column for all rows in that table are deleted). Instead, the tools help to preserve the meaning of the data or to reorganize existing data to meet new requirements. Since meaning of the data often cannot be encoded, the configuration of the tools usually needs manual intervention.

Risks and benefits Schema migration allows for fixing mistakes and adapting the data as requirements change. They are an essential part of software evolution, especially in agile environments (see below). Applying a schema migration to a production database is always a risk. Development and test databases tend to be smaller and cleaner. The data in them is better understood, and if everything else fails, the amount of data is small enough for a human to process. Production databases are usually huge, old and full of surprises. The surprises can come from many sources:

Corrupt data that was written by old versions of the software and not cleaned properly Implied dependencies in the data which no one knows about anymore People directly changing the database without using the designated tools Bugs in the schema migration tools Mistakes in assumptions how data should be migrated For these reasons, the migration process needs a high level of discipline, thorough testing and a sound backup strategy.

Migration strategies In the steady state, one version of an application only understands one version of a schema. So the most basic strategy is to shut down the application, execute the schema migration, and then start the newer version of the application. While simple, this strategy causes a downtime. Depending on the criticality of the system and its usage patterns, downtimes of various duration may be tolerated, but in some cases none may be tolerated at all. In those cases, one of the following zero-downtime strategies may be used.

Dual writing These are the general steps of dual writing (also called double writing):

Prepare the schema so that it can hold data in both the old and new formats. This might mean adding a new version of a column or a table, without affecting existing data. Deploy a new version of the application which writes data in both the old and new formats (hence the name dual writing). It's important to ensure consistency of these writes. After this point, all newly written data will exist in both old and new formats. Execute a backfill in the database: copy data from the old format to the new format that existed previously, and hasn't been updated recently, so it's not dual written yet. After this point, the database has a complete replica of the data in both the old and new formats. Deploy a new version of the application which switches to reading data in the new format, and stops dual writing. In distributed systems, it's important to switch the reading path before stopping dual writing, so this step may be divided into two. Remove the old format data from the schema.

Dual reading Dual reading (also called double reading) is similar to dual writing, with the following steps:

Prepare the schema so that it can hold data in both the old and new formats. Same as above. Deploy a new version of the application which tries to read both the old and new formats (hence the name dual reading), and works with whichever format is currently present. Deploy another version of the application which stops writing the old format and starts writing the new format instead. Everything should continue working normally as the dual reading will recognize that it has to read the new format for newly written rows. Execute a backfill in the database: for all data that was written in the old format, move it over to the new format. Deploy an application change once more that stops reading the old data. Remove the old format data from the schema.

Dual reading and writing In this combined approach, the application is changed to both dual read and dual write. Since both individual strategies ensure that the database can remain online without interruption, the combined approach achieves the same as well. This strategy allows for more fine grained control over the backfill, which can be divided into smaller batches, and feature flags may be used to toggle both the reading and writing paths more freely and separately from each other. This can also be useful when regular dual writing alone can't be guaranteed to happen in consistent transactions.

Comparison All strategies above achieve a zero-downtime migration. Dual writing has the advantage that the old and new version of the data live side by side, so comparisons can be run between them to ensure consistency before committing to the new format. This, however, comes at the cost of doubling the storage requirements. With dual reading only one version of every piece of data exists at any point in time, so there's no increase in storage requirements. The combined approach allows doing the backfill in smaller batches so the storage increase can be controlled, but this comes at the cost of added complexity.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Schema migration

Start with the simplest possible case. Write down what Schema migration claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Schema migration before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Schema migration ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Schema migration

In research
Schema migration appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Schema migration in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Schema migration is common in secondary-school and first-year university syllabi. It links to neighbouring topics Agile software development, Databases, Software maintenance, so understanding it makes those chapters shorter.
In everyday life
Look for Schema migration outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Schema migration” →

How to study Schema migration in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Schema migration means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Schema migration out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Schema migration in simple terms?

In software engineering, a schema migration (also database migration, database change management) refers to the management of version-controlled, incremental and sometimes reversible changes to relational database schemas. A schema migration is performed on a database whenever it is necessary to up…

Why does Schema migration matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Schema migration?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Schema migration.

Tags

  • Agile software development
  • Databases
  • Software maintenance

Keep exploring