Skip to content

Commit 099b62e

Browse files
committed
ci: test postgres
1 parent f77fd00 commit 099b62e

File tree

2 files changed

+152
-6
lines changed

2 files changed

+152
-6
lines changed

.github/workflows/rust.yml

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,100 @@
1-
name: Rust
1+
name: CI
22

33
on:
44
push:
55
branches: [ "master" ]
6+
67
pull_request:
7-
branches: [ "master" ]
8+
types: [ opened, synchronize, reopened ]
9+
branches:
10+
- master
811

912
env:
1013
CARGO_TERM_COLOR: auto
14+
SQLX_VERSION: 0.8.2
15+
SQLX_FEATURES: "rustls,postgres"
1116

1217
jobs:
13-
build:
18+
build-test:
19+
name: Build + Test
1420
runs-on: ubuntu-latest
21+
services:
22+
postgres:
23+
image: postgres:14
24+
env:
25+
POSTGRES_USER: postgres
26+
POSTGRES_PASSWORD: password
27+
POSTGRES_DB: postgres
28+
ports:
29+
- 5432:5432
30+
# redis:
31+
# image: redis:7
32+
# ports:
33+
# - 6379:6379
1534
steps:
16-
- uses: actions/checkout@v4
17-
- name: Check
18-
run: cargo check
35+
- uses: actions/checkout@v4
36+
- uses: dtolnay/rust-toolchain@stable
37+
- uses: Swatinem/rust-cache@v2
38+
with:
39+
key: sqlx-${{ env.SQLX_VERSION }}
40+
- name: Install sqlx-cli
41+
run:
42+
cargo install sqlx-cli
43+
--version=${{ env.SQLX_VERSION }}
44+
--no-default-features
45+
--features ${{ env.SQLX_FEATURES }}
46+
--locked
47+
- name: Migrate database
48+
run: |
49+
sudo apt-get install libpq-dev -y
50+
./ci/init_db.sh
51+
# - name: Check sqlx-data.json is up-to-date
52+
# run: |
53+
# cargo sqlx prepare --workspace --check
54+
- name: Test
55+
run: cargo test
56+
57+
# fmt:
58+
# name: Rustfmt
59+
# runs-on: ubuntu-latest
60+
# steps:
61+
# - uses: actions/checkout@v3
62+
# - uses: dtolnay/rust-toolchain@stable
63+
# with:
64+
# components: rustfmt
65+
# - name: Check fmt
66+
# run: cargo fmt --check
67+
68+
# clippy:
69+
# name: Clippy
70+
# runs-on: ubuntu-latest
71+
# services:
72+
# postgres:
73+
# image: postgres:14
74+
# env:
75+
# POSTGRES_USER: postgres
76+
# POSTGRES_PASSWORD: password
77+
# POSTGRES_DB: postgres
78+
# ports:
79+
# - 5432:5432
80+
# steps:
81+
# - uses: actions/checkout@v3
82+
# - uses: dtolnay/rust-toolchain@stable
83+
# with:
84+
# components: clippy
85+
# - uses: Swatinem/rust-cache@v2
86+
# with:
87+
# key: sqlx-${{ env.SQLX_VERSION }}
88+
# - name: Install sqlx-cli
89+
# run:
90+
# cargo install sqlx-cli
91+
# --version=${{ env.SQLX_VERSION }}
92+
# --features ${{ env.SQLX_FEATURES }}
93+
# --no-default-features
94+
# --locked
95+
# - name: Migrate database
96+
# run: |
97+
# sudo apt-get install libpq-dev -y
98+
# SKIP_DOCKER=true ./scripts/init_db.sh
99+
# - name: Linting
100+
# run: cargo clippy -- -D warnings

ci/init_db.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
set -eo pipefail
4+
5+
if ! [ -x "$(command -v psql)" ]; then
6+
echo >&2 "Error: psql is not installed."
7+
exit 1
8+
fi
9+
10+
if ! [ -x "$(command -v sqlx)" ]; then
11+
echo >&2 "Error: sqlx is not installed."
12+
echo >&2 "Use:"
13+
echo >&2 " cargo install --version='~0.8' sqlx-cli --no-default-features --features rustls,postgres"
14+
echo >&2 "to install it."
15+
exit 1
16+
fi
17+
18+
# Check if a custom user has been set, otherwise default to 'postgres'
19+
DB_USER="${POSTGRES_USER:=postgres}"
20+
# Check if a custom password has been set, otherwise default to 'password'
21+
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
22+
# Check if a custom database name has been set, otherwise default to 'bdk_wallet'
23+
DB_NAME="${POSTGRES_DB:=bdk_wallet}"
24+
# Check if a custom port has been set, otherwise default to '5432'
25+
DB_PORT="${POSTGRES_PORT:=5432}"
26+
# Check if a custom host has been set, otherwise default to 'localhost'
27+
DB_HOST="${POSTGRES_HOST:=localhost}"
28+
29+
# Allow to skip Docker if a dockerized Postgres database is already running
30+
# if [[ -z "${SKIP_DOCKER}" ]]
31+
# then
32+
# # if a postgres container is running, print instructions to kill it and exit
33+
# RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
34+
# if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
35+
# echo >&2 "there is a postgres container already running, kill it with"
36+
# echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
37+
# exit 1
38+
# fi
39+
# # Launch postgres using Docker
40+
# docker run \
41+
# -e POSTGRES_USER=${DB_USER} \
42+
# -e POSTGRES_PASSWORD=${DB_PASSWORD} \
43+
# -e POSTGRES_DB=${DB_NAME} \
44+
# -p "${DB_PORT}":5432 \
45+
# -d \
46+
# --name "postgres_$(date '+%s')" \
47+
# postgres -N 1000
48+
# # ^ Increased maximum number of connections for testing purposes
49+
# fi
50+
51+
# Keep pinging Postgres until it's ready to accept commands
52+
until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
53+
>&2 echo "Postgres is still unavailable - sleeping"
54+
sleep 1
55+
done
56+
57+
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations."
58+
59+
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
60+
export DATABASE_TEST_URL=$DATABASE_URL
61+
sqlx database create
62+
sqlx migrate run --source ./db/migrations
63+
64+
>&2 echo "Completed migration."

0 commit comments

Comments
 (0)