How to integrate Flyway into our spring-batch jdbc application

Multi tool use
How to integrate Flyway into our spring-batch jdbc application
We have a spring-batch application with DB2 jdbc data source. Want to add Flyway migration capabilities to our app. I've been exploring this article, it makes perfect sense, except the section that mentions how to specify the 'entityManagerFactory' - their example is for JPA with Hibernate,and it looks like this:
<!-- Entity Manager Factory configuration -->
<bean id="entityManagerFactory" class="o.s.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="o.s.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="${jpa.database}"/>
</bean>
</property>
</bean>
Our app is simple JDBC datasource for db2. How can I define that <bean id="entityManagerFactory"
so that Spring recognizes it as a managed Bean? Do I even need to specify the entityManagerFactory bean configuration?
<bean id="entityManagerFactory"
1 Answer
1
No, you don't have to specify the entityMangerFactory bean. The flyway migrations don't have to be beans.
This is an example configuration for flyway migrations:
@Configuration
public class FlywayInitializer {
@Autowired
private DataSource dataSource;
@PostConstruct()
private void startMigrations() {
Flyway flyway = new Flyway();
flyway.setDataSource(dataSource);
flyway.setLocations("db/migrations");
flyway.setSchemas("public");
flyway.setSqlMigrationPrefix("H");
flyway.migrate();
}
}
We start by creating a new Flyway object. The javax.Sql.DataSource
is the only bean that flyway needs. Flyway needs the data from this bean so it can connect to the database.
Then we configure the locations where the migrations are located, the schemas for flyway (the first schema is the one where the schema_version
table will be created) and the migration prefix for the migrations (for example, my migrations look like this: H1__init_db.sql).
javax.Sql.DataSource
schema_version
There are also a lot of other properties that can be set. Once you are done with configuring the flyway object, you call the migrate
method in order to execute the migrations.
migrate
I am glad that it worked for you @JamesD. Please consider accepting the answer (by clicking on the checkmark bellow the upvote/downvote buttons) so other people with similar problem will know that this answer is a solution to the problem.
– Dvorog
Jul 2 at 13:03
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Fantastic - thanks very much
– JamesD
Jul 2 at 12:37