Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway database migration to run automatically when new war deployed

I would like Flyway to run whenever I deploy a new war to my server.

Does flyway automatically get run when a server is deployed? Do I have to always automate a script which would then the flyway migration command? Or what is the best way to do this?

Server:

The server is a Java Tomcat Server running on Elastic Beanstalk (AWS) that is connected to a MySQL database.

Deployment Process

We run our sql migration scripts on the database manually. Then we upload a new war of the server to Elastic Beanstalk.

like image 204
tt_Gantz Avatar asked May 11 '16 07:05

tt_Gantz


Video Answer


2 Answers

As the comments said, there may be multiple ways to do this.

ServletContextListener

One common way is to use the hook defined by the Java Servlet spec for being notified when your web app is launching and shutting-down. That hook is the ServletContextListener interface. Add a class to your project implementing the two methods in this interface, one for launch and one for shutdown. In the launch method, run your Flyway code.

The word “context” is the technical term meaning your web app.

  • contextInitialized
    Your web app is launching. No incoming web request has yet been handled, and will not be handled until your implementation of this method completes. Run your Flyway migrations here.
  • contextDestroyed
    Your web app is shutting down. The last remaining web request has been serviced, and no more will be accepted.

Annotating this class with @WebListener is the easiest of multiple ways to get your Servlet container to register an instance.

Pretty easy.

Your ServletContextListener is guaranteed to be called and run to completion before the first execution of any Servlet (or Filter) in your web app. So this is the perfect place to do setup work that you want finished before your servlets go to work. Flyway seems like a natural fit to me.

Search Stack Overflow for “ServletContextListener” to learn more and see examples, such as my own Question & Answer.

Handling failure

Be aware that stopping a web app’s deployment when something goes wrong (when your ServletContextListener encounters an Exception) is not well-defined in the Servlet spec.

An example might be your Flyway migrations failing for some reason, such as not able to connect to database. At that point you might want to halt deployment of your web app.

See my own Question and Answer and the group of related questions I list in that answer. Tomcat 8.0.33 halts the deployment, and un-deploys the web app, but unfortunately does not report the offending Exception (or at least I could not find any such report in the logs nor in the IDE console while in development mode). The behavior of other Servlet containers may vary.

like image 56
Basil Bourque Avatar answered Oct 19 '22 19:10

Basil Bourque


This can be useful:

Auto-migration on startup : https://flywaydb.org/documentation/api/

So for Java all it takes is to create scripts (eg. V1__initial_schema.sql, ...), put them under /src/main/resources/db/migration/ and then:

Flyway flyway = new Flyway();
flyway.setDataSource(...);
flyway.migrate();
like image 34
Patrycja K Avatar answered Oct 19 '22 18:10

Patrycja K