简体   繁体   English

Flyway数据库迁移在部署新战争时自动运行

[英]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. 每当我向服务器部署新战争时,我都希望Flyway能够运行。

Does flyway automatically get run when a server is deployed? 在部署服务器时,flyway会自动运行吗? Do I have to always automate a script which would then the flyway migration command? 我是否必须始终自动化一个脚本,然后是flyway migration命令? 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. 服务器是在Elastic Beanstalk(AWS)上运行的Java Tomcat服务器,它连接到MySQL数据库。

Deployment Process 部署过程

We run our sql migration scripts on the database manually. 我们手动在数据库上运行sql迁移脚本。 Then we upload a new war of the server to Elastic Beanstalk. 然后我们将服务器的新战争上传到Elastic Beanstalk。

This can be useful: 这可能很有用:

Auto-migration on startup : https://flywaydb.org/documentation/api/ 启动时自动迁移: 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: 所以对于Java来说,只需创建脚本(例如,V1__initial_schema.sql,...),将它们放在/ src / main / resources / db / migration /下,然后:

Flyway flyway = new Flyway();
flyway.setDataSource(...);
flyway.migrate();

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. 一种常见的方法是使用Java Servlet规范定义的钩子在Web应用程序启动和关闭时收到通知。 That hook is the ServletContextListener interface. 该钩子是ServletContextListener接口。 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. 在启动方法中,运行您的Flyway代码。

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. 尚未处理传入的Web请求,并且在您完成此方法的实现之前不会处理。 Run your Flyway migrations here. 在此处运行您的Flyway迁移。
  • contextDestroyed
    Your web app is shutting down. 您的网络应用程序正在关闭。 The last remaining web request has been serviced, and no more will be accepted. 最后剩余的Web请求已经过服务,不再接受。

Annotating this class with @WebListener is the easiest of multiple ways to get your Servlet container to register an instance. 使用@WebListener注释此类是使Servlet容器注册实例的最简单的多种方法。

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. 保证在Web应用程序中首次执行任何Servlet(或过滤器)之前调用ServletContextListener并运行完成。 So this is the perfect place to do setup work that you want finished before your servlets go to work. 因此,这是在servlet上班之前完成设置工作的理想场所。 Flyway seems like a natural fit to me. Flyway对我来说似乎很自然。

Search Stack Overflow for “ServletContextListener” to learn more and see examples, such as my own Question & Answer . 搜索“ServletContextListener”的堆栈溢出以了解更多信息并查看示例,例如我自己的问答

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. 请注意,在出现问题时(当ServletContextListener遇到异常时)停止Web应用程序的部署在Servlet规范中没有明确定义。

An example might be your Flyway migrations failing for some reason, such as not able to connect to database. 一个例子可能是您的Flyway迁移由于某种原因失败,例如无法连接到数据库。 At that point you might want to halt deployment of your web app. 此时,您可能希望暂停Web应用程序的部署。

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). Tomcat 8.0.33停止部署,并取消部署Web应用程序,但遗憾的是没有报告有问题的异常(或者至少在开发模式下我无法在日志中或IDE控制台中找到任何此类报告)。 The behavior of other Servlet containers may vary. 其他Servlet容器的行为可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM