使用Flyway根据环境有条件地加载数据

fan*_*nly 13 flyway

flyway是否支持每个环境的脚本条件执行?

例如,如果我有测试数据,我是否可以创建一个仅在env配置为test时才加载的测试数据脚本文件夹?

Bus*_*ata 9

对于将来的访问者,这是针对数据库特定的sql的解决方案,但也适用于数据加载.https://flywaydb.org/documentation/faq#db-specific-sql

您可以设置flyway.locations = sql/common,sql/data属性,并且可以使用spring配置文件(dev/test/prod)将其设置为不同的值,省略生产中的sql/data脚本.

  • 当你使用spring boot时,它已经更新为`spring.flyway.locations=classpath:sql/common,classpath:sql/data`。对于版本 flyway-core:5.0.7 (5认同)

Ria*_*ian 5

Maven 配置文件没有给我想要的灵活性。我想出了一个使用 ant 合并文件的策略。这允许我拥有通用脚本,然后根据部署类型包含其他数据,例如。生产,开发。

这是我的项目结构:

??? 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
Run Code Online (Sandbox Code Playgroud)

根文件夹中的database.properties.template 文件,在运行之前必须手动将其复制到database.properties 并输入用户名和密码。database.properties 应该被 VCS 忽略,这样密码就不会出现在 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>
Run Code Online (Sandbox Code Playgroud)

这个 ant 文件由 maven 执行,这里是 pom 配置:

<?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>
Run Code Online (Sandbox Code Playgroud)

如果您需要不同的客户端分布的不同数据,您可以将 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>
Run Code Online (Sandbox Code Playgroud)


Sah*_*oob 4

如果您使用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>
Run Code Online (Sandbox Code Playgroud)