Search code examples
spring-bootliquibaseliquibase-hibernate

SQL script is not executed during startup


I want to run SQL script always when I start Spring boot application. I added this Liquibase configuration:

application.yml

spring:
  datasource:
    platform: org.hibernate.dialect.PostgreSQL95Dialect
    url: jdbc:postgresql://10.10.10.10:5432/test
    driverClassName: org.postgresql.Driver
    username: root
    password: test
  liquibase:
    changeLog: 'classpath:db/changelog/db.changelog-master.yaml'
    dropFirst: false
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    database: postgresql

db.changelog-master.yaml

databaseChangeLog:
  - include:
      file: db/changelog/changes/ch_0001/changelog.yaml

changelog.yaml

databaseChangeLog:
  - include:
      file: db/changelog/changes/ch_0001/data/data.yaml

data.yaml

databaseChangeLog:
  - changeSet:
      id: 0001
      author: test
      dbms: postgres
      runAlways: true # WARNING - remove this before prod - it will run every time with clean data
      changes:
        - sqlFile:
            - relativeToChangelogFile: true
            - path: data.sql

data.sql

INSERT into tasks SELECT generate_series(1,355) AS id,
    left (md5(random()::text), 10) AS business_name,
    (select NOW() + (random() * (NOW()+'90 days' - NOW())) + '30 days') AS created_at,
    left (md5(random()::text), 10) AS meta_title,
    left (md5(random()::text), 10) AS status,
    left (md5(random()::text), 10) AS title,
    left (md5(random()::text), 10) AS task_type,
    (select NOW() + (random() * (NOW()+'90 days' - NOW())) + '30 days') AS updated_at;

Database table should be populated with test data but it's not. I don't see into logs data.sql file execution.

Do you know know what could be the issue?


Solution

  • Seems like you did not add full config.

    Try adding these.

    spring:
      liquibase:
        change-log: classpath:db/changelog/db.changelog-master.yaml
        url: {same as spring.datasource.url}
        user: {same as spring.datasource.username}
        password: {same as spring.datasource.password}
        enabled: true
    

    Update

    For Postgres DB dbmd value is postgresql. Not postgres. Reference - Here

    Updated data.yml

    databaseChangeLog:
      - changeSet:
          id: 0001
          author: test
          dbms: postgresql
          runAlways: true # WARNING - remove this before prod - it will run every time with clean data
          changes:
            - sqlFile:
                - relativeToChangelogFile: true
                - path: data.sql