Second Brain: Crafted, Curated, Connected, Compounded on 10月02日
Liquibase:数据库变更管理利器
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Liquibase是一款开源数据库模式变更管理工具,它能够跟踪、版本化和部署数据库的演变过程。借鉴了Git等版本控制系统的理念,Liquibase将数据库变更“代码化”,允许开发者使用SQL、XML、YAML或JSON等格式定义变更。这些变更与应用程序代码一同进行版本控制,并能以可重复、自动化的方式应用。通过Changesets和Changelogs,Liquibase能够清晰地记录数据库变更历史,支持跨多种数据库类型管理同一模式,并提供回滚功能,甚至在免费开源版本中也支持。此外,它还能选择性地部署变更到不同环境,并提供Pro版本增强的差异比较和可视化功能。

📜 **版本化数据库变更:** Liquibase将数据库模式的演变视为代码,允许使用SQL、XML、YAML或JSON等多种格式定义变更。这些变更与应用代码一同进行版本控制,确保了数据库状态的可追溯性和管理性。

🔄 **自动化部署与回滚:** Liquibase通过Changelogs和Changesets实现数据库变更的自动化部署,并能智能地跟踪已部署的变更。它还提供了强大的回滚功能,允许撤销已应用的变更,有效降低了数据库操作的风险。

🗄️ **跨数据库兼容性:** Liquibase支持定义数据库无关的变更,能够自动生成特定于数据库的SQL。这使得开发者能够轻松地将变更推送到不同的数据库类型,提高了开发效率和灵活性。

⚙️ **精细化变更控制:** 利用Labels和Contexts,Liquibase能够实现对数据库变更的选择性部署,确保变更仅在指定的数据库环境中应用。这对于多环境的复杂项目尤为重要。

🔍 **数据迁移与处理:** Liquibase能够处理数据类型的修改和列的重命名,并尝试在变更过程中保留现有数据。对于复杂的数据迁移场景,它也提供了通过preconditions或自定义SQL来处理的选项。

Liquibase is a database schema change management tool that helps track, version, and deploy database changes - particularly useful for changing database schemas over time. It works similar to version control systems (like Git), but specifically for database changes (and Schema Evolution).

In the context of the evolution of database management, Liquibase was significant because it introduced the concept of “migrations as code” where database schema changes are:

    Written as code (in XML, SQL, YAML, or JSON)Version controlled alongside application codeApplied in a repeatable, automated wayTracked to maintain database change history

There is an open-source version on GitHub, but also a closed source.

# Features

Features from Open Source | Liquibase.

12345678
--liquibase formatted sql--changeset nvoxland:create-test-tableCREATE TABLE testTable(columnName1 VARCHAR (355));--rollback DROP TABLE--rollback testTable

# Define database changes

Liquibase makes it easy to define database changes in a format that’s familiar and comfortable to each user and then automatically generates database-specific SQL for you.

Liquibase uses changesets to represent a single change to your database.

# Update and manage the same schema across multiple database types

Liquibase allows you to specify the database change you want using SQL or several different database-agnostic formats, including XML, YAML, and JSON. Developers can abstract the database code to make it extremely easy to push out changes to different database types. The abstract format options also offer more flexibility and many more features over a formatted SQL changelog.

 1 2 3 4 5 6 7 8 9101112
--liquibase formatted sql--changeset <author name>:<a unique identifier for the SQL changeset><SQL statements go here><SQL statements go here>--rollback <rollback SQL statements>--rollback <rollback SQL statements>--changeset <author name>:<another unique identifier><SQL statements go here><SQL statements go here>--rollback <rollback SQL statements>--rollback <rollback SQL statements>

# Easily add new changes and reorder them

Database changes are managed with one ledger that we call the changelog. Liquibase tracks which changesets have or have not been deployed in a tracking table.

Using a unique identification scheme (an ID and author, along with the name and path of the file) makes it easy to manage and reorder database changes. Since there are multiple factors that drive the uniqueness of a given change, it also reduces developer conflicts and collisions.

# Track, version, and deploy database changes

Changelogs and tracking tables allow Liquibase to help teams track and version database changes and deploy changes.

    Track and version database changes
    Developers know which changes have been deployed to the database and which changes have not yet been deployed.Deploy changes
    Liquibase compares the changelog against the tracking table and only deploys changes that have not been deployed to the database.

# Rollbacks included

Even the free, open-source version of Liquibase allows you to undo changes you have made to your database, either automatically or via custom rollback SQL with the rollback command.

Liquibase Pro adds Targeted Rollbacks, which allow users to rollback a specific change or set of changes without rolling everything else back, saving a lot of time and headaches.

# Selectively deploy changes to different environments

If your use case requires you to selectively deploy changes, Liquibase is a perfect solution. Since Liquibase uses one ledger (changelog), it’s very straightforward to add labels and contexts to ensure that your deployments are defined in one place.

123456
-- liquibase formatted sql changeLogID:123-- changeset nvoxland:example labels:v1 context:allcreate table "public"."customer_customer_demo" ("customer_id" CHAR not null, "customer_type_id" CHAR not null, constraint "pk_customer_customer_demo" primary key ("customer_id", "customer_type_"));-- changeset nvxoland:example2 labels:v1 context:all create table "public"."customers" ("customer_id" CHAR not null, "company_name" VARCHAR(40) not null, "contact_name" VARCHAR(30), "contact_title" VARCHAR(30), "address" VARCHAR(60), "postal_code" VARCHAR(10), constraint "pk_customers" primary key ("customer_id"));

# Work with stored logic

Liquibase Pro Link enables users to snapshot and work with stored logic. Snapshots allow you to get a static view of your database at a particular point in time and is useful for reporting and safeguarding your data by comparing databases (performing diffs) to find differences.

# Visualize changes

Liquibase Enterprise provides a dashboard with insights into your database changes.

    Liquibase DMC is our on-prem dashboard and reporting tool designed for our Liquibase Enterprise users.

# Package code and automate

Package your SQL scripts and test the corresponding rollback scripts at the same time. Liquibase Enterprise validates the SQL scripts as well as the rollback scripts. If the rollbacks fail to revert the database back to its original state, then the build fails, and you’re alerted. The Liquibase command returns an error that you’ll see on the command line. Instant feedback loop!

# Keep your database safe & compliant

Automatically enforce best practices set by your DBAs, including naming conventions, grants & revokes, and rollback scripts. Quality checks provide instant feedback on database code. Developers no longer have to wait for hours or days for a DBA to provide feedback on their work.
Quality Checks

# Forecast changes and eliminate failed deployments

Forecast the impact of changes on a target database in any environment. Take the risk out of deployment! Liquibase Enterprise checks to ensure that nothing has changed in the target database that would cause changes that worked in a lower environment to fail in the target environment.

Never hear “it worked in Test but failed in Stage” ever again.

# Others

# Workflow

To start using Liquibase quickly and easily, you can write your migration scripts in SQL.

To take advantage of database abstraction abilities that allow you to write changes once and deploy to different database platforms, you can specify database-agnostic changes in XML, JSON, or YAML

Introduction to Liquibase

# Changelog organization

Liquibase uses SQL, XML, JSON, and YAML changelog files to list database changes in sequential order. Database changes have the format of changesets. Changesets contain Change Types, which are types of operations to apply to the database, such as adding a column or primary key. Contextlabel, and precondition changelog tags help precisely control when a database change is made and to which database environment it is deployhttps://www.ssp.sh/brain/img_Liquibase_1745304277178.webp1745304277178.webp">

# Database Changelog and Database Changelog Lock

When you deploy your changes, Liquibase creates two tables in your database: DATABASECHANGELOG and DATABASECHANGELOGLOCK.

The DATABASECHANGELOG table tracks deployed changes so that you have a record. Liquibase compares the changesets in the changelog file with the DATABASECHANGELOG tracking table and deploys only new changesets.

DATABASECHANGELOGLOCK prevents multiple instances of Liquibase from updating the database at the same time. The table manages access to the DATABASECHANGELOG table during deployment and ensures only one instance of Liquibase is updating the database.

# Database management options

Liquibase offers many ways to manage your database changes:

# Update Command

The update command deploys any changes that are in the changelog file and that have not been deployed to your database yet.

# Uses

The update command is typically used to apply database changes that are specified in the changelog file to your database.

When you run the update command, Liquibase sequentially reads changesets in the changelog file, then it compares the unique identifiers of idauthor, and path to filename to the values stored in the DATABASECHANGELOG table.

    If the unique identifiers do not exist, Liquibase will apply the changeset to the database.If the unique identifiers exist, the MD5Sum of the changeset is compared to the one in the database.If they are different, Liquibase will produce an error message that someone has changed it unexpectedly. However, if the status of the [runOnChange](https://docs.liquibase.com/concepts/changelogs/attributes/runonchange.html) or runAlways changeset attribute is set to TRUE, Liquibase will re-apply the changeset.

In Liquibase Pro 4.25.1 and later, you can automatically generate a database Update Report summarizing this command.

# Syntax

To run the update command, specify the driver, classpath, and URL in the Liquibase properties file. For more information, see Create and Configure a liquibase.properties File. You can also specify these properties in your command line.

Then run the update command:

1
liquibase update --changelog-file=example-changelog.xml

# How to handle data

Liquibase handles schema changes like column renames and type changes through specific change types in its changesets. Here’s how it manages these operations:

For ALTER RENAME operations:

    Column renames use the renameColumn change typeTable renames use the renameTable change typeThese operations preserve the existing data while changing the structure

For ALTER UPDATE TYPES (changing column data types):

    Liquibase uses the modifyDataType change typeThe behavior depends on database compatibility and the specific type conversion

When the database contains data:

    Liquibase attempts to preserve existing data during schema changesFor simple changes like renaming, data is maintained intactFor type changes, Liquibase will try to convert the data to the new typeIf the conversion is compatible (like VARCHAR(50) to VARCHAR(100)), data is preservedFor incompatible conversions (like VARCHAR to INTEGER), behavior depends on:
      The database system’s conversion rulesWhether the data can be validly convertedConstraints on the column

If a data conversion might fail, Liquibase provides options to:

    Use preconditions to check data before attempting changesWrite custom SQL for more complex migrationsUse multiple changesets to handle the migration (e.g., add new column, copy/transform data, drop old column)

# LoadUpdateData

loadUpdateData
this function can even run and load data from source and MERGE if data existis, see example in above link


Origin: Building a Data Engineering Project in 20 Minutes : dataengineering , We use at HelloDATA BE in 2023-09-11.
References:

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Liquibase 数据库变更管理 Schema Evolution 版本控制 自动化部署 SQL Database Management DevOps
相关文章