<?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.squareup.keywhiz</groupId>
    <artifactId>keywhiz-parent</artifactId>
    <version>0.7.7</version>
  </parent>

  <artifactId>keywhiz-model</artifactId>
  <name>Keywhiz Model</name>

  <dependencies>
    <dependency>
      <groupId>com.google.code.findbugs</groupId>
      <artifactId>annotations</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.jooq</groupId>
      <artifactId>jooq</artifactId>
    </dependency>
    <dependency>
      <groupId>org.jooq</groupId>
      <artifactId>jooq-meta</artifactId>
    </dependency>
    <dependency>
      <groupId>org.jooq</groupId>
      <artifactId>jooq-codegen</artifactId>
    </dependency>

    <dependency>
      <groupId>com.impossibl.pgjdbc-ng</groupId>
      <artifactId>pgjdbc-ng</artifactId>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>postgres</id>
      <activation>
        <!-- If you change the default database, make sure to also update the server's pom.xml -->
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <skipResetDatabase>false</skipResetDatabase>
        <db.driver>com.impossibl.postgres.jdbc.PGDriver</db.driver>
        <db.create-url>jdbc:pgsql:postgres</db.create-url>
        <db.migrate-url>jdbc:pgsql:keywhizdb_test</db.migrate-url>
        <db.username>${user.name}</db.username>
        <db.migrations-path>postgres/migration</db.migrations-path>
        <jooq.dialect>org.jooq.util.postgres.PostgresDatabase</jooq.dialect>
        <jooq.excludes>pg.*</jooq.excludes>
        <jooq.input-schema>public</jooq.input-schema>
      </properties>
    </profile>

    <profile>
      <id>mysql</id>
      <properties>
        <skipResetDatabase>false</skipResetDatabase>
        <db.driver>com.mysql.jdbc.Driver</db.driver>
        <db.create-url>jdbc:mysql://localhost/?useUnicode=true&amp;characterEncoding=utf8</db.create-url>
        <db.migrate-url>jdbc:mysql://localhost/keywhizdb_test?useUnicode=true&amp;characterEncoding=utf8</db.migrate-url>
        <db.username>root</db.username>
        <db.migrations-path>mysql/migration</db.migrations-path>
        <jooq.dialect>org.jooq.util.mysql.MySQLDatabase</jooq.dialect>
        <jooq.excludes>mysql.*</jooq.excludes>
        <jooq.input-schema>keywhizdb_test</jooq.input-schema>
      </properties>
    </profile>

    <profile>
      <id>h2</id>
      <properties>
        <skipResetDatabase>true</skipResetDatabase>
        <db.driver>org.h2.Driver</db.driver>
        <db.create-url>jdbc:h2:/tmp/h2_data/keywhizdb_test</db.create-url>
        <db.migrate-url>jdbc:h2:/tmp/h2_data/keywhizdb_test</db.migrate-url>
        <db.username>root</db.username>
        <db.migrations-path>h2/migration</db.migrations-path>
        <jooq.dialect>org.jooq.util.h2.H2Database</jooq.dialect>
        <jooq.excludes />
        <jooq.input-schema>PUBLIC</jooq.input-schema>
      </properties>
    </profile>
  </profiles>

  <build>
    <plugins>
      <!-- Creates (or re-creates) a keywhizdb_test postgres database. -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>
        </executions>

        <configuration>
          <skip>${skipResetDatabase}</skip>
          <driver>${db.driver}</driver>
          <url>${db.create-url}</url>
          <username>${db.username}</username>
          <autocommit>true</autocommit>
          <sqlCommand>
            DROP DATABASE IF EXISTS keywhizdb_test;
            CREATE DATABASE keywhizdb_test;
          </sqlCommand>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-maven-plugin</artifactId>

        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>migrate</goal>
            </goals>
          </execution>
        </executions>

        <configuration>
          <driver>${db.driver}</driver>
          <url>${db.migrate-url}</url>
          <user>${db.username}</user>
          <locations>
            <location>filesystem:../server/src/main/resources/db/${db.migrations-path}</location>
          </locations>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen-maven</artifactId>

        <!-- The plugin should hook into the generate goal -->
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>

        <configuration>
          <jdbc>
            <driver>${db.driver}</driver>
            <url>${db.migrate-url}</url>
            <user>${db.username}</user>
          </jdbc>

          <!-- Generator parameters -->
          <generator>
            <name>org.jooq.util.JavaGenerator</name>
            <database>
              <name>${jooq.dialect}</name>
              <includes>.*</includes>
              <excludes>${jooq.excludes}</excludes>
              <inputSchema>${jooq.input-schema}</inputSchema>

              <!-- Registers custom types -->
              <customTypes>
                <customType>
                  <name>OffsetDateTime</name>
                  <type>java.time.OffsetDateTime</type>
                  <converter>keywhiz.model.OffsetDateTimeConverter</converter>
                </customType>
                <customType>
                  <name>java.lang.Boolean</name>
                  <converter>keywhiz.model.TinyIntConverter</converter>
                </customType>
              </customTypes>

              <!-- Forces JOOQ to use custom types in generated code -->
              <forcedTypes>
                <forcedType>
                  <name>OffsetDateTime</name>
                  <types>(?i:timestamp.*)</types>
                </forcedType>
                <forcedType>
                  <name>java.lang.Boolean</name>
                  <!-- mysql doesn't support booleans and uses tinyint instead. -->
                  <types>(?i:tinyint.*)</types>
                </forcedType>
              </forcedTypes>
            </database>
            <target>
              <packageName>keywhiz.jooq</packageName>
            </target>
          </generator>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
