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还创建了表databasechangelog
和databasechangeloglock
。 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