Skip to content

@DataJpaTest using H2 with schema.sql and spring.datasource.schema-username fails #19321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
silovmar opened this issue Dec 6, 2019 · 10 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@silovmar
Copy link

silovmar commented Dec 6, 2019

Version: Spring Boot v2.2.1.RELEASE, Spring v5.2.1.RELEASE

Hi, I'm using @DataJpaTest with default embedded H2 DB for testing. Also I'm creating DB schema using schema.sql which contains CREATE SCHEMA IF NOT EXISTS TEST_SCHEMA;.

Unit tests annotated with the @DataJpaTest ran fine until I added to my production application.properties properties spring.datasource.schema-username=${DB_USERNAME_SCH} and spring.datasource.schema-password=${DB_PASSWORD_SCH}.

This causes that a datasource based on the application.properties is trying to be created instead of the default H2 database. The creation of DB from application.properties fails because the datasource configuration looks like this:

spring.datasource.driver-class-name= oracle.jdbc.OracleDriver
spring.datasource.url= ${DB_URL}
spring.datasource.username= ${DB_USERNAME}
spring.datasource.password= ${DB_PASSWORD}
spring.datasource.schema-username= ${DB_USERNAME_SCH}
spring.datasource.schema-password= ${DB_PASSWORD_SCH}

I would expect that the embedded H2 is used even if the schema-username and schema-password is used.

Source code which caueses the problem:
org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java:197 which is called from org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java:101

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 6, 2019
@philwebb
Copy link
Member

philwebb commented Dec 6, 2019

This is an edge case that we've not considered. You might be able to work around it by overriding the spring.datasource.schema-username property to "" in your test.

@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 6, 2019
@philwebb philwebb added this to the 2.2.x milestone Dec 6, 2019
eddumelendez added a commit to eddumelendez/spring-boot that referenced this issue Dec 7, 2019
Currently, `jdbcUrl` is determined when `spring.datasource.schema-username`
and `spring.datasource.schema-password` configuration properties are set.
This behaviour causes an issue trying to perform sql scripts in the
wrong database. This commit introduces to get the right `url` from the
`dataSource` bean.

See spring-projectsgh-19321
@eddumelendez
Copy link
Contributor

I have added my findings in #19327 (comment)

eddumelendez added a commit to eddumelendez/spring-boot that referenced this issue Dec 9, 2019
Currently, `jdbcUrl` is determined when `spring.datasource.schema-username`
and `spring.datasource.schema-password` configuration properties are set.
This behaviour causes an issue trying to perform sql scripts in the
wrong database. This commit introduces to get the right `url` from the
`dataSource` bean.

See spring-projectsgh-19321
@wilkinsona
Copy link
Member

Closing in favour of #19327.

@wilkinsona wilkinsona removed this from the 2.2.x milestone Jan 14, 2020
@wilkinsona wilkinsona added the status: superseded An issue that has been superseded by another label Jan 14, 2020
@snicoll
Copy link
Member

snicoll commented Apr 8, 2020

We're going to give it another go by overriding spring.datasource.schema-username when we replace the DataSource.

@snicoll snicoll reopened this Apr 8, 2020
@snicoll snicoll removed the status: superseded An issue that has been superseded by another label Apr 8, 2020
@snicoll snicoll added this to the 2.2.x milestone Apr 8, 2020
@somayaj
Copy link
Contributor

somayaj commented Oct 15, 2020

Can this be worked?

@somayaj
Copy link
Contributor

somayaj commented Oct 20, 2020

The !isEmbedded boolean check added to the username and password might solve the issue here

@wilkinsona
Copy link
Member

This appears to work, but I'd like some more eyes on it before merging it.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Nov 3, 2020
@somayaj
Copy link
Contributor

somayaj commented Nov 3, 2020

@wilkinsona I haven't created a PR yet. will do so if you think its a viable fix.

@wilkinsona
Copy link
Member

wilkinsona commented Nov 3, 2020

Thanks for the offer, @somayaj, but if the team agree that my proposed changes seem reasonable we won't need a PR for this one.

@wilkinsona wilkinsona self-assigned this Nov 3, 2020
@snicoll
Copy link
Member

snicoll commented Nov 4, 2020

LGTM Andy. Thanks!

@wilkinsona wilkinsona modified the milestones: 2.2.x, 2.2.12 Nov 4, 2020
@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

7 participants