- Practical DevOps
- Joakim Verona
- 388字
- 2021-07-16 09:48:07
Handling database migrations
Handling changes in a relational database requires special consideration.
A relational database stores both data and the structure of the data. Upgrading a database schema offers other challenges then the ones present when upgrading program binaries. Usually, when we upgrade an application binary, we stop the application, upgrade it, and then start it again. We don't really bother about the application state. That is handled outside of the application.
When upgrading databases, we do need to consider state, because a database contains comparatively little logic and structure, but contains much state.
In order to describe a database structure change, we issue a command to change the structure.
The database structures before and after a change is applied should be seen as different versions of the database. How do we keep track of database versions?
Note
Liquibase is a database migration system that, at its core, uses a tried and tested method. There are many systems like this, usually at least one for every programming language. Liquibase is well-known in the Java world, and even in the Java world, there are several alternatives that work in a similar manner. Flyway is another example for the Java platform.
Generally, database migration systems employ some variant of the following method:
- Add a table to the database where a database version is stored.
- Keep track of database change commands and bunch them together in versioned changesets. In the case of Liquibase, these changes are stored in XML files. Flyway employs a slightly different method where the changesets are handled as separate SQL files or occasionally as separate Java classes for more complex transitions.
- When Liquibase is being asked to upgrade a database, it looks at the metadata table and determines which changesets to run in order to make the database up-to-date with the latest version.
As previously stated, many database version management systems work like this. They differ mostly in the way the changesets are stored and how they determine which changesets to run. They might be stored in an XML file, like in the case of Liquibase, or as a set of separate SQL files, as with Flyway. This later method is more common with homegrown systems and has some advantages. The Clojure ecosystem also has at least one similar database migration system of its own, called Migratus.
- 數據庫系統原理及MySQL應用教程(第2版)
- Pandas Cookbook
- AngularJS Web Application Development Blueprints
- Ext JS Data-driven Application Design
- 軟件測試項目實戰之性能測試篇
- 精通軟件性能測試與LoadRunner實戰(第2版)
- C語言程序設計立體化案例教程
- Linux命令行與shell腳本編程大全(第4版)
- Python完全自學教程
- Python深度學習:基于TensorFlow
- INSTANT Yii 1.1 Application Development Starter
- R數據科學實戰:工具詳解與案例分析
- .NET 4.5 Parallel Extensions Cookbook
- Python從入門到精通(第3版)
- jQuery技術內幕:深入解析jQuery架構設計與實現原理