Skip to content

Commit

Permalink
build: patch database in build
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Honlet committed Feb 21, 2025
1 parent c555e6e commit 41aa713
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
6 changes: 6 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ services:
database:
build:
context: ./services/database
additional_contexts:
project: .
expose:
- 3306
volumes:
Expand All @@ -57,6 +59,10 @@ services:
- type: bind
source: services/database/initial/
target: /docker-entrypoint-initdb.d/
# TODO: Gitlab - move this into services/databases
- type: bind
source: www/maintenance/versions/
target: /versions/

phpmyadmin:
# https://hub.docker.com/r/phpmyadmin/phpmyadmin/
Expand Down
7 changes: 5 additions & 2 deletions services/database/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mysql:9
FROM mysql:8

ENV MYSQL_ALLOW_EMPTY_PASSWORD=yes
ENV MYSQL_DATABASE=cryptomedic
Expand All @@ -11,6 +11,9 @@ HEALTHCHECK \
CMD mysql --database cryptomedic -e 'SELECT * FROM `settings` WHERE id = "structure_version";'

# For GitLab services
ADD initial/ /docker-entrypoint-initdb.d/
COPY initial/ /docker-entrypoint-initdb.d/

# For Gitlab Only
COPY --from=project www/maintenance/versions /versions/

# https://docs.docker.com/reference/dockerfile/#healthcheck
2 changes: 2 additions & 0 deletions services/database/initial/499 flush.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- https://dev.mysql.com/doc/refman/8.0/en/flush.html#flush-tables
FLUSH TABLES;
25 changes: 25 additions & 0 deletions services/database/initial/500 versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -o pipefail
set -o errexit

version="$(docker_process_sql cryptomedic --raw --silent -e 'SELECT value FROM settings WHERE id = "structure_version"')"
echo "Initial version: $version"

readarray -d '' files < <(printf '%s\0' /versions/* | sort -zV)
for f in "${files[@]}"; do
fn="$(basename "$f")"
if [[ "$fn" =~ ^([0-9]+)" " ]]; then
vf="${BASH_REMATCH[1]}"
if ((vf <= version)); then
echo "Skipping $fn [$vf]"
else
echo "Running $fn [$vf]"
docker_process_sql <"$f"
version="$vf"
fi
else
echo "Running $fn [invalid]"
docker_process_sql <"$f"
fi
done

0 comments on commit 41aa713

Please sign in to comment.