<?xml version="1.0"?>
<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">
    <parent>
        <groupId>io.debezium</groupId>
        <artifactId>debezium-parent</artifactId>
        <version>0.2.3</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>debezium-connector-mysql</artifactId>
    <name>Debezium Connector for MySQL</name>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>io.debezium</groupId>
            <artifactId>debezium-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.shyiko</groupId>
            <artifactId>mysql-binlog-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>connect-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- Testing -->
        <dependency>
            <groupId>io.debezium</groupId>
            <artifactId>debezium-core</artifactId>
            <type>test-jar</type>
        </dependency>
        <dependency>
            <groupId>io.debezium</groupId>
            <artifactId>debezium-embedded</artifactId>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.debezium</groupId>
            <artifactId>debezium-embedded</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert</artifactId>
        </dependency>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-connect-avro-converter</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <!-- 
        Specify the properties that will be used for setting up the integration tests' Docker container.
        Note that the `dockerhost.ip` property is computed from the IP address of DOCKER_HOST, which will
        work on all platforms. We'll set some of these as system properties during integration testing.
        -->
        <database.port>3306</database.port>
        <database.gtid.port>3306</database.gtid.port>
        <database.alt.port>3306</database.alt.port>
        <database.user>mysqluser</database.user>
        <database.password>mysqlpw</database.password>
        <!--
        By default, we should use the docker image maintained by the MySQL team. This property is changed with different profiles.
        However, we run one container with GTIDs and one without.
        -->
        <docker.image>debezium/mysql-server-test-database</docker.image>
        <docker.skip>false</docker.skip>
    </properties>
    <build>
        <plugins>
            <plugin>
              <groupId>io.fabric8</groupId>
              <artifactId>docker-maven-plugin</artifactId>
              <configuration>
                <watchInterval>500</watchInterval>
                <logDate>default</logDate>
                <verbose>true</verbose>
                <autoPull>always</autoPull>
                <images>
                  <image>
                    <!-- A Docker image using a partial MySQL installation maintained by MySQL team. -->
                    <name>debezium/mysql-server-test-database</name>
                    <alias>database</alias>
                    <run>
                      <namingStrategy>alias</namingStrategy>
                      <env>
                        <MYSQL_ROOT_PASSWORD>debezium-rocks</MYSQL_ROOT_PASSWORD>
                        <MYSQL_DATABASE>mysql</MYSQL_DATABASE> <!-- database created upon init -->
                        <MYSQL_USER>${database.user}</MYSQL_USER>
                        <MYSQL_PASSWORD>${database.password}</MYSQL_PASSWORD>
                      </env>
                      <ports>
                        <port>${database.port}:3306</port>
                      </ports>
                      <log>
                        <prefix>mysql</prefix>
                        <enabled>true</enabled>
                        <color>yellow</color>
                      </log>
                      <wait>
                        <log>MySQL init process done. Ready for start up.</log>
                        <time>30000</time> <!-- 30 seconds max -->
                      </wait>
                    </run>
                    <build>
                      <from>mysql/mysql-server:${version.mysql.server}</from>
                      <assembly>
                        <inline>
                          <fileSets>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/server</directory>
                              <includes>
                                <include>my.cnf</include>
                              </includes>
                              <outputDirectory>etc/mysql</outputDirectory>
                            </fileSet>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/init</directory>
                              <outputDirectory>docker-entrypoint-initdb.d</outputDirectory>
                            </fileSet>
                          </fileSets>
                        </inline>
                        <basedir>/</basedir>
                      </assembly>
                    </build>
                  </image>
                  <image>
                    <!-- A Docker image using a partial MySQL installation maintained by MySQL team. -->
                    <name>debezium/mysql-server-gtids-test-database</name>
                    <alias>database-gtids</alias>
                    <run>
                      <namingStrategy>alias</namingStrategy>
                      <env>
                        <MYSQL_ROOT_PASSWORD>debezium-rocks</MYSQL_ROOT_PASSWORD>
                        <MYSQL_DATABASE>mysql</MYSQL_DATABASE> <!-- database created upon init -->
                        <MYSQL_USER>${database.user}</MYSQL_USER>
                        <MYSQL_PASSWORD>${database.password}</MYSQL_PASSWORD>
                      </env>
                      <ports>
                        <port>${database.gtid.port}:3306</port>
                      </ports>
                      <log>
                        <prefix>mysql-gtids</prefix>
                        <enabled>true</enabled>
                        <color>cyan</color>
                      </log>
                      <wait>
                        <log>MySQL init process done. Ready for start up.</log>
                        <time>30000</time> <!-- 30 seconds max -->
                      </wait>
                    </run>
                    <build>
                      <from>mysql/mysql-server:${version.mysql.server}</from>
                      <assembly>
                        <inline>
                          <fileSets>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/server-gtids</directory>
                              <includes>
                                <include>my.cnf</include>
                              </includes>
                              <outputDirectory>etc/mysql</outputDirectory>
                            </fileSet>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/init</directory>
                              <outputDirectory>docker-entrypoint-initdb.d</outputDirectory>
                            </fileSet>
                          </fileSets>
                        </inline>
                        <basedir>/</basedir>
                      </assembly>
                    </build>
                  </image>
                  <image>
                    <!-- A Docker image using a complete MySQL installation maintained by Docker team. -->
                    <name>debezium/mysql-test-alt-database</name>
                    <alias>database-alt</alias>
                    <run>
                      <namingStrategy>alias</namingStrategy>
                      <env>
                        <MYSQL_ROOT_PASSWORD>debezium-rocks</MYSQL_ROOT_PASSWORD>
                        <MYSQL_DATABASE>mysql</MYSQL_DATABASE> <!-- database created upon init -->
                        <MYSQL_USER>${database.user}</MYSQL_USER>
                        <MYSQL_PASSWORD>${database.password}</MYSQL_PASSWORD>
                      </env>
                      <ports>
                        <port>${database.alt.port}:3306</port>
                      </ports>
                      <log>
                        <prefix>alt-mysql</prefix>
                        <enabled>true</enabled>
                        <color>magenta</color>
                      </log>
                      <wait>
                        <log>socket: \'/var/run/mysqld/mysqld\.sock\'  port: 3306  MySQL Community Server \(GPL\)</log>
                        <time>30000</time>
                      </wait>
                    </run>
                    <build>
                      <from>mysql:${version.mysql.server}</from>
                      <assembly>
                        <inline>
                          <fileSets>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/alt-server</directory>
                              <includes>
                                <include>my.cnf</include>
                              </includes>
                              <outputDirectory>etc/mysql/conf.d</outputDirectory>
                            </fileSet>
                            <fileSet>
                              <directory>${project.basedir}/src/test/docker/init</directory>
                              <outputDirectory>docker-entrypoint-initdb.d</outputDirectory>
                            </fileSet>
                          </fileSets>
                        </inline>
                        <basedir>/</basedir>
                      </assembly>
                    </build>
                  </image>
                </images>
              </configuration>
              <!--
              Connect this plugin to the maven lifecycle around the integration-test phase:
              start the container in pre-integration-test and stop it in post-integration-test.
              -->
              <executions>
                <execution>
                  <id>start</id>
                  <phase>pre-integration-test</phase>
                  <goals>
                    <goal>build</goal>
                    <goal>start</goal>
                  </goals>
                </execution>
                <execution>
                  <id>stop</id>
                  <phase>post-integration-test</phase>
                  <goals>
                    <goal>stop</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
            <!-- 
            Unlike surefire, the failsafe plugin ensures 'post-integration-test' phase always runs, even
            when there are failed integration tests. We rely upon this to always shut down the Docker container
            after the integration tests (defined as '*IT.java') are run.
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <skipTests>${skipITs}</skipTests>
                    <enableAssertions>true</enableAssertions>
                    <systemPropertyVariables>
                        <!-- Make these available to the tests via system properties -->
                        <database.hostname>${docker.host.address}</database.hostname>
                        <database.port>${database.port}</database.port>
                        <database.user>${database.user}</database.user>
                        <database.password>${database.password}</database.password>
                        <skipLongRunningTests>${skipLongRunningTests}</skipLongRunningTests>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <!-- Apply the properties set in the POM to the resource files -->
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*</include>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*</include>
                    <include>**/*</include>
                </includes>
            </testResource>
        </testResources>
    </build>
    <!--
    Define several useful profiles
    -->
    <profiles>
        <!--  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              This assembly profile is used during official builds. In addition to 
              compiling, and running the unit and integration tests like the non-assembly
              profiles, this provfile creates additional (like the connector plugin archives),
              starts up all three Docker containers (normal MySQL, MySQL+GTIDs, and alt-MySQL)
              and runs the integration tests against each of them.

              To use, specify "-Passembly" on the Maven command line.
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <profile>
            <id>assembly</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
              <!-- Run multiple images at the same time, but use different ports for all MySQL servers -->
              <docker.image>debezium/mysql-server-test-database,debezium/mysql-server-gtids-test-database,debezium/mysql-test-alt-database</docker.image>
              <database.port>3306</database.port>
              <database.gtid.port>4306</database.gtid.port>
              <database.alt.port>5306</database.alt.port>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <dependencies>
                            <dependency>
                                <groupId>io.debezium</groupId>
                                <artifactId>debezium-assembly-descriptors</artifactId>
                                <version>${project.version}</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <id>default</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <finalName>${project.artifactId}-${project.version}</finalName>
                                    <attach>true</attach>  <!-- we want attach & deploy these to Maven -->
                                    <descriptorRefs>
                                        <descriptorRef>connector-distribution</descriptorRef>
                                    </descriptorRefs>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Override the failsafe plugin to run the integration tests twice -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <executions>
                            <!-- First run the integration tests with the non-GTID server -->
                            <execution>
                                <id>integration-test-mysql</id>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                            <!-- Then run the integration tests with the GTID server on different port -->
                            <execution>
                                <id>integration-test-mysql-gtids</id>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                                <configuration>
                                    <systemPropertyVariables>
                                        <database.port>${database.gtid.port}</database.port>
                                    </systemPropertyVariables>
                                </configuration>
                            </execution>
                            <!-- Then run the integration tests with the Docker's MySQL server (without GTIDs) on different port -->
                            <execution>
                                <id>integration-test-alt-mysql</id>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                                <configuration>
                                    <systemPropertyVariables>
                                        <database.port>${database.alt.port}</database.port>
                                    </systemPropertyVariables>
                                </configuration>
                            </execution>
                            <execution>
                                <id>verify</id>
                                <goals>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <skipTests>${skipITs}</skipTests>
                            <enableAssertions>true</enableAssertions>
                            <systemPropertyVariables>
                                <!-- Make these available to the tests via system properties -->
                                <database.hostname>${docker.host.address}</database.hostname>
                                <database.port>${database.port}</database.port>
                                <database.user>${database.user}</database.user>
                                <database.password>${database.password}</database.password>
                                <skipLongRunningTests>false</skipLongRunningTests>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

      <!--  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Do not perform any Docker-related functionality
            To use, specify "-DskipITs" on the Maven command line.
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <profile>
        <id>skip-integration-tests</id>
        <activation>
          <activeByDefault>false</activeByDefault>
          <property>
            <name>skipITs</name>
          </property>
        </activation>
        <properties>
          <docker.skip>true</docker.skip>
        </properties>
      </profile>
      <!--  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Use the alternative Docker image for MySQL.
            To use, specify "-Dalt-mysql" or -Palt-mysql on the Maven command line.
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <profile>
        <id>alt-mysql</id>
        <activation>
          <activeByDefault>false</activeByDefault>
          <property>
            <name>alt-mysql</name>
          </property>
        </activation>
        <properties>
          <docker.image>debezium/mysql-test-alt-database</docker.image>
        </properties>
      </profile>
      <!--  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            Use the Docker image for MySQL configured to use GTIDs.
            To use, specify "-Dgtid-mysql" or -Pgtid-mysql on the Maven command line.
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <profile>
        <id>gtid-mysql</id>
        <activation>
          <activeByDefault>false</activeByDefault>
          <property>
            <name>gtid-mysql</name>
          </property>
        </activation>
        <properties>
          <docker.image>debezium/mysql-server-gtids-test-database</docker.image>
        </properties>
      </profile>
    </profiles>
</project>
