1

I recently work with flyway in springboot and I have such a question. Why flyway did not create a database? I just added a flyway dependency to my build.gradle, which now looks like this.

dependencies {
    implementation 'org.springframework:spring-orm:5.1.5.RELEASE'
    implementation 'org.hibernate.search:hibernate-search-backend-lucene:6.0.0.Beta1'
    implementation 'org.hibernate.search:hibernate-search-mapper-orm:6.0.0.Beta1'
    implementation 'org.postgresql:postgresql:42.2.5'

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.1.8.RELEASE'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'
    compile('org.flywaydb:flyway-core:6.0.6')
}

I configured data source like this:

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:hibernate.properties")
public class HibernateConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        final DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.postgresql.Driver");
        dataSourceBuilder.url("jdbc:postgresql://localhost:5432/geo_test");
        dataSourceBuilder.username("postgres");
        dataSourceBuilder.password("root");
        return dataSourceBuilder.build();
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.test.hibernate.domain");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public HibernateTransactionManager getTransactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    private Properties hibernateProperties() {
        final Properties properties = new Properties();
        properties.put(AvailableSettings.DIALECT, env.getRequiredProperty("hibernate.dialect"));
        properties.put(AvailableSettings.SHOW_SQL, env.getRequiredProperty("hibernate.show_sql"));
        properties.put(AvailableSettings.POOL_SIZE, env.getRequiredProperty("hibernate.connection_pool_size"));
        properties.put("hibernate.search.default_backend", env.getRequiredProperty("hibernate.search.default_backend"));
        properties.put("hibernate.search.backends.myBackend.type", env.getRequiredProperty("hibernate.search.backends.myBackend.type"));
        properties.put(AvailableSettings.HBM2DDL_AUTO, env.getRequiredProperty("hibernate.hbm2ddl.auto"));
        return properties;
    }
}

And here is my hibernate.properties:

hibernate.dialect=org.hibernate.dialect.PostgresPlusDialect
hibernate.search.default_backend=myBackend
hibernate.search.backends.myBackend.type=lucene
hibernate.show_sql=true
hibernate.connection_pool_size=1
hibernate.hbm2ddl.auto=validate

And migration file which located in resources/db/migration:

create sequence hibernate_sequence start 1 increment 1;

create table geo_table (
    point_id int8 not null,
    latitude float8 not null,
    longitude float8 not null,

    primary key (point_id)
);

So, now I have next error:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searchServiceImpl': Unsatisfied dependency expressed through field 'sessionManager'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionManager': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/test/hibernate/config/HibernateConfig.class]: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [geo_table]

upd: application.properties:

spring.flyway.url=jdbc:postgresql://localhost:5432/geo_test
spring.flyway.schemas=public
spring.flyway.user=postgres
spring.flyway.password=root
spring.flyway.baseline-version=1
spring.flyway.locations=classpath:db/migration
spring.flyway.enabled=true

What's can be a problem? Should I configured datasource in application.properties? Or how it can be fixed?

Mefisto_Fell
  • 876
  • 1
  • 10
  • 30

1 Answers1

1

Flyway is expecting there to be an existing database for you to run your migration on. Try creating the database, add the datasource url, username and password. Then rerun your app and the geo_table will be created for you.

Chris Savory
  • 2,597
  • 1
  • 17
  • 27
  • I'm very confused. Your question says "Why flyway did not create a database?" and now your comment says "Database already created." Which one is it? – Chris Savory Oct 18 '19 at 17:51
  • Sorry, I mean flyway did not create a table) – Mefisto_Fell Oct 21 '19 at 09:46
  • In your application logs, do you have any logs from `DbMigrate` before the `Hibernate` logs start? I'm not familiar with Gradle so it is hard for me to tell if you have that flyway dependency configured correctly. – Chris Savory Oct 21 '19 at 20:00
  • Everything fine if I remove d this line `sessionFactory.setPackagesToScan("com.test.geolocation.hibernate.domain");` but in this case hibernate did not see my entities – Mefisto_Fell Oct 22 '19 at 14:23
  • 1
    You don't need that `HibernateConfig` config class. You should be able to let Spring Boot do all that initialization. If your application has `@SpringBootApplication` then it will automatically scan for those entity files. – Chris Savory Oct 22 '19 at 15:02
  • 1
    See https://github.com/spring-projects/spring-boot/tree/master/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-flyway and https://github.com/spring-projects/spring-boot/tree/master/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jpa – Chris Savory Oct 22 '19 at 15:05