Skip to content

Commit

Permalink
try a service
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Mar 4, 2025
1 parent 18ea5d1 commit a0bb7a5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 78 deletions.
18 changes: 0 additions & 18 deletions .github/actions/setup-postgres-linux/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,5 @@ runs:
steps:
- shell: bash
run: |
sudo apt-get --purge remove postgresql postgresql-*
sudo apt update -y
sudo apt install gnupg2 wget vim -y
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
sudo apt update -y
sudo apt install postgresql-17
sudo apt-get -y install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql
pg_isready
echo "Updating setup_db.sh script ownership and execute bit"
sudo chown postgres ${{ github.action_path }}/../../../test/setup_db.sh
sudo chown postgres ${{ github.action_path }}/setup_db.sh
sudo chmod +x ${{ github.action_path }}/../../../test/setup_db.sh
sudo chmod +x ${{ github.action_path }}/setup_db.sh
ls -la ${{ github.action_path }}/../../../test/setup_db.sh
ls -la ${{ github.action_path }}/setup_db.sh
echo "Running setup_db.sh"
sudo -u postgres bash ${{ github.action_path }}/setup_db.sh
24 changes: 21 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ jobs:
DD_ENV: ci
DD_SERVICE: ${{ github.event.repository.name }}

services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Check out the repository
uses: actions/checkout@v4
Expand All @@ -188,9 +206,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Set up postgres (linux)
if: runner.os == 'Linux'
uses: ./.github/actions/setup-postgres-linux
# - name: Set up postgres (linux)
# if: runner.os == 'Linux'
# uses: ./.github/actions/setup-postgres-linux

- name: Set up postgres (macos)
if: runner.os == 'macOS'
Expand Down
58 changes: 1 addition & 57 deletions test/setup_db.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1 @@
#!/bin/bash
set -x
env | grep '^PG'

# If you want to run this script for your own postgresql (run with
# docker-compose) it will look like this:
# PGHOST=127.0.0.1 PGUSER=root PGPASSWORD=password PGDATABASE=postgres \
PGUSER="${PGUSER:-postgres}"
export PGUSER
PGPORT="${PGPORT:-5432}"
export PGPORT
PGHOST="${PGHOST:-localhost}"

function connect_circle() {
# try to handle circleci/docker oddness
let rc=1
while [[ $rc -eq 1 ]]; do
nc -z ${PGHOST} ${PGPORT}
let rc=$?
done
if [[ $rc -ne 0 ]]; then
echo "Fatal: Could not connect to $PGHOST"
exit 1
fi
}

# appveyor doesn't have 'nc', but it also doesn't have these issues
if [[ -n $CIRCLECI ]]; then
connect_circle
fi

for i in {1..10}; do
if pg_isready -h "${PGHOST}" -p "${PGPORT}" -U "${PGUSER}" ; then
break
fi

echo "Waiting for postgres to be ready..."
sleep 2;
done;

createdb dbt
psql -c "SELECT version();"
psql -c "CREATE ROLE root WITH PASSWORD 'password';"
psql -c "ALTER ROLE root WITH LOGIN;"
psql -c "GRANT CREATE, CONNECT ON DATABASE dbt TO root WITH GRANT OPTION;"

psql -c "CREATE ROLE noaccess WITH PASSWORD 'password' NOSUPERUSER;"
psql -c "ALTER ROLE noaccess WITH LOGIN;"
psql -c "GRANT CONNECT ON DATABASE dbt TO noaccess;"
psql -c "CREATE ROLE dbt_test_user_1;"
psql -c "CREATE ROLE dbt_test_user_2;"
psql -c "CREATE ROLE dbt_test_user_3;"

psql -c 'CREATE DATABASE "dbtMixedCase";'
psql -c 'GRANT CREATE, CONNECT ON DATABASE "dbtMixedCase" TO root WITH GRANT OPTION;'

set +x
../../../test/setup_db.sh

0 comments on commit a0bb7a5

Please sign in to comment.