I am trying to setup a GitHub Action to test my Rust application, but the diesel migrations
keep failing with the error message:
Could not connect to database via
postgresql://app_user@127.0.0.1:5432/app_testdb
: Invalid connection url for multiconnection
Full Error Message Text
Run diesel migration run
diesel migration run
shell: /usr/bin/bash -e {0}
env:
DATABASE_URL: postgresql://app_user@127.0.0.1:5432/app_testdb
CARGO_INCREMENTAL: 0
CARGO_TERM_COLOR: always
Could not connect to database via `postgresql://app_user@127.0.0.1:5432/app_testdb`: Invalid connection url for multiconnection
Error: Process completed with exit code 1.
At this point I have tried adding a LOT of troubleshooting steps, including multiple steps to confirm that that the Postgresql.service
is up and running. Currently I have also added a step to verify connectivity using pg_isready
, which is successful.
What else am I missing here?
Database URL being used:
postgresql://test_user@127.0.0.1:5432/app_testdb
Complete GitHub Action YAML:
name: App-Testing
on:
push:
branches:
- "**/*"
pull_request:
concurrency:
group: "${{ github.ref }}"
cancel-in-progress: true
jobs:
Test-App-Build:
runs-on: ubuntu-latest
steps:
- name: Start PostgreSQL
run: |
sudo systemctl enable postgresql.service
sudo systemctl start postgresql.service
# Create the app database
- name: Create database
run: |
cd ~postgres/
sudo -u postgres psql -c 'CREATE DATABASE app_testdb;'
# Create the app database user
- name: Create database user
run: |
cd ~postgres/
sudo sudo netstat -plunt |grep postgres
sudo -u postgres psql -c "CREATE USER app_user WITH PASSWORD 'changeme';"
sudo -u postgres psql -c "grant all privileges on database app_testdb to app_user;"
# Set up environment variables for database URLs
- name: Set up environment variables
run: echo "DATABASE_URL=postgresql://app_user@127.0.0.1:5432/app_testdb" > "$GITHUB_ENV"
- name: checkout
uses: actions/checkout@v3.5.3
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
# Install diesel CLI if not already installed
- name: Check diesel installation
run: if which diesel; then echo "diesel already installed"; else cargo install diesel_cli --no-default-features --features=postgres; fi
# Run database migrations
- run: pushd app-models
# Test the database connection as app_user
- name: Test app_user database connection
run: pg_isready
- run: diesel migration run
- run: popd
- run: echo "SCRIPT"
- run: cargo build --all --verbose
- run: cargo test --all --verbose
P.S
I am aware that I -could- use a Docker Container, however the GitHub-Runner ubuntu-latest
already comes with PostgreSQL installed, so I see no reason to add complexity by trying to use "external" database service.
Diesel uses itself github actions for CI runs. So it's a good idea to have a look at their setup:
- name: Install postgres (Linux)
if: runner.os == 'Linux' && matrix.backend == 'postgres'
run: |
sudo apt-get update
sudo apt-get install -y libpq-dev postgresql
echo "host all all 127.0.0.1/32 md5" > sudo tee -a /etc/postgresql/10/main/pg_hba.conf
sudo service postgresql restart && sleep 3
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
sudo service postgresql restart && sleep 3
echo "PG_DATABASE_URL=postgres://postgres:postgres@localhost/" >> $GITHUB_ENV
echo "PG_EXAMPLE_DATABASE_URL=postgres://postgres:postgres@localhost/diesel_example" >> $GITHUB_ENV