<?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>org.squashtest.tm</groupId>
    <artifactId>squash-tm</artifactId>
    <version>15.0.0-springboot4.IT3</version>
  </parent>

  <artifactId>squashtest-tm-database</artifactId>
  <packaging>jar</packaging>
  <name>squashtest-tm-database</name>

  <description>SquashTM database definition script. This module produces a jar which is to be used as a "remote
    resource" by poms which need to provision / update a database model (if confused, search for
    "maven-remote-resource-plugin")</description>

  <properties>

    <!--
      Note : the database connection properties are defined in the profiles, near the end of the pom
    -->

    <incremental.updates.changelog>src/main/liquibase/global.changelog-incremental-updates.xml</incremental.updates.changelog>
    <master.changelog>src/main/liquibase/global.changelog-master.xml</master.changelog>
    <migration-test.changelog>src/test/liquibase/global.changelog-migration-test.xml</migration-test.changelog>
    <saxon.version>8.7</saxon.version>

    <!-- Set to true to skip the linter (or use the skip-changeset-linter profile)-->
    <skip.linter>false</skip.linter>

    <sonar.sources>src/main</sonar.sources>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>squash-tm-bom</artifactId>
        <version>${project.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>

    <dependency>
      <groupId>org.dom4j</groupId>
      <artifactId>dom4j</artifactId>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/liquibase</directory>
      </resource>
      <resource>
        <directory>src/main/script</directory>
      </resource>
      <resource>
        <targetPath>../dbdoc</targetPath>
        <filtering>true</filtering>
        <directory>src/main/dbdoc</directory>
      </resource>
    </resources>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.liquibase</groupId>
          <artifactId>liquibase-maven-plugin</artifactId>
          <configuration>
            <dropFirst>true</dropFirst>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <!-- ====================
        Syntax check
      ========================-->
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>${gmavenplus.version}</version>

        <dependencies>
          <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>${groovy.version}</version>
          </dependency>
          <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy-ant</artifactId>
            <version>${groovy.version}</version>
          </dependency>
        </dependencies>
        <executions>

          <!--
          Check that the changeset have no content that would upset a Postgresql database
          -->
          <execution>
            <id>check-postgres-compliance</id>
            <goals>
              <goal>execute</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <scripts>
                <script>${project.basedir}/src/build/script/VerifyPostgreChangelogs.groovy</script>
              </scripts>
            </configuration>
          </execution>

          <!--
           Check that the tablenames are all uppercase (except those who should not)
          -->
          <execution>
            <id>check-tablenames-uppercase</id>
            <goals>
              <goal>execute</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <scripts>
                <script>${project.basedir}/src/build/script/VerifyUpperCaseInScript.groovy</script>
              </scripts>
            </configuration>
          </execution>

          <!--
          Check that each changeset contains a logicalFilePath attribute and the last changeset is the squashtest.tm.database.version update
          -->
          <execution>
            <id>check-logicalPath-and-core-config</id>
            <goals>
              <goal>execute</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
              <scripts>
                <script>${project.basedir}/src/build/script/VerifyLogicalPath.groovy</script>
                <script>${project.basedir}/src/build/script/VerifyChangesetCoreConfig.groovy</script>
              </scripts>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- ====================
        /Syntax check
      ========================-->

      <!-- ====================
        Linter
      ========================-->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>run-linter</id>
            <goals>
              <goal>java</goal>
            </goals>
            <phase>process-classes</phase>
            <configuration>
              <mainClass>org.squashtest.tm.database.linter.Linter</mainClass>
              <classpathScope>compile</classpathScope>
              <skip>${skip.linter}</skip>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- ====================
        /Linter
      ========================-->

      <!-- ====================
        Run configuration check
      ========================-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <executions>
          <execution>
            <id>enforce-configuration</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireProperty>
                  <property>liquibase.driver</property>
                </requireProperty>
                <requireProperty>
                  <property>liquibase.url</property>
                </requireProperty>
                <requireProperty>
                  <property>liquibase.username</property>
                </requireProperty>
                <requireProperty>
                  <property>liquibase.password</property>
                </requireProperty>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- ====================
        /Run configuration check
      ========================-->

      <!-- ====================
        Run against DB
      ========================-->
      <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>

        <!--
          The database connection properties are set in the profiles, settings.xml etc
         -->
        <executions>
          <execution>
            <!-- Run the master changelog against the database  -->
            <id>check-fullinstall-changelog</id>
            <goals>
              <goal>update</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <changeLogFile>${master.changelog}</changeLogFile>
            </configuration>
          </execution>

          <execution>
            <!-- Run the incremental updates against the database -->
            <id>check-incremental-updates</id>
            <goals>
              <goal>update</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <changeLogFile>${incremental.updates.changelog}</changeLogFile>
            </configuration>
          </execution>
        </executions>

      </plugin>
      <!-- ====================
        /Run against DB
      ========================-->

      <!-- ====================
        Generate database documentation
      ========================-->

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>xml-maven-plugin</artifactId>
        <configuration>
          <transformationSets>
            <transformationSet>
              <dir>${project.build.directory}/dbdoc</dir>
              <includes>
                <include>dbdoc.html</include>
              </includes>
              <stylesheet>${project.build.directory}/dbdoc/dbdoc_style.xsl</stylesheet>
            </transformationSet>
          </transformationSets>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon</artifactId>
            <version>${saxon.version}</version>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>transform-dbdoc</id>
            <goals>
              <goal>transform</goal>
            </goals>
            <phase>prepare-package</phase>
          </execution>
        </executions>
      </plugin>

      <!-- ====================
        /Generate database documentation
      ========================-->

      <!-- ====================
        Package
      ========================-->

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <!-- TODO : currently doesn't work, me is disappointed -->
            <id>package-dbdoc</id>
            <goals>
              <goal>add-resource</goal>
            </goals>
            <phase>prepare-package</phase>
            <configuration>
              <resources>
                <resource>
                  <directory>${project.build.directory}/generated-resources/xml/xslt</directory>
                  <targetPath>dbdoc</targetPath>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-remote-resources-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>bundle</goal>
            </goals>
            <configuration>
              <!-- non-standard resource location requires configuration -->
              <resourcesDirectory>src/main/liquibase</resourcesDirectory>
              <!-- included resources *inside* the resource directory -->
              <includes>
                <include>**/*.xml</include>
                <include>**/*.sql</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- ====================
        /Package
      ========================-->

    </plugins>
  </build>

  <profiles>

    <!-- ======================
      Database profiles
    ======================= -->
    <profile>
      <!-- h2 profile, enabled by default -->
      <id>h2</id>
      <activation>
        <activeByDefault>true</activeByDefault>
        <property>
          <name>database</name>
          <value>h2</value>
        </property>
      </activation>

      <properties>
        <liquibase.driver>org.h2.Driver</liquibase.driver>
        <liquibase.password>sa</liquibase.password>
        <liquibase.url>jdbc:h2:${project.build.directory}/database/h2-database;NON_KEYWORDS=ROW,VALUE</liquibase.url>
        <liquibase.username>sa</liquibase.username>
      </properties>

      <dependencies>
        <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>

    </profile>

    <profile>
      <!-- mariadb profile -->
      <id>mariadb</id>
      <activation>
        <property>
          <name>database</name>
          <value>mariadb</value>
        </property>
      </activation>

      <properties>
        <liquibase.driver>org.mariadb.jdbc.Driver</liquibase.driver>
      </properties>

      <dependencies>
        <dependency>
          <groupId>org.mariadb.jdbc</groupId>
          <artifactId>mariadb-java-client</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>

    </profile>

    <profile>
      <!-- postgresql profile -->
      <id>postgresql</id>
      <activation>
        <property>
          <name>database</name>
          <value>postgresql</value>
        </property>
      </activation>

      <properties>
        <liquibase.driver>org.postgresql.Driver</liquibase.driver>
      </properties>

      <dependencies>
        <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </profile>

    <profile>
      <id>skip-changeset-linter</id>
      <properties>
        <skip.linter>true</skip.linter>
      </properties>
    </profile>

    <!-- ======================
      /Database profiles
    ======================= -->

  </profiles>
</project>
