Mefisto_Fell
Mefisto_Fell

Reputation: 916

Why flyway did not create a table?

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?

Upvotes: 1

Views: 10644

Answers (1)

Chris Savory
Chris Savory

Reputation: 2755

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.

Upvotes: 1

Related Questions