官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 楚雄市| 肥西县| 乌兰浩特市| 东宁县| 奉贤区| 射阳县| 漳浦县| 淮滨县| 汉寿县| 宿松县| 灵璧县| 临颍县| 商河县| 十堰市| 广安市| 丹寨县| 海南省| 信阳市| 成安县| 潜山县| 清新县| 绵阳市| 保康县| 温宿县| 浦县| 九龙城区| 鹤山市| 凉城县| 大理市| 安远县| 太原市| 萨迦县| 建宁县| 东乌| 汉川市| 即墨市| 重庆市| 页游| 株洲县| 香格里拉县| 无锡市|