Liquibase —在Spring Boot中处理数据库

If you are building an application with Spring Boot, handling the database changes becomes a nightmare over time. The more changes you add, the more changes you have to maintain for your database. Liquibase is the best solution out there. In this post, we will show how to handle database changes using liquibase.

如果您使用Spring Boot构建应用程序,随着时间的推移,处理数据库更改将成为一场噩梦。 添加的更改越多,数据库维护的更改就越多。 Liquibase是目前最好的解决方案。 在本文中,我们将展示如何使用liquibase处理数据库更改。

什么是Liquibase? (What is Liquibase?)

Liquibase is an open-source library to track, manage, and apply database changes. Liquibase tracks the changes to database through an XML configuration where a developer will usually add changesets.

Liquibase是一个开源库,用于跟踪,管理和应用数据库更改。 Liquibase通过XML配置跟踪对数据库的更改,开发人员通常会在其中添加更改集。

Each changeset will have an id and author attributes. Liquibase uses a changelog to track the database changes. Every changeset you add, that will get added in the changelog. Changelog is a ledger of all the changes you are doing to database.

每个变更集将具有一个id和author属性。 Liquibase使用更改日志来跟踪数据库更改。 您添加的每个变更集都将添加到变更日志中。 Changelog是您对数据库所做的所有更改的分类帐。

Liquibase如何工作? (How does Liquibase work?)

To track database changes, you will write an XML file that is platform-independent. This XML file will be used on the command line to translate into scripts for your database engine.

要跟踪数据库更改,您将编写一个与平台无关的XML文件。 此XML文件将在命令行上使用,以转换为数据库引擎的脚本。

We can also use a maven or Gradle plugin to include database changes in the build configuration.

我们还可以使用Maven或Gradle插件在构建配置中包括数据库更改。

Liquibase uses its own tables to track changes. Those tables will be part of the schema you are building for consistency purposes. It records the hash of each changeset.

Liquibase使用其自己的表来跟踪更改。 这些表将成为您出于一致性目的而构建的架构的一部分。 它记录每个变更集的哈希。

如何编写变更集? (How to write a changeset?)

Previously, I mentioned you can write a changeset using XML. But liquibase also offers the support for JSON or YAML.

之前,我提到过您可以使用XML编写变更集。 但是liquibase还提供了对JSON或YAML的支持。

As part of this post, I will show how I add a changeset and generate scripts for the database.

作为本文的一部分,我将展示如何添加变更集并为数据库生成脚本。

Create an XML changelog file db.changelog-master.xml for our database under folder src\main\resources\db. Usually, if you start using liquibase from the start of the project, you will create an initial changelog file that will generate initial scripts. You can track every change after that through a change set.

在文件夹src\main\resources\db下为我们的数据库创建一个XML changelog文件db.changelog-master.xml 。 通常,如果从项目开始就开始使用liquibase,则将创建一个初始变更日志文件,该文件将生成初始脚本。 之后,您可以通过变更集跟踪每个变更。

The file without any changeset will look like below:

没有任何变更集的文件如下所示:

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
</databaseChangeLog>

Now I can handle this master file in two ways. For each changeset, I can create a separate file and include that file in the master file OR I can add every changeset in the same master file.

现在,我可以通过两种方式处理该主文件。 对于每个变更集,我可以创建一个单独的文件并将该文件包含在主文件中,或者可以将每个变更集添加到同一主文件中。

Each changeset needs an author and unique id.

每个变更集都需要一个作者和唯一的ID。

Now we will add changeset to this changelog file and it will look like below:

现在,我们将更改集添加到此更改日志文件,如下所示:

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">


    <changeSet author="Yogesh Mali" id="jira-ticket-01">
        <createTable tableName="user">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="guid" type="varchar(50)">
            </column>
            <column name="firstname" type="varchar(100)">
                <constraints nullable="false"/>
            </column>
            <column name="middlename" type="varchar(100)"></column>
            <column name="lastname" type="varchar(100)"></column>
            <column name="email" type="varchar(100)">
                <constraints nullable="false"/>
            </column>
            <column name="companyid" type="int"></column>
            <column name="roleid" type="int"></column>
        </createTable>
        <createTable tableName="company">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="guid" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="type" type="varchar(10)"></column>
        </createTable>
        <createTable tableName="role">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="role_name" type="varchar(20)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseTableName="user" baseColumnNames="companyid"
                                  constraintName="company_fk" referencedTableName="company"
                                  referencedColumnNames="id" />
        <addForeignKeyConstraint baseTableName="user" baseColumnNames="roleid"
                                  constraintName="role_fk" referencedTableName="role"
                                  referencedColumnNames="id"/>
    </changeSet>




</databaseChangeLog>

Now we are ready to create liquibase Bean in our Spring Boot project. We will have to add the following property in our application.properties file.

现在,我们准备在Spring Boot项目中创建liquibase Bean。 我们将不得不在application.properties文件中添加以下属性。

spring.liquibase.changeLog=classpath:/db/db.changelog-master.xml.

spring.liquibase.changeLog=classpath:/db/db.changelog-master.xml

Also, don’t forget to add database properties in application.properties file.

另外,不要忘记在application.properties文件中添加数据库属性。

spring.datasource.url=jdbc:mysql://127.0.0.1/demo
spring.datasource.username = sa
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.liquibase.changeLog=classpath:/db/db.changelog-master.xml

Before we run our Spring Boot Project, add liquibase dependency in our gradle project.

在运行Spring Boot Project之前,请在gradle项目中添加liquibase依赖项。

compile('org.liquibase:liquibase-core:4.0.0').

compile('org.liquibase:liquibase-core:4.0.0')

Now if we run our Spring Boot project, we will see the database tables created in the log messages as follows:

现在,如果我们运行Spring Boot项目,我们将在日志消息中看到创建的数据库表,如下所示:

2020-07-26 12:22:24.362  INFO 32412 --- [           main] liquibase.lockservice                    : Successfully acquired change log lock
2020-07-26 12:22:25.314  INFO 32412 --- [           main] liquibase.changelog                      : Creating database history table with name: blogdemo.DATABASECHANGELOG
2020-07-26 12:22:25.345  INFO 32412 --- [           main] liquibase.changelog                      : Reading from blogdemo.DATABASECHANGELOG
2020-07-26 12:22:25.427  INFO 32412 --- [           main] liquibase.changelog                      : Table user created
2020-07-26 12:22:25.443  INFO 32412 --- [           main] liquibase.changelog                      : Table company created
2020-07-26 12:22:25.458  INFO 32412 --- [           main] liquibase.changelog                      : Table role created
2020-07-26 12:22:25.520  INFO 32412 --- [           main] liquibase.changelog                      : Foreign key constraint added to user (companyid)
2020-07-26 12:22:25.588  INFO 32412 --- [           main] liquibase.changelog                      : Foreign key constraint added to user (roleid)
2020-07-26 12:22:25.588  INFO 32412 --- [           main] liquibase.changelog                      : ChangeSet db/db.changelog-master.xml::jira-ticket-01::Yogesh Mali ran successfully in 186ms
2020-07-26 12:22:25.600  INFO 32412 --- [           main] liquibase.lockservice                    : Successfully released change log lock

As part of this execution, liquibase also created the tables databasechangelog and databasechangeloglock. Liquibase uses these tables to track the changes for the database. If you add another changeset in the changelog file, liquibase will identify that changeset based on previous changes and will perform appropriate action next time you run the application.

在执行过程中,liquibase还创建了表databasechangelogdatabasechangeloglock 。 Liquibase使用这些表来跟踪数据库的更改。 如果您在变更日志文件中添加另一个变更集,则liquibase将基于先前的更改识别该变更集,并在下次运行该应用程序时执行适当的操作。

结论 (Conclusion)

In this post, I showed how to use liquibase to handle database changes in a Spring Boot project.

在这篇文章中,我展示了如何使用liquibase处理Spring Boot项目中的数据库更改。

One thing, I didn’t discuss in this post is another database migration tool Flyway. Flyway is also an open-source database migration tool.

我没有在本文中讨论的一件事是另一种数据库迁移工具Flyway。 Flyway还是开源数据库迁移工具。

Originally published at https://betterjavacode.com on July 26, 2020.

最初于 2020年7月26日 发布在 https://betterjavacode.com

翻译自: https://medium.com/dev-genius/liquibase-handling-database-in-spring-boot-dbdf237d2b3b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring Boot进行Liquibase迁移时无法生效可能有以下几个原因: 首先,确保已经正确配置了Liquibase。在Spring Boot,我们可以在application.properties或application.yml进行配置。在这些配置文件,需要指定Liquibase的相关属性,如数据库连接URL、用户名、密码以及要执行的迁移脚本位置等。请确保配置是正确的,并且Liquibase相关的依赖已经正确导入。 其次,检查是否有正确设置Liquibasechangelog文件。changelog文件是记录数据库迁移脚本的地方,Liquibase会根据changelog文件来执行相应的迁移操作。请确保changelog文件的位置与配置文件所指定的位置相符,并且文件名也正确。 第三,在启动时检查日志输出,查看是否有Liquibase的相关信息。在Spring Boot启动时,Liquibase会自动运行并输出相关的日志信息。请确保没有出现异常或错误信息,并且能正确加载并执行迁移脚本。 最后,如果以上步骤都没有问题,但仍然无法生效,可以尝试手动执行Liquibase迁移。可以编写一个简单的Java程序,在程序手动调用Liquibase API进行迁移操作。这样可以确保Liquibase配置和迁移脚本本身没有问题。如果手动迁移可以成功执行,那么可能是Spring Boot的自动迁移机制有问题,可以尝试查看相关文档或向社区寻求帮助。 总结起来,解决Spring Boot在启动时无法进行Liquibase迁移的问题,需要确保正确配置了Liquibase、正确设置了changelog文件,并注意查看日志输出以及尝试手动执行迁移脚本来排除其他可能的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值