title: Setup h2 in spring boot application with r2dbc and flyway
body: <p>I'm playing around with Spring Boot and the reactive jdbc driver called r2dbc. In my main application I'm using Postgres as a database and now I want to the use h2 for the tests. And the Flyway migration is working with the setup but when the Spring application is able to insert records.</p>
  <p>Here is my setup and code</p>
  <pre><code>@SpringBootTest
  class CustomerRepositoryTest {
  
      @Autowired
      CustomerRepository repository;
  
      @Test
      void insertToDatabase() {
          repository.saveAll(List.of(new Customer(&quot;Jack&quot;, &quot;Bauer&quot;),
                  new Customer(&quot;Chloe&quot;, &quot;O'Brian&quot;),
                  new Customer(&quot;Kim&quot;, &quot;Bauer&quot;),
                  new Customer(&quot;David&quot;, &quot;Palmer&quot;),
                  new Customer(&quot;Michelle&quot;, &quot;Dessler&quot;)))
                  .blockLast(Duration.ofSeconds(10));
      }
  }
  </code></pre>
  <p>Here is the <strong>error</strong> that I'm getting</p>
  <pre><code> :: Spring Boot ::        (v2.3.4.RELEASE)
  
  2020-10-14 15:59:18.538  INFO 25279 --- [           main] i.g.i.repository.CustomerRepositoryTest  : Starting CustomerRepositoryTest on imalik8088.fritz.box with PID 25279 (started by imalik in /Users/imalik/code/private/explore-java/spring-example)
  2020-10-14 15:59:18.540  INFO 25279 --- [           main] i.g.i.repository.CustomerRepositoryTest  : No active profile set, falling back to default profiles: default
  2020-10-14 15:59:19.108  INFO 25279 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data R2DBC repositories in DEFAULT mode.
  2020-10-14 15:59:19.273  INFO 25279 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 160ms. Found 1 R2DBC repository interfaces.
  2020-10-14 15:59:19.894  INFO 25279 --- [           main] o.f.c.internal.license.VersionPrinter    : Flyway Community Edition 6.5.0 by Redgate
  2020-10-14 15:59:20.052  INFO 25279 --- [           main] o.f.c.internal.database.DatabaseFactory  : Database: jdbc:h2:mem:///DBNAME (H2 1.4)
  2020-10-14 15:59:20.118  INFO 25279 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.022s)
  2020-10-14 15:59:20.131  INFO 25279 --- [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table &quot;PUBLIC&quot;.&quot;flyway_schema_history&quot; ...
  2020-10-14 15:59:20.175  INFO 25279 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema &quot;PUBLIC&quot;: &lt;&lt; Empty Schema &gt;&gt;
  2020-10-14 15:59:20.178  INFO 25279 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema &quot;PUBLIC&quot; to version 1.0.0 - schma
  2020-10-14 15:59:20.204  INFO 25279 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema &quot;PUBLIC&quot; (execution time 00:00.036s)
  2020-10-14 15:59:20.689  INFO 25279 --- [           main] i.g.i.repository.CustomerRepositoryTest  : Started CustomerRepositoryTest in 2.466 seconds (JVM running for 3.326)
  
  2020-10-14 15:59:21.115 DEBUG 25279 --- [           main] o.s.d.r2dbc.core.DefaultDatabaseClient   : Executing SQL statement [INSERT INTO customer (first_name, last_name) VALUES ($1, $2)]
  
  
  org.springframework.data.r2dbc.BadSqlGrammarException: executeMany; bad SQL grammar [INSERT INTO customer (first_name, last_name) VALUES ($1, $2)]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [42102] [42S02] Tabelle &quot;CUSTOMER&quot; nicht gefunden
  Table &quot;CUSTOMER&quot; not found; SQL statement:
  INSERT INTO customer (first_name, last_name) VALUES ($1, $2) [42102-200]
  </code></pre>
  <p>My src/test/resources/application.yaml is looking like this:</p>
  <pre><code>spring:
    r2dbc:
      url: r2dbc:h2:mem:///DBNAME?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
      username: sa
      password:
  
    flyway:
      url: jdbc:h2:mem:///DBNAME
      baseline-on-migrate: true
      user: sa
      password:
  </code></pre>
  <p>Any ideas whats missing missing or whats wrong with the setup? If further information is needed please let me know.</p>
  <h3>Addition/Solution:</h3>
  <p>The url pattern is different between jdbc and r2dbc. The working solution for me is as follows:</p>
  <pre><code>url: r2dbc:h2:file:///./tmp/test-database
  url: jdbc:h2:file:./tmp/test-database
  </code></pre>
  <p>And In order to setup Flyway you have to Configure Flyway:</p>
  <pre><code>// Flyway is not compatible with r2dbc yet, therefore this config class is created
  @Configuration
  public class FlywayConfig {
  
      private final Environment env;
  
      public FlywayConfig(final Environment env) {
          this.env = env;
      }
  
      @Bean(initMethod = &quot;migrate&quot;)
      public Flyway flyway() {
          return new Flyway(Flyway.configure()
                  .baselineOnMigrate(true)
                  .dataSource(
                          env.getRequiredProperty(&quot;spring.flyway.url&quot;),
                          env.getRequiredProperty(&quot;spring.flyway.user&quot;),
                          env.getRequiredProperty(&quot;spring.flyway.password&quot;))
          );
      }
  }
  </code></pre>