Search code examples
spring-bootterraformamazon-rdsliquibase

Liquibase ,Terraform and Spring Boot question


I am using terraform to create a RDS aurora mysql. I connect to it using SpringBoot. My question is on creating the table. I thought that terraform would create the table, but it looks like that SpringBoot connects to the database and uses liquibase to create the table. is this correct. I was hoping to create the tables as I create the database via terraform calling liquibase, But cant find any documentation detailing how to do this,


Solution

  • Database tables should be created by liquibase scripts. To ensure this, you need to configure liquibase with spring boot application by providing database connection details in app config. This means, before liquibase execution, the database must already be created (manually or automated by a process). Then, you will also be able to trigger liquibase execution (by mvn liquibase:update or running the app) within an automated process or pipeline.

    A typical liquibase configuration with spring boot may look like this:

    application.yaml:

    spring:   
      datasource:
        url: jdbc:postgresql://localhost:5432/db1
        username: test
        password: test   
      liquibase:
        enabled: true
        change-log: classpath:changelog.xml
    

    changelog.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-3.8.xsd">
    
        <include file="change-sets/create-table1.xml"/>
    
    
    </databaseChangeLog>
    

    change-sets/create-table1.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-3.8.xsd">
    
        <changeSet id="create-table1-ddl" author="bmfirat">
            <createTable tableName="table1">
                <column name="id" type="BIGINT" autoIncrement="true" >
                    <constraints primaryKey="true" nullable="false" primaryKeyName="pk_table1"/>
                </column>
                <column name="title" type="varchar(255)">
                    <constraints nullable="false" unique="false"/>
                </column>           
                <column name="description" type="varchar(255)">
                    <constraints nullable="false" unique="false"/>
                </column>   
            </createTable>
            <rollback>
                <dropTable tableName="table1"/>
            </rollback>
        </changeSet>
    
    </databaseChangeLog>