简体   繁体   English

如何从 Gradle 依赖项运行 Flyway 迁移?

[英]How to run Flyway migrations from a Gradle dependency?

I've got two Spring Boot webservices that must both use the same database, same schema and same tables - one service reads from the tables, the other writes to them.我有两个 Spring 启动 Web 服务,它们必须都使用相同的数据库、相同的模式和相同的表——一个服务从表中读取,另一个向它们写入。 Right now one of the services is in development and works with an in-memory DB instead of the real DB, so I can have each service contain its own copy of the database migrations.现在其中一项服务正在开发中,并且与内存数据库而不是真实数据库一起使用,因此我可以让每个服务都包含自己的数据库迁移副本。 Once both services are working with the same DB, that won't work - the first one that gets started will run its migrations and the other one will fail.一旦两个服务都在同一个数据库上工作,这将不起作用——第一个启动的服务将运行其迁移,而另一个将失败。 But both projects still have to have the migrations, because they're needed to create in-memory databases for automated tests.但是这两个项目仍然需要进行迁移,因为需要它们来创建内存数据库以进行自动化测试。

As a solution, I'm making a third project whose only responsibility would be to run the migrations, and both original applications would pull the migrations from this new one, but I'm stuck on how to make that happen.作为一种解决方案,我正在制作第三个项目,其唯一职责是运行迁移,两个原始应用程序都会从这个新应用程序中提取迁移,但我一直坚持如何实现这一目标。

The closest thing to what I need that I've seen is this code from another SO question :我见过的最接近我需要的是另一个 SO question中的代码:

sourceSets {
    main {
        resources {
            srcDirs += [
                project(':data').sourceSets.main.resources
            ]
        }
    }
}

But this is for including resources from another local project, not from a gradle dependency.但这是为了包含来自另一个本地项目的资源,而不是来自 gradle 依赖项的资源。 I've got my private Maven repo listed and the dependency resolved, but I'm stumped on what to write instead of the inner line of that block above.我已经列出了我的私人 Maven 存储库并解决了依赖关系,但我不知道要写什么而不是上面那个块的内行。

repositories {
    mavenCentral()
    maven {
        url "http://my.repo.link/"
    }
}

dependencies {
    // other dependencies 
    runtime 'my.group:database-project:1.0'
}

If you are using a Gradle multi-project layout and you like to use the Flyway plugin, I guess you can just configure the migration folder directly.如果您使用的是 Gradle 多项目布局并且您喜欢使用 Flyway 插件,我想您可以直接配置迁移文件夹。 Something like this:像这样的东西:

flyway {
  locations = ["filesystem:$rootDir/data/migrations"]
}

If you are using stand-alone projects, you can package your migrations into a jar file using the java plugin, publish it to a Maven repository and make a normal dependency to that. If you are using stand-alone projects, you can package your migrations into a jar file using the java plugin, publish it to a Maven repository and make a normal dependency to that. If you don't want to put your migrations on the application classpath, you can put the dependency in a custom configuration.如果您不想将迁移放在应用程序类路径中,可以将依赖项放在自定义配置中。 And if you have a non-standard folder structure, you can configure that through a classpath: value in the locations parameter.如果你有一个非标准的文件夹结构,你可以通过一个classpath: locations参数中的值。 Something like this:像这样的东西:

configurations {
    flywayMigration
}

dependencies {
    flywayMigration "com.group:database-project:1.0" // Jar file containing your migration scripts
}

flyway {
    configurations = ["flywayMigration"]
    locations = ["classpath:db/migrations"]
}

Unless I misunderstand your requirements, you don't need a third party project for applying the migrations.除非我误解了您的要求,否则您不需要第三方项目来应用迁移。 (Though you still can if you want, of cause.) In my project, we are in a similar situation, and we have all our Spring Boot applications bundle the migrations scripts with them, and then on start-up we problematically apply the migrations to the database. (当然,如果你愿意,你仍然可以。)在我的项目中,我们处于类似的情况,我们所有的 Spring 引导应用程序都将迁移脚本与它们捆绑在一起,然后在启动时我们有问题地应用迁移到数据库。 In this scenario, on a clean database, the first application will perform all migrations, and all subsequent ones will just verify that everything is OK.在这种情况下,在一个干净的数据库上,第一个应用程序将执行所有迁移,所有后续迁移都将验证一切正常。 Flyway supports parallel migrations as well, so there should not be any concurrency issues. Flyway 也支持并行迁移,因此不应该存在任何并发问题。 You can achieve a similar thing using applying the Flyway plugin to all Boot projects and then make a task dependency from bootRun to flywayMigrate.您可以通过将 Flyway 插件应用于所有 Boot 项目,然后将任务依赖关系从 bootRun 到 flywayMigrate 来实现类似的事情。

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

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