<?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>ca.ibodrov</groupId>
        <artifactId>mica</artifactId>
        <version>0.0.1</version>
    </parent>

    <artifactId>mica-db</artifactId>

    <properties>
        <db.image>library/postgres:15-alpine</db.image>
        <db.host>localhost</db.host>
        <db.username>postgres</db.username>
        <db.password>mica</db.password>
        <db.changeLogPath>ca/ibodrov/mica/db/liquibase.xml</db.changeLogPath>
        <liquibase.version>4.24.0</liquibase.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.walmartlabs.concord.server</groupId>
            <artifactId>concord-server-db</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>reserve-ports</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>reserve-network-port</goal>
                        </goals>
                        <configuration>
                            <portNames>
                                <portName>db.port</portName>
                            </portNames>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <id>start</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <images>
                        <image>
                            <name>${db.image}</name>
                            <alias>db</alias>
                            <run>
                                <ports>
                                    <port>${db.port}:5432</port>
                                </ports>
                                <env>
                                    <POSTGRES_PASSWORD>${db.password}</POSTGRES_PASSWORD>
                                    <POSTGRES_INITDB_ARGS>--no-sync</POSTGRES_INITDB_ARGS>
                                </env>
                                <wait>
                                    <log>(?s).*ready for start up.*ready to accept connections.*</log>
                                    <time>60000</time>
                                </wait>
                            </run>
                        </image>
                    </images>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>update</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <changeLogFile>src/main/resources/${db.changeLogPath}</changeLogFile>
                    <driver>org.postgresql.Driver</driver>
                    <url>jdbc:postgresql://${db.host}:${db.port}/postgres</url>
                    <username>${db.username}</username>
                    <password>${db.password}</password>
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>org.jooq.no-tips</name>
                                    <value>true</value>
                                </property>
                                <property>
                                    <name>org.jooq.no-logo</name>
                                    <value>true</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jooq</groupId>
                <artifactId>jooq-codegen-maven</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <logging>WARN</logging>
                    <jdbc>
                        <driver>org.postgresql.Driver</driver>
                        <url>jdbc:postgresql://${db.host}:${db.port}/postgres</url>
                        <user>${db.username}</user>
                        <password>${db.password}</password>
                    </jdbc>
                    <generator>
                        <database>
                            <name>org.jooq.meta.postgres.PostgresDatabase</name>
                            <inputSchema>public</inputSchema>
                            <includes>.*</includes>
                        </database>
                        <target>
                            <packageName>ca.ibodrov.mica.db.jooq</packageName>
                            <directory>target/generated-sources/jooq</directory>
                        </target>
                    </generator>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.revelc.code.formatter</groupId>
                <artifactId>formatter-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>check-format</id>
                        <goals>
                            <goal>validate</goal>
                        </goals>
                        <phase>process-sources</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
