Skip to content
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

H2 + liquibase + schema.sql functionality broken in integration tests with 1.4.3 #7876

Closed
XaeroDegreaz opened this issue Jan 4, 2017 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@XaeroDegreaz
Copy link

XaeroDegreaz commented Jan 4, 2017

@snicoll Hello! I believe the fix in #7473 this may have caused a regression when it comes to schema.sql files and liquibase functionality.

I have a project which has it's own schema defined in a liquibase changeset, and a foreign schema stubbed out in a schema.sql file. There is a cross-schema foreign key constraint on one of the liquibase columns. In order to to write integration tests in the dependent application, I use a schema.sql file to create the stub foreign schema and table.

When booting the application, I can see that the schema.sql file successfully executes, and then the liquibase migration fires off, but fails because it cannot find the foreign schema defined in schema.sql.

This works great in 1.4.2

application.yml

spring:
  profiles: test
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
liquibase:
  url: ${spring.datasource.url}
  change-log: "classpath:/db/changelog/changesets.xml"
  enabled: true
  user: sa
  password:
  drop-first: true

schema.sql

DROP SCHEMA IF EXISTS `foodatabase`;
CREATE SCHEMA `foodatabase`;
CREATE TABLE `foodatabase`.`users` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
);
INSERT INTO foodatabase.users VALUES ();

changeset.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
	xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
	<changeSet
		id="1"
		author="jerome.doby">
		<createTable tableName="user_queues">
			<column
				autoIncrement="true"
				name="id"
				type="INT">
				<constraints primaryKey="true" />
			</column>
			<column
				name="user_id"
				type="INT">
				<constraints
					references="foodatabase.users(id)"
					foreignKeyName="FK_user_queues_user_id"
					unique="true"
					uniqueConstraintName="UNQ_user_queues_user_id" />
			</column>
		</createTable>
	</changeSet>
</databaseChangeLog>

Test class setup

@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({
	"test",
	"test-mock-databunker-agent-client",
	"test-mock-dialer-manager-client",
	"test-mock-customer-matching-client"
})
public class UserQueueControllerTest {
......
}

When the liquibase plugin begins to perform the migration, it can't see the foodatabase schema and throws an exception:

2017-01-04 15:35:51.207  INFO [my-user-queue,,,] 25204 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:33fc0ec2-b352-47d0-a994-495b1648982b;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
2017-01-04 15:35:51.288  INFO [my-user-queue,,,] 25204 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from URL [file:/my/path/my-user-queue/target/test-classes/schema.sql]
2017-01-04 15:35:51.290  INFO [my-user-queue,,,] 25204 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executed SQL script from URL [file:/my/path/my-user-queue/target/test-classes/schema.sql] in 2 ms.
2017-01-04 15:35:51.510  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Successfully acquired change log lock
2017-01-04 15:35:51.510  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Dropping Database Objects in schema: TESTDB.PUBLIC
2017-01-04 15:35:51.545  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Successfully released change log lock
2017-01-04 15:35:51.552  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Successfully acquired change log lock
2017-01-04 15:35:51.592  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Creating database history table with name: PUBLIC.DATABASECHANGELOG
2017-01-04 15:35:51.595  INFO [my-user-queue,,,] 25204 --- [           main] liquibase                                : /db/changelog/changesets.xml::1::jerome.doby: Reading from PUBLIC.DATABASECHANGELOG
2017-01-04 15:35:51.599 ERROR [my-user-queue,,,] 25204 --- [           main] liquibase                                : classpath:/db/changelog/master.xml: /db/changelog/changesets.xml::1::jerome.doby: Change Set /db/changelog/changesets.xml::1::jerome.doby failed.  Error: org.h2.jdbc.JdbcSQLException: Schema "FOODATABASE" not found; SQL statement:
CREATE TABLE PUBLIC.user_queues (id INT AUTO_INCREMENT NOT NULL, meta__version INT DEFAULT 1, meta__created_at TIMESTAMP, meta__last_updated_at TIMESTAMP, meta__trace_id VARCHAR(36), meta__span_id VARCHAR(36), user_id INT, queue_time INT, queue_status VARCHAR(255), CONSTRAINT PK_USER_QUEUES PRIMARY KEY (id), CONSTRAINT FK_user_queues_user_id FOREIGN KEY (user_id) REFERENCES foodatabase.users(id), CONSTRAINT UNQ_user_queues_user_id UNIQUE (user_id)) [90079-193]

I seem to be able to work-around this new issue by using the replace = AutoConfigureTestDatabase.Replace.NONE attribute in my @AutoConfigureTestDatabase, but this is not ideal since I have several classes in my test suite, and want to ensure they start with a clean slate every time.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 4, 2017
@snicoll
Copy link
Member

snicoll commented Jan 5, 2017

@XaeroDegreaz I don't think (yet) it's broken. #7473 is legit but I expected side effects for sure.

Can you please remove the liquibase settings for the url? You're basically asking liquibase to run on that database while we replace it with something else. The auto-config will automatically find the datasource for you, no need for that stuff.

If that does not work, rather than pasting some code in a comment, can you please share a project that I can run?

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jan 5, 2017
@XaeroDegreaz
Copy link
Author

Thanks, I can confirm that removing url: ${spring.datasource.url} fixes the issue.

Seems that it was working before, but only because of a bug?

@snicoll
Copy link
Member

snicoll commented Jan 10, 2017

Seems that it was working before, but only because of a bug?

Yes and no. Previously we were "replacing" the database with the same settings as the default datasource you get with auto-configuration. This is obviously wrong since in case you let the auto-configuration kicks in, we were not replacing anything. Also, your url is exactly what we do by default. If you replace testdb with foo it would have been broken as well.

Thanks for confirming though.

@snicoll snicoll closed this as completed Jan 10, 2017
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Jan 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants