简体   繁体   English

如何在 Spring Boot 应用程序中使用 Liquibase 更新数据库?

[英]How to update database using Liquibase in Spring boot application?

I have the following liquibase configuration for the database in the spring boot application.我在 Spring Boot 应用程序中为数据库提供了以下 liquibase 配置。

在此处输入图像描述

Initially, these YAML scripts were executed when the application started and the database was created, now I want to update the datatype for one column so do I need to update the existing create-tables.YAML with column configuration or need to create another file with a different name and add the entry in "db.changelog-master.yaml" file.最初,这些 YAML 脚本是在应用程序启动并创建数据库时执行的,现在我想更新一列的数据类型,所以我需要使用列配置更新现有的 create-tables.YAML 还是需要创建另一个文件一个不同的名称并在“db.changelog-master.yaml”文件中添加条目。

Please suggest, Thanks请推荐,谢谢

You have to create a different change log and needs to be added to the master file您必须创建不同的更改日志并且需要添加到主文件中

Best Practices Using Liquibase使用 Liquibase 的最佳实践

Organizing Changelogs: Create a master changelog file that does not have actual changeSets but includes other changelogs (only YAML, JSON, and XML support using include, SQL does not).组织变更日志:创建一个没有实际变更集但包含其他变更日志的主变更日志文件(仅使用包含的 YAML、JSON 和 XML 支持,SQL 不支持)。 Doing so allows us to organize our changeSets in different changelog files.这样做可以让我们在不同的变更日志文件中组织我们的变更集。 Every time we add a new feature to the application that requires a database change, we can create a new changelog file, add it to version control, and include it in the master changelog.每次我们向需要更改数据库的应用程序添加新功能时,我们都可以创建一个新的更改日志文件,将其添加到版本控制中,并将其包含在主更改日志中。

One Change per ChangeSet: Have only one change per changeSet, as this allows easier rollback in case of a failure in applying the changeSet.每个变更集一个变更:每个变更集只有一个变更,因为这样可以在应用变更集失败的情况下更轻松地回滚。

Don't Modify a ChangeSet: Never modify a changeSet once it has been executed.不要修改变更集:一旦执行了变更集,就不要修改它。 Instead, add a new changeSet if modifications are needed for the change that has been applied by an existing changeSet.相反,如果需要对已由现有 changeSet 应用的更改进行修改,请添加新的 changeSet。 Liquibase keeps track of the checksums of the changeSets that it already executed. Liquibase 会跟踪它已经执行的变更集的校验和。 If an already run changeSet is modified, Liquibase by default will fail to run that changeSet again, and it will not proceed with the execution of other changeSets.如果修改了已经运行的 changeSet,默认情况下 Liquibase 将无法再次运行该 changeSet,并且不会继续执行其他 changeSet。

Sample Setup样品设置

Currently, I am using SQL format liquibase changelog but you can use XML, YAML or any other format. 目前,我使用 SQL 格式的 liquibase 更改日志,但您可以使用 XML、YAML 或任何其他格式。

test_schema.sql test_schema.sql

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
    <include file="schemas/test_schema.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>

changelog-master.xml更改日志-master.xml

 <?xml version="1.0" encoding="UTF-8"?> <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd"> <include file="schemas/test_schema.sql" relativeToChangelogFile="true"/> </databaseChangeLog>

directory setup目录设置

[![directory_setup][1]][1] [![directory_setup][1]][1]

Soure : https://reflectoring.io/database-migration-spring-boot-liquibase/酸: https ://reflectoring.io/database-migration-spring-boot-liquibase/

As you can read here ,正如你可以在这里读到的,

Liquibase provides version control for your database Liquibase 为您的数据库提供版本控制

, the main reason Liquibase became so popular was that it allowed some type of version control to be applied on databases. ,Liquibase 变得如此流行的主要原因是它允许在数据库上应用某种类型的版本控制。

For this to be able to happen you need to always record your changes in database into changelogs.为此,您需要始终将数据库中的更改记录到更改日志中。

So an example of Liquibase project will have所以 Liquibase 项目的一个例子将有

  1. create-initial-tables-changelog.xml创建初始表-changelog.xml

  2. update-schema-10-5-22.changelog.xml update-schema-10-5-22.changelog.xml

  3. update-data-15-5-22.changelog.xml更新数据15-5-22.changelog.xml

  4. update-schema-10-6-22.changelog.xml update-schema-10-6-22.changelog.xml

  5. update-data-15-6-22.changelog.xml update-data-15-6-22.changelog.xml

Each changelog file with changes may be named according to a representation of what the main changes are (dates are used here only for simplifying).每个带有更改的更改日志文件都可以根据主要更改的表示来命名(此处使用日期仅用于简化)。

Then the user can use a version control system (like git) and when for example the user checkouts a commit of the past (ex commit A on 15/05/2022) then he is able to view the database on that version as it existed on 15/05/2022, since liquibase will execute only the scripts that existed on that commit, namely create-initial-tables-changelog.xml , update-schema-10-5-22.changelog.xml , update-data-15-5-22.changelog.xml .然后用户可以使用版本控制系统(如 git),例如,当用户签出过去的提交(前 15/05/2022 上的提交 A)时,他可以查看该版本上的数据库,因为它存在2022 年 5 月 15 日,因为 liquibase 将仅执行该提交中存在的脚本,即create-initial-tables-changelog.xmlupdate-schema-10-5-22.changelog.xmlupdate-data-15-5-22.changelog.xml

Also all changelog files need to be referenced in the master changelog file for a specific version, because this file is used for executing scripts in database when asked for.此外,所有变更日志文件都需要在特定版本的主变更日志文件中引用,因为该文件用于在请求时在数据库中执行脚本。

The master changelog file works as a configuration file that will hold all the references to all your other changelogs.主变更日志文件用作配置文件,它将保存对所有其他变更日志的所有引用。 Your master changelog file must be in an XML, YAML, or JSON format.您的主变更日志文件必须采用 XML、YAML 或 JSON 格式。 From docs 来自文档

Having said all the above, now I can answer your question说了这么多,现在我可以回答你的问题了

So do I need to update the existing create-tables.YAML with column configuration or need to create another file with a different name所以我需要使用列配置更新现有的 create-tables.YAML 还是需要创建另一个具有不同名称的文件

Probably the main reason you use Liquibase, is to have a version control of your database.您使用 Liquibase 的主要原因可能是对数据库进行版本控制。 If you wish to respect this reason, then you must create a different changelog file which would be a snapshot of a different version of database than the initial version that existed.如果您希望尊重这个原因,那么您必须创建一个不同的变更日志文件,该文件将是与存在的初始版本不同的数据库版本的快照。

Also if for example you had the initial create-tables-changelog.xml and then you also had 3 more changelog.xml files that applied changes in database, you could not afford to make changes only in the create-tables-changelog.xml since this would risk that the execution of the 3 next files changelog1.xml , changelog2.xml , changelog3.xml would break.此外,例如,如果您有初始的create-tables-changelog.xml ,然后您还有 3 个在数据库中应用更改的changelog.xml文件,那么您不能只在create-tables-changelog.xml中进行更改,因为这可能会导致 3 个下一个文件changelog1.xmlchangelog2.xmlchangelog3.xml的执行中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spring Boot 和 Liquibase 时,如何在每次集成测试后清理数据库表? - How to clean database tables after each integration test when using Spring Boot and Liquibase? 如何使用 spring 引导更新数据库 - How to update database with spring boot 如何使用Spring Boot Batch Job更新数据库中的大数据 - How to update huge data in database using Spring Boot Batch Job 使用 spring 引导更新数据库中的数据 - Update data in database using spring boot 在 Spring 引导中使用 Maven、Liquibase 和 Hibernate 引导生成数据库和实体之间的差异文件会产生错误 - Using Maven, Liquibase and Hibernate in Spring Boot to generate Diff-Files between a Database and Entities yields Error 如何更新 spring 启动应用程序中的记录? - How to update record in spring boot application? 如何更新/升级 Spring Boot 云应用程序 - How to update/upgrade a spring boot cloud application liquibase changelogsync 与 spring 启动 - liquibase changelogsync with spring boot 如何在 spring 引导应用程序中保持数据库活动 - How to keep database active in spring boot application Liquibase 是否在 Spring Boot 应用程序中注册所有 bean 之前运行? - Does Liquibase runs before registering all the beans in spring boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM