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 gh-19321

Comment From: eddumelendez

These are my finding regarding the issue: The first h2 database created is generated with a random uuid which is used in the url. When spring.datasource.schema-username and spring.datasource.schema-password are set there is a call to determineUrl which will return testdb in this case and this schema.sql is executed right there. This behaviour combined with spring.datasource.data will perform data.sql into the database with the random uuid which will cause issues because the schema doesn't exist due to in this case determineUrl is not call.

I'm missing the test in this PR but trying to figure it out how to do it. This is due to we need to set up the right spring.datasource.*-username and spring.datasource.*-password (both required right now) otherwise we will get an exception with a message don't have right permissions.

I have two thoughts here: 1. Add !isEmbedded() as a part of the condition in order to create the new datasource with the right credentials because embedded datasources doesn't have password. And they way to do it for h2 at least which I was looking into it is a way more complicated to test it. 2. Relax the condition from AND to OR

I would like to discuss approaches in order to tackle this issue.

Comment From: eddumelendez

I have updated the PR, moving the password validation which make sense for embedded database and will work for other escenarios if is needed. Also, test was added.

Comment From: snicoll

We've discussed this one and we'd like to try a different approach. I've reopened #19321, see this comment. Thanks for the PR, in any case!