diff --git a/postgres/Dockerfile b/postgres/Dockerfile new file mode 100644 index 00000000..4c3cc347 --- /dev/null +++ b/postgres/Dockerfile @@ -0,0 +1,20 @@ +FROM postgres:12.1 +WORKDIR /app + +ENV PGHOST=localhost +ENV PGDATABASE=kata +ENV PGPASSWORD=admin +ENV PGUSER=postgres +ENV POSTGRES_PASSWORD=admin +ENV PGDATA /var/lib/postgresql/data_local + +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" + +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates wget \ + && rm -rf /var/lib/apt/lists/* + +ADD ./*.sh /app/ +ADD ./*.sql /app/ +RUN chmod +x ./*.sh \ + && ./initializeDocker.sh diff --git a/postgres/docker-compose.yml b/postgres/docker-compose.yml index efcd13fc..60183e85 100644 --- a/postgres/docker-compose.yml +++ b/postgres/docker-compose.yml @@ -2,15 +2,7 @@ version: '3' services: database: - image: postgres:12.1 - environment: - - DB_HOST=localhost - - DB_PASSWORD=admin - - PGHOST=localhost - - PGDATABASE=postgres - - PGPASSWORD=admin - - PGUSER=postgres - - POSTGRES_PASSWORD=admin + build: . ports: - "5432:5432" volumes: diff --git a/postgres/initializeDatabase.sh b/postgres/initializeDatabase.sh new file mode 100644 index 00000000..12b1d33d --- /dev/null +++ b/postgres/initializeDatabase.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -ex + +echo "Create database" +psql -d postgres -c 'DROP DATABASE IF EXISTS kata;' +psql -d postgres -c 'CREATE DATABASE kata;' +psql -d kata -c 'CREATE EXTENSION DBLINK;' + +echo "Initialize test framework" +wget https://raw.githubusercontent.com/adrianandrei-ca/pgunit/bc69dfc526ec3db55ff72af5d78eab55661502af/PGUnit.sql \ + && psql -d kata -f PGUnit.sql \ + && rm PGUnit.sql + +echo "Initialize custom asserts" +psql -d kata -f asserts.sql + +echo "Add current code" +psql -d kata -f item.sql +psql -d kata -f new_item.sql +psql -d kata -f update_quality.sql diff --git a/postgres/initializeDocker.sh b/postgres/initializeDocker.sh new file mode 100644 index 00000000..e962623a --- /dev/null +++ b/postgres/initializeDocker.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +wait_database() +{ + while : + do + (echo > /dev/tcp/127.0.0.1/5432) >/dev/null 2>&1 + result=$? + + if [[ $result -eq 0 ]]; then + break + fi + sleep 1 + done + return $result +} + +nohup docker-entrypoint.sh postgres > /dev/null 2>&1 & +wait_database + +set -ex + +./initializeDatabase.sh + +echo "Stop database" +disown %1