Where to put Flyway migrations in a Vaadin project

2019-09-10 09:59发布

Where does one put their Flyway migration files in a Vaadin 7 project created with the multi-module Maven archetype?

I will activate the migrations through the Java API in Flyway (not the command-line).

Might the solution for Vaadin work in any Java Servlet based web app project running in a web container such as Tomcat or Jetty?

1条回答
【Aperson】
2楼-- · 2019-09-10 10:38

Update

As of 2018-06, the Vaadin 8 archetype named vaadin-archetype-application-multimodule no longer provides an existing folders for Web Pages, META-INF, and WEB-INF as seen below in the Previously section. The WEB-INF and classes folders are created dynamically at build-time.

Look to your foobar-ui module’s folder. Locate the src > main > resources folder hierarchy. Add the nested folders db & migration following Flyway conventions.

screen shot showing nested folders "db" and "migration" added to the "ui" module’s “src > main > resources” folder hierarchy.

If we explode the resulting WAR file, we see that during the build process a WEB-INF folder was created, with a nested classes folder, where we find our db > migration folder containing our first added SQL script file.

screenshot of file browser showing the exploded WAR file’ contents that includes a folder hierarchy of <code>WEB-INF</code> > <code>classes</code> > <code>db</code> > <code>migration</code> containing our added SQL script file

Previously

Prior versions of the multi-module Maven archetype provided by Vaadin Ltd. used a different arrangement than that seen above.

Default folder nested in WEB-INF

Flyway by default will find your .sql migration files if located in this exact folder path:

/WEB-INF/classes/db/migration/

enter image description here

In your project, in the -ui module, locate the WEB-INF folder. The Java Servlet spec defines this special folder as a place to store class files, .jar files, and other resources that should be made available to your servlets but always shield from outside, never available as a public part of the web app.

That WEB-INF folder will contain classes & lib folder. You may not yet see these two folders in your IDE’s project view. These two special folders are created during the compiling and build process. The first houses individual .class files for your web app, while lib houses .jar files. Again, all this is defined by the Servlet spec.

The Trick: Manually create the classes folder in your IDE if not shown. What you place inside will be automatically merged with the other content destined for the classes folder when created during the compiling/build process.

Flyway by default looks on the classpath for a hierarchy named exactly /db/migration/. So create folders by these names inside that classes folder you added to WEB-INF. Now when Flyway looks on the classpath at runtime, Flyway will look in /WEB-INF/classes and find /db/migration/.

So in your IDE:

  1. Create a folder named classes in your project’s WEB-INF.
  2. Create a nested folder named db.
  3. Create a nested folder named migration.
  4. Place your .sql files within that migration folder.

Run your Flyway migration. Here is example code using the H2 Database called in the contextInitialized method of my class implementing [ServletContextListener][3].

Flyway flyway = new Flyway ();
flyway.setDataSource ( "jdbc:h2:~/test" , "scott" , "tiger" );
flyway.migrate ();

Caveat

Take this Answer with a grain of salt as I am new to Flyway. But this is working for me using Flyway 4, Vaadin 7.6.4, Java 1.8 Update 77, NetBeans 8.1, Apache Tomcat 8.0.27, Mac OS X El Capitan.

查看更多
登录 后发表回答