<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2015-2017 Hewlett Packard Enterprise Development LP.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<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>
        <artifactId>corepolicy-aggregator</artifactId>
        <groupId>com.github.cafdataprocessing</groupId>
        <version>1.0.0-4</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
   
    <artifactId>corepolicy-databasescriptgeneration</artifactId>
    <name>Core Policy Database Script Generation</name>
    <packaging>jar</packaging>
   
    <properties>       
        <!-- used if we want to use updateSQL / update calls directly to the liquibase mvn plugin. -->
        <hibernate.connection.url>jdbc:postgresql://${docker.host.address}:${postgres.db.port}/corepolicy</hibernate.connection.url>
        <hibernate.username>postgres</hibernate.username>
        <hibernate.password>root</hibernate.password>
        
        <postgres-db-container.url>index.docker.io/postgres:9.4</postgres-db-container.url>

        <org.liquibase.version>3.3.2</org.liquibase.version>
    </properties>
    
    <dependencies>
         <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>${org.liquibase.version}</version>
        </dependency>

        <!--Db drivers-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.187</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${org.liquibase.version}</version>
                <configuration>
                    <url>${hibernate.connection.url}</url>
                    <username>${hibernate.username}</username>
                    <password>${hibernate.password}</password>
                    <changeLogFile>${basedir}/../Database/src/main/resources/changelog1.xml</changeLogFile>
                    <promptForNonLocalDatabase>false</promptForNonLocalDatabase>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    <profiles>
        <profile>
            <id>create-db-script</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jolokia</groupId>
                        <artifactId>docker-maven-plugin</artifactId>
                        <version>${docker.maven.version}</version>
                        <configuration>
                            <skip>true</skip>
                            <images>    
                                <!-- pull in a normal postgres DB, we will mount a volume containing our scripts as a linked volume -->
                                <image>
                                    <alias>integrationtests-corepolicydb-postgres</alias>
                                    <name>${postgres-db-container.url}</name>
                                    <run>
                                        <ports>
                                            <port>${postgres.db.port}:5432</port>
                                        </ports>
                                        <env> 
                                            <POSTGRES_PASSWORD>root</POSTGRES_PASSWORD>
                                            <POSTGRES_USER>postgres</POSTGRES_USER>
                                        </env>
                                        <wait>
                                            <log>PostgreSQL init process complete</log>
                                            <time>20000</time>
                                            <shutdown>500</shutdown>
                                        </wait>
                                        <log>
                                            <enabled>true</enabled>
                                        </log>
                                        <volumes>
                                            <from>
                                              <image>integrationtests-dbsetup</image>
                                            </from>
                                        </volumes>
                                    </run>
                                </image>
                                <image>
                                    <alias>integrationtests-dbsetup</alias>
                                    <name>policy/integrationtests/dbsetup</name>
                                    <build>
                                        <tags>
                                            <tag>built-as-part-of-${project.parent.artifactId}-latest</tag>
                                            <tag>temporary-item-please-remove</tag>
                                        </tags>
                                        <assembly>
                                            <basedir>/docker-entrypoint-initdb.d</basedir>
                                            <inline>
                                                <fileSets>
                                                    <fileSet>
                                                        <directory>test-dbscripts</directory>
                                                        <outputDirectory>/</outputDirectory>
                                                        <includes>
                                                            <include>*</include>
                                                        </includes>
                                                    </fileSet>
                                                </fileSets>
                                            </inline>
                                        </assembly>
                                    </build>
                                </image>
                            </images>
                        </configuration>
                        <executions>
                            <execution>
                                <id>deploy is always to be skipped!</id>
                                <phase>deploy</phase>
                                <goals>
                                    <goal>push</goal>
                                </goals>
                                <configuration>
                                    <skip>true</skip>
                                </configuration>
                            </execution>
                            <execution>
                                <id>build container package - postgres db only!</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                </configuration>
                            </execution>
                            <execution>
                                <id>start container package - postgres db only!</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop container package - postgres db only!</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                                <configuration>
                                    <skip>false</skip>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <version>${org.liquibase.version}</version>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                        <executions>
                            <execution>
                                <id>Update DB Machine - FS</id>
                                <phase>integration-test</phase>                                 
                                <goals>
                                    <goal>updateSQL</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
       
       <!-- optional profile if you just want to generate the SQL from a hibernate DB, which isn't the postgres container in the other profile -->
        <profile>
            <!-- Optional profile which can create a migrate.sql file based on our changelog information -->
            <!-- This is not part of the final application, but is best positioned beside the rest of the liquibase 
            db code. -->
            <id>generate-sql</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.liquibase</groupId>
                        <artifactId>liquibase-maven-plugin</artifactId>
                        <version>${org.liquibase.version}</version>
                        <executions>
                            <execution>
                                <id>Generate Output SQL</id>
                                <phase>prepare-package</phase>                                 
                                <goals>
                                    <!-- allow us to generate updateSQL script optionally -->
                                    <goal>updateSQL</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>              
        </profile>
    </profiles>
</project>
