0

During testing I'm trying to execute my flyway migrations on an H2 in memory database. But even the call to flyway.baseline() fails with the error message:

Caused by: org.h2.jdbc.JdbcSQLException: Tabelle "flyway_schema_history" nicht gefunden
Table "flyway_schema_history" not found; SQL statement:
SELECT "installed_rank","version","description","type","script","checksum","installed_on","installed_by","execution_time","success" FROM "PUBLIC"."flyway_schema_history" WHERE "installed_rank" > -1 ORDER BY "installed_rank" [42102-197]

I didn't notice this at first glance but if you look at the SQL statement you can see that column and table names are enclosed in double quotes. This seems to be reason for the statement to fail.

I'm now wondering why this happens.

I'm using flyway from within a spring boot application. This is my config for testing:

spring:   datasource:
    url: jdbc:h2:./testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;trace_level_file=3;trace_level_system_out=3
    driver_class_name: org.h2.Driver
    username: sa
    password: sa   jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        show_sql: true
        format_sql: true

  flyway:
      locations: filesystem:sql
      cleanDisabled: true
      baselineVersion: "19700101.001"
      outOfOrder: true
      table: flyway_schema_history

I'm calling the migrations in my tests like this:

@Bean
@Profile("test")
public FlywayMigrationStrategy actuallyExecuteTheMigrations() {
    return new FlywayMigrationStrategy() {
        @Override
        public void migrate(Flyway flyway) {
            // start the H2 server during the test phase, so we have a chance to inspect the database
            try {
                startH2Server();
                startH2WebServer();
            } catch (Exception e) {
                logger.warn("Failure starting H2 server, probably it's already running");
            }
            DataSource dataSource = flyway.getConfiguration().getDataSource();
            try {
                Connection connection = dataSource.getConnection();
                Statement statement = connection.createStatement();
                statement.execute(FileUtils.readFileToString(new File("sql/T20190219_100__special_script_for_test_evironment.sql"), Charset.forName("UTF-8")));
                connection.commit();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            flyway.baseline();
            flyway.migrate();
        }
    };

}

Because I want to do some special setup in the database only for testing, I execute a special SQL script before calling flyway.migrate().

2
  • Why do you do that by your own? Spring Boot Test already starts flyway for you Mar 11, 2019 at 10:33
  • I edited the question because I left out parts of the code where I execute some extra SQL scripts with hard code file names which want to be executed only in test mode.
    – smeier_ec
    Apr 1, 2019 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.