lbruun-net / Pre-Liquibase

Spring Boot add-on to Liquibase
Apache License 2.0
49 stars 9 forks source link

pre-liquibase with spring boot DataJpaTest doesn't seem to work #2

Closed paul58914080 closed 3 years ago

paul58914080 commented 3 years ago

How to re-produce ?

  1. Create a simple spring boot starter with jpa project (v 2.5.2) (or you can also use the example project)

  2. Write a simple jpa test annotated with @DataJpaTest and application.yaml with property

    spring:
    datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:testdb;
    jpa:
    generate-ddl: false
    hibernate:
      ddl-auto: none
    database-platform: org.hibernate.dialect.H2Dialect
    show-sql: false
    properties:
      hibernate:
        default_schema: EXAMPLE
        show_sql: false
        use_sql_comments: false
        format_sql: false
      org:
        hibernate:
          envers:
            default_schema: EXAMPLE_AUDIT
            store_data_at_delete: true
    liquibase:
    enabled: true
    liquibase-schema: LIQUIBASE
    default-schema: EXAMPLE
  3. When you run a simple boot test, you would notice exception Schema "LIQUIBASE" not found; SQL statement:

lbruun commented 3 years ago

Thanks for reporting. This happens because @DataJpaTest doesn't fire the Pre-Liquibase auto-configuration. (it fires DataSource auto-config, Liquibase auto-config, Hibernate auto-config and what not). This means, in your example, Pre-Liquibase never fires, so that when Liquibase fires it tries to create its tables in schema LIQUIBASE ... which hasn't been created, resulting in error.

As far as I understand @DataJpaTest only triggers the auto-configuration relevant for JPA testing, not any/all Spring Boot auto-configuration. I'm investigating how Spring Boot figures out what that list is, exactly, so that I can get Pre-Liquibase Auto-Configuration class on that list.

In the meantime you can annotate your test with:

@DataJpaTest
@ImportAutoConfiguration(PreLiquibaseAutoConfiguration.class)
paul58914080 commented 3 years ago

Yes this surely helps

lbruun commented 3 years ago

Done!

Background: The magic that @DataJpaTest does (meaning which auto-config to execute) is in spring.factories in the core Spring Boot.

Fix: The Pre-Liquibase auto-configuration has now been added to that list so that you can now use @DataJpaTest without further ceremony.

Look for version 1.1.1 in Central Maven. It should be available shortly.

The example project has also been updated with a unit test example.

lbruun commented 3 years ago

Fix for this issue:

paul58914080 commented 3 years ago

Thank you so much for your quick response and fix :zap: