feat: Switch to postgres

Migrate from MariaDB to PostgreSQL.

Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-11-30 15:13:42 +01:00
parent eca8bbf515
commit 122df968dc
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
64 changed files with 1467 additions and 1441 deletions

View file

@ -47,7 +47,7 @@ Luckily such data can be generated.
docker compose exec fastapi /bin/bash
pacman -S words fortune-mod
./schema/gendummydata.py dummy.sql
mariadb aurweb < dummy.sql
su postgres -q -c 'psql aurweb < dummy.sql'
```
The generation script may prompt you to install other Arch packages before it

View file

@ -71,7 +71,7 @@ start_step_ca() {
kill_step_ca() {
# Stop the step-ca web server.
killall step-ca >/dev/null 2>&1 || /bin/true
killall -w step-ca >/dev/null 2>&1 || /bin/true
}
install_step_ca() {
@ -105,8 +105,6 @@ if [ ! -d /root/.step/config ]; then
echo -n "WARN: Your certificates are being regenerated to resolve "
echo -n "an inconsistent step-ca state. You will need to re-import "
echo "the root CA certificate into your browser."
else
exec "$@"
fi
# Set permissions to /data to rwx for everybody.

View file

@ -2,7 +2,7 @@
set -eou pipefail
# Setup the DB.
NO_INITDB=1 /docker/mariadb-init-entrypoint.sh
/docker/postgres-init-entrypoint.sh
# Create aurblup's directory.
AURBLUP_DIR="/aurweb/aurblup/"

View file

@ -2,7 +2,7 @@
set -eou pipefail
# Setup database.
NO_INITDB=1 /docker/mariadb-init-entrypoint.sh
/docker/postgres-init-entrypoint.sh
# Setup some other options.
aurweb-config set options cache 'redis'

View file

@ -39,7 +39,7 @@ Match User aur
EOF
# Setup database.
NO_INITDB=1 /docker/mariadb-init-entrypoint.sh
/docker/postgres-init-entrypoint.sh
# Setup some other options.
aurweb-config set serve repo-path '/aurweb/aur.git/'

View file

@ -1,2 +0,0 @@
#!/bin/bash
exec mariadb-admin ping --silent

2
docker/health/postgres.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/bash
exec su postgres -c 'pg_isready'

View file

@ -1,31 +0,0 @@
#!/bin/bash
set -eou pipefail
MYSQL_DATA=/var/lib/mysql
mariadb-install-db --user=mysql --basedir=/usr --datadir=$MYSQL_DATA
# Start it up.
mariadbd-safe --datadir=$MYSQL_DATA --skip-networking &
while ! mariadb-admin ping 2>/dev/null; do
sleep 1s
done
# Configure databases.
DATABASE="aurweb" # Persistent database for fastapi.
echo "Taking care of primary database '${DATABASE}'..."
mariadb -u root -e "CREATE USER IF NOT EXISTS 'aur'@'localhost' IDENTIFIED BY 'aur';"
mariadb -u root -e "CREATE USER IF NOT EXISTS 'aur'@'%' IDENTIFIED BY 'aur';"
mariadb -u root -e "CREATE DATABASE IF NOT EXISTS $DATABASE;"
mariadb -u root -e "CREATE USER IF NOT EXISTS 'aur'@'%' IDENTIFIED BY 'aur';"
mariadb -u root -e "GRANT ALL ON aurweb.* TO 'aur'@'localhost';"
mariadb -u root -e "GRANT ALL ON aurweb.* TO 'aur'@'%';"
mariadb -u root -e "CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY 'aur';"
mariadb -u root -e "GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;"
mariadb-admin -uroot shutdown
exec "$@"

View file

@ -1,17 +0,0 @@
#!/bin/bash
set -eou pipefail
# Setup a config for our mysql db.
aurweb-config set database name 'aurweb'
aurweb-config set database user 'aur'
aurweb-config set database password 'aur'
aurweb-config set database host 'localhost'
aurweb-config set database socket '/var/run/mysqld/mysqld.sock'
aurweb-config unset database port
if [ ! -z ${NO_INITDB+x} ]; then
exec "$@"
fi
python -m aurweb.initdb 2>/dev/null || /bin/true
exec "$@"

34
docker/postgres-entrypoint.sh Executable file
View file

@ -0,0 +1,34 @@
#!/bin/bash
set -eou pipefail
PGDATA=/var/lib/postgres/data
DATABASE="aurweb"
# Initialize and setup postgres
if [ ! -f "$PGDATA/../init" ]; then
echo "Preparing postgres instance..."
touch $PGDATA/../init
# Init db directory
su postgres -c "pg_ctl initdb -D $PGDATA"
su postgres -c "echo \"listen_addresses='*'\" >> $PGDATA/postgresql.conf"
su postgres -c "echo \"host all all 0.0.0.0/0 scram-sha-256\" >> $PGDATA/pg_hba.conf"
install -d -o postgres -g postgres /run/postgresql
# Start postgres
su postgres -c "pg_ctl start -D $PGDATA"
# Configure database & user
echo "Taking care of primary database '$DATABASE'..."
su postgres -c "psql -c \"create database $DATABASE;\""
su postgres -c "psql -c \"create role aur superuser login password 'aur';\"";
# Provision database
python -m aurweb.initdb 2>/dev/null || /bin/true
# Stop postgres
su postgres -c "pg_ctl stop -D $PGDATA"
fi
exec "$@"

View file

@ -0,0 +1,12 @@
#!/bin/bash
set -eou pipefail
# Setup a config for our postgres db via socket connection.
aurweb-config set database name 'aurweb'
aurweb-config set database user 'aur'
aurweb-config set database socket '/run/postgresql'
aurweb-config unset database host
aurweb-config unset database port
aurweb-config unset database password
exec "$@"

View file

@ -13,8 +13,8 @@ pacman -Sy --noconfirm --noprogressbar archlinux-keyring
# Install other OS dependencies.
pacman -Syu --noconfirm --noprogressbar \
--cachedir .pkg-cache git gpgme nginx redis openssh \
mariadb mariadb-libs cgit-aurweb uwsgi uwsgi-plugin-cgi \
git gpgme nginx redis openssh \
postgresql cgit-aurweb uwsgi uwsgi-plugin-cgi \
python-pip pyalpm python-srcinfo curl libeatmydata cronie \
python-poetry python-poetry-core step-cli step-ca asciidoc \
python-virtualenv python-pre-commit

View file

@ -8,7 +8,7 @@ make -C test clean
# Run sharness tests.
bash $dir/run-sharness.sh
# Run Python tests with MariaDB database.
# Run Python tests with PostgreSQL database.
# Pass --silence to avoid reporting coverage. We will do that below.
bash $dir/run-pytests.sh --no-coverage

View file

@ -1,19 +0,0 @@
#!/bin/bash
set -eou pipefail
# We use the root user for testing in Docker.
# The test user must be able to create databases and drop them.
aurweb-config set database user 'root'
aurweb-config set database host 'localhost'
aurweb-config set database socket '/var/run/mysqld/mysqld.sock'
# Remove possibly problematic configuration options.
# We depend on the database socket within Docker and
# being run as the root user.
aurweb-config unset database password
aurweb-config unset database port
# Setup notifications for testing.
aurweb-config set notifications sendmail "$(pwd)/util/sendmail"
exec "$@"

View file

@ -0,0 +1,15 @@
#!/bin/bash
set -eou pipefail
# Setup a config for our postgres db via socket connection.
aurweb-config set database name 'aurweb'
aurweb-config set database user 'aur'
aurweb-config set database socket '/run/postgresql'
aurweb-config unset database host
aurweb-config unset database port
aurweb-config unset database password
# Setup notifications for testing.
aurweb-config set notifications sendmail "$(pwd)/util/sendmail"
exec "$@"

View file

@ -2,6 +2,6 @@
set -eou pipefail
dir="$(dirname $0)"
bash $dir/test-mysql-entrypoint.sh
bash $dir/test-postgres-entrypoint.sh
exec "$@"