Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter databasechangelog.filename for Spring Boot and Liquibase?

I'm using Spring Boot 2.0.1 and liquibase-core-3.5.5.

When I run the application from IntelliJ (which will run the main() method) I see the following type of value in the Liquibase database column database.filename:

db/changelog/changes/v0001.sql

If I create a fat jar with embedded Tomcat and run the application, the same changeset will appear in the database as:

BOOT-INF/classes/db/changelog/changes/v0001.sql

I would like these values to match so that I can run my applications as JAR files, but connect to a remote database from IntelliJ if debugging is necessary. (Note: this would be for a test database, I would not debug production in this way).

I have found references to logicalFilePath (such as this SO question), but I don't believe this will provide what I need.

I have also tried debugging into the Liquibase code to determine if there is a way I can alter this value. The closest I've come is these two observations:

  1. The absolute file path of the SQL file trimmed to its final value here.
  2. The full set of changes being persisted is available here. And if I change one of those values (while debugging), I can see that it makes it into the database.

If there is a way to make these consistent, or possibly just change the filename column to the actual filename (e.g. v0001.sql), that would be best.

UPDATE

I have Liquibase setup as the following in Spring Boot:

My resources/db/changelog/db.changelog-master.yaml file has the following contents:

databaseChangeLog:
    - includeAll:
        path: db/changelog/changes/

And then in resources/db/changelog/changes I have individual *.sql files that contain the SQL I want to run. I do not recall where I found this setup, but I don't have anything specific to "changesets", is this an error on my part?

like image 742
mnd Avatar asked Jul 13 '18 19:07

mnd


People also ask

How do I run a .SQL file in Liquibase?

To run the execute-sql command, specify the following parameters in the Liquibase properties file, environment variables, or the command prompt while running the command: URL, driver [optional], and user authentication information such as username and password.


2 Answers

I was able to use this by no longer using the includeAll option in my db.changelog-master.yaml file. The includeAll option is a helpful way to get started quickly (and process all files in a specific directory), but it seems to be unadvisable for regular use. As mentioned here.

This is what I previously had in db.changelog-master.yaml, don't use it this way.

databaseChangeLog:
    - includeAll:
        path: db/changelog/changes/

This is what I updated it to:

databaseChangeLog:
    - include:
        file: db/changelog/changes/v0001_users.sql
    - include:
        file: db/changelog/changes/v0002_accounts.sql

The important thing to note is that I'm specifically listing every single file that I want included, in the order I want it run. This is a little bit of a hassle, but well worth the change.

In the original version, the databasechangelog.filename column would be different based on how the application was executed:

  • db/changelog/changes/v0001_users.sql is how the column would appear when running from IntelliJ
  • BOOT-INF/classes/db/changelog/changes/v0001_users.sql is how the column would appear when running from an executable jar.

After making the change, both options above are the same, and no longer include BOOT-INF/classes/ as a prefix.

Thanks to the other answers that helped point me in the correct direction!

like image 59
mnd Avatar answered Sep 28 '22 06:09

mnd


The logicalFilePath attribute is exactly what you want. I was facing same issue with my projects where I was executing some parts with maven and some parts with spring-boot. this question can also help you.

like image 30
bilak Avatar answered Sep 28 '22 05:09

bilak