简体   繁体   中英

How to update database using Liquibase in Spring boot application?

I have the following liquibase configuration for the database in the spring boot application.

在此处输入图像描述

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.

Please suggest, Thanks

You have to create a different change log and needs to be added to the master file

Best Practices Using 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). 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. Liquibase keeps track of the checksums of the changeSets that it already executed. 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.

Sample Setup

Currently, I am using SQL format liquibase changelog but you can use XML, YAML or any other format.

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

 <?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]

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

As you can read here ,

Liquibase provides version control for your database

, the main reason Liquibase became so popular was that it allowed some type of version control to be applied on databases.

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

  1. create-initial-tables-changelog.xml

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

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

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

  5. 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 .

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. 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

Probably the main reason you use Liquibase, is to have a version control of your database. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM