Home > Software design >  How to set liquibase variables in application.yml in Spring Boot?
How to set liquibase variables in application.yml in Spring Boot?

Time:12-07

When Spring Boot application first starts, the liquibase should create a user for runtime usage. The password should be configurable, so that it could vary across environments. I am trying to achieve that like this:

    <changeSet id="2021-12-02 12:00:01" author="author" dbms="mysql">
        <sql>
            CREATE USER consumer_app IDENTIFIED BY '${spring.datasource.password}';
        </sql>
        <rollback>
            DROP USER IF EXISTS consumer_app;
        </rollback>
    </changeSet>

and application.yml:

spring:
  liquibase:
    enabled: true
    user: root
    password: root
  datasource:
    url: "jdbc:mysql://localhost:3306/db"
    username: consumer_app
    password: password

And it does not work. The password is set as ${spring.datasource.password}, not as the value of this property in application.yml. Without single quotes I get a syntax error.

The project is built with Maven.

The password is going to be injected from docker-compose, so having init.sql with docker-managed variables is also an option.

CodePudding user response:

In spring boot you are passing parameters from yaml with following example:

spring:
  liquibase:
    parameters:
      spring.datasource.password: ${spring.datasource.password}

note that only parameters passed to spring.liquibase.parameters are being send to liquibase context.

CodePudding user response:

You should use insert into mysql.user instead, and use insert liquiabse balise instead of plain SQL to pass param on value.

  • Related