<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-email-processor-integration-test-parent</artifactId>
        <version>4.6.0-QR-0045</version>
    </parent>

    <artifactId>integration-test-dbsetup</artifactId>
    <packaging>jar</packaging>

    <build>
        <resources>
            <!-- copy the sql setup files and filter properties -->
            <resource>
                <directory>src/sql</directory>
                <filtering>true</filtering>
                <targetPath>${project.build.directory}/sql</targetPath>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>ci-realdb</id>
            <activation>
                <property>
                    <name>db</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin><!-- Settings for all sql-maven-plugin executions that are shared -->
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <dependencies>
                            <!-- Use the JDBC driver dependending on what RDBMS we're running against -->
                            <dependency>
                                <groupId>${db.artifactGroup}</groupId>
                                <artifactId>${db.artifactId}</artifactId>
                                <version>${db.artifactVersion}</version>
                            </dependency>
                        </dependencies>
                        <!-- default config shared by all sql-maven-plugin executions, can be overiden in each execution -->
                        <configuration>
                            <username>${db.admin.username}</username>
                            <password>${db.admin.password}</password>
                            <driver>${jdbc.driver}</driver>
                            <url>${jdbc.url}</url>
                            <autocommit>true</autocommit>
                            <onError>abort</onError>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <!-- Database profiles -->
        <profile>
            <id>Postgres</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>postgres</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <configuration>
                            <username>${db.username}</username>
                            <password>${db.password}</password>
                        </configuration>
                        <executions>
                            <execution>
                                <id>create-db-postgres</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-postgres.sql</srcFile>
                                    </srcFiles>
                                    <!-- do no include the DB name/instance name here, as we need to delete it -->
                                    <url>jdbc:postgresql://${db.host}:${db.port}/</url>
                                    <delimiterType>row</delimiterType>
                                </configuration>
                            </execution>
                            <execution>
                                <!-- this is only for compatibility with Postgres 8.x without plpgsql pre-installed -->
                                <id>create-plpgsql-postgres</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>src/sql/create-plpgsql-postgres.sql</srcFile>
                                    </srcFiles>
                                    <!-- include the dbinstance name as we need to create plpgsql there -->
                                    <url>jdbc:postgresql://${db.host}:${db.port}/${db.instance}</url>
                                    <delimiterType>row</delimiterType>
                                    <enableFiltering>true</enableFiltering>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>import-postgres-dump</id>
                                <!-- making sure that this runs after create-db-postgres and create-plpgsql-postgres -->
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <environmentVariables>
                                        <PGPASSWORD>${db.password}</PGPASSWORD>
                                    </environmentVariables>
                                    <executable>psql</executable>
                                    <arguments>
                                        <argument>-p</argument>
                                        <argument>${db.port}</argument>
                                        <argument>-U</argument>
                                        <argument>${db.username}</argument>
                                        <argument>-h</argument>
                                        <argument>${db.host}</argument>
                                        <argument>-d</argument>
                                        <argument>${db.instance}</argument>
                                        <argument>-f</argument>
                                        <argument>${project.basedir}/src/sql/postgres_dump.sql</argument>
                                        <argument>-q</argument>
                                        <argument>-o</argument>
                                        <argument>${project.build.directory}/postres_import.log</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>Oracle</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>oracle</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>create-db-oracle</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-oracle.sql</srcFile>
                                    </srcFiles>
                                    <delimiterType>row</delimiterType>
                                    <delimiter>/</delimiter>
                                    <enableFiltering>true</enableFiltering>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>MySQL</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>mysql</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>create-db-mysql</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-mysql.sql</srcFile>
                                    </srcFiles>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>MSSQL</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>mssql</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>create-db-mssql</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-mssql.sql</srcFile>
                                    </srcFiles>
                                    <delimiterType>row</delimiterType>
                                    <delimiter>GO</delimiter>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>MSSQL2016</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>mssql2016</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>create-db-mssql</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-mssql.sql</srcFile>
                                    </srcFiles>
                                    <delimiterType>row</delimiterType>
                                    <delimiter>GO</delimiter>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>MSSQL2017</id>
            <activation>
                <property>
                    <name>db</name>
                    <value>mssql2017</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>sql-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>create-db-mssql</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>execute</goal>
                                </goals>
                                <configuration>
                                    <srcFiles>
                                        <srcFile>${project.build.directory}/sql/setup-mssql.sql</srcFile>
                                    </srcFiles>
                                    <delimiterType>row</delimiterType>
                                    <delimiter>GO</delimiter>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
