0

I developed a website using spring boot in this application I'm using architecture multi tenant to manage my database. I want to use Liquibase as a DB migration tool. The problem is that when i do migration the new modification(modification means by add new columns to different tables and also add new tables) is only apply in schema public and doesn't apply on the others sachems , what i want , when i do migration i want the new modification apply on all sachems ps : i'm using hibernate to create new sachems

1 Answers1

0

Liquibase allows dynamic substitution of properties in changelog files. We can configure multiple properties inside a file and then use them wherever required. In your case, we can configure properties "schema1", "schema2" with some value and then use it in changelog file using ${schema1} or ${schema2} syntax as per requirement.

In liquibase.properties file, we will configure these properties as follows:

schema1=ABC
schema2=PQR

Liquibase assigns or prioritizes value for configured property in below order:

  1. As an attribute passed to your liquibase runner.
  2. As a JVM sytem property
  3. As an environment variable
  4. As a CLI attribute if you are running liquibase through command line
  5. In liquibase.properties file
  6. In the parameters block (property element of the DATABASECHANGELOG table)

You can do it as below example code snippet:

1. Adding a column to some table in schema ABC:

<?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-3.8.xsd">

    <changeSet author="authorName" id="some-unique-id" dbms="${dbType}" context="some-context">
       <sql endDelimiter=";" splitStatements="true" stripComments="true">
            **My SQL query/ transactional logic goes here**
            ALTER TABLE "${schema1}"."TableName" ADD COLUMN COLUMNNAME DATATYPE;
       </sql>
    </changeSet>
    
</databaseChangeLog>

2. Creating a table in PQR schema:

<?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-3.8.xsd">

    <changeSet author="authorName" id="some_unique_id" dbms="${dbType}" context="some_context">
        <createTable tableName="TableName" schemaName="${schema2}">
            <column name="id" type="VARCHAR(200)" />
            <column name="name" type="VARCHAR(255)"/>
        </createTable>
    </changeSet>

</databaseChangeLog>

Note: above example is using 2 properties (schema1 and schema2). You can use only even more than that.

If you need help with creating "liquibase.properties" file, visit this link

Cheers!

Rakhi Agrawal
  • 827
  • 7
  • 14