flyway是否支持每个环境的脚本条件执行?
例如,如果我有测试数据,是否可以创建仅在env配置为test时才加载的测试数据脚本文件夹?
答案 0 :(得分:8)
对于将来的访问者,这是针对特定于数据库的SQL的解决方案,但也适用于数据加载。 https://flywaydb.org/documentation/faq#db-specific-sql
您可以设置flyway.locations = sql / common,sql / data属性,并且可以使用spring配置文件(dev / test / prod)将其设置为不同的值,省略生产中的sql / data脚本。
答案 1 :(得分:3)
如果你正在使用maven,你可以通过maven profile概念轻松实现它。请参考以下示例
<强> 的pom.xml 强>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.3</version>
<configuration>
<url>jdbc:sqlserver://${db.hostname};databaseName=${db.name}</url>
<user>${db.username}</user>
<password>${db.password}</password>
<initVersion>0</initVersion>
<initDescription>Base Migration</initDescription>
<table>Changelog_testproject</table>
<locations>
<location>filesystem:${sql.file.path}</location>
</locations>
</configuration>
</plugin>
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.name>dev</profile.name>
<sql.file.path>${basedir}/deploy/dev/sqldelta/sqlserver</sql.file.path>
<db.hostname>127.0.0.1:1433</db.hostname>
<db.name>dev</db.name>
<db.username>dev</db.username>
<db.password>devadmin</db.password>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.name>test</profile.name>
<sql.file.path>${basedir}/deploy/test/sqldelta/sqlserver</sql.file.path>
<db.hostname>127.0.0.1:1433</db.hostname>
<db.name>test</db.name>
<db.username>test</db.username>
<db.password>testadmin</db.password>
</properties>
</profile>
</profiles>
答案 2 :(得分:3)
Maven个人资料并没有给我我想要的灵活性。我想出了一个使用ant来合并文件的策略。这允许我拥有通用脚本,然后根据部署类型包含其他数据,例如。 prod,dev。
这是我的项目结构:
├── build.xml
├── database.properties
├── database.properties.template
├── lib
│ └── ant-contrib-1.0b3.jar
├── pom.xml
└── sql
├── common
│ ├── V1.0__.sql
│ ├── V1.2.1__.sql
│ └── V1.3__.sql
├── dev
│ ├── V1.0__.sql
│ └── V1.3__.sql
└── prod
└── V1.0__.sql
在根文件夹中的database.properties.template文件,在运行之前必须手动将其复制到database.properties并输入用户名和密码。 VCS应忽略database.properties,以便密码不会在repo中结束。
将deployType脚本合并到src/main/resources/db/migrate
目录中,这是执行该操作的ant脚本,请注意要合并的脚本具有相同的名称:
<project name="data" default="prepareSql">
<property file="database.properties" />
<property name="destDir" value="src/main/resources/db/migration" />
<echo message="Deploy type: ${deployType}"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="prepareSql">
<!-- ensure the dest dir exists -->
<mkdir dir="${destDir}"/>
<!-- clear out the dest dir -->
<delete>
<fileset dir="${destDir}">
<include name="*" />
</fileset>
</delete>
<!-- append the deploy type files to the common files, delegate to the append target -->
<foreach target="append" param="file">
<fileset dir="sql/common" casesensitive="yes">
<include name="*" />
</fileset>
</foreach>
</target>
<target name="append">
<basename property="basename" file="${file}" />
<property name="destFile" value="${destDir}/${basename}"/>
<echo message="Appending ${file} to ${destFile}" />
<concat destfile="${destFile}" >
<filelist dir="sql/common" files="${basename}" />
<filelist dir="sql/${deployType}" files="${basename}" />
</concat>
</target>
</project>
这个ant文件由maven执行,这里是pom config:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>data</groupId>
<artifactId>data</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>data</name>
<properties>
<sqlBaseDir>filesystem:${basedir}/src/main/resources/</sqlBaseDir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>mergeScripts</id>
<phase>validate</phase>
<inherited>false</inherited>
<configuration>
<target>
<ant antfile="build.xml" target="prepareSql" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<schemas>
<schema>common</schema>
</schemas>
<configFile>database.properties</configFile>
<table>flyway</table>
<locations>
<location>filesystem:${basedir}/src/main/resources/db/migration</location>
</locations>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
</project>
如果您需要为不同的客户端发行版提供不同的数据,您可以将dist目录添加到sql目录中,它们可以包含deployType子目录。
将dist属性添加到database.properties.template文件中,然后将build.xml中的append目标修改为如下所示:
<target name="append">
<basename property="basename" file="${file}" />
<property name="destFile" value="${destDir}/${basename}"/>
<echo message="Appending ${file} to ${destFile}" />
<concat destfile="${destFile}" >
<filelist dir="sql/common" files="${basename}" />
<filelist dir="sql/${deployType}" files="${basename}" />
<filelist dir="sql/${dist}/common" files="${basename}" />
<filelist dir="sql/${dist}/${deployType}" files="${basename}" />
</concat>
</target>
答案 3 :(得分:1)
您可以使用 flyway占位符:
在您的环境config.properties上配置它:
flyway.placeholders。的表名强> = MY_TABLE
flyway.placeholders的名称强> =&#39;先生。测试&#39;
然后,把它放在你的脚本上: 插入 $ {tableName} (名称)VALUES( $ {name} );
我也使用了flyway.locations,但占位符比简单更改的位置更简单。