From be6dd261edbe4ad698f67f012d6067a9ccc5a3b2 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Fri, 19 Dec 2025 13:57:29 +0100 Subject: [PATCH] Fix Joplin migration locks on startup --- joplin/CHANGELOG.md | 4 +++ joplin/Dockerfile | 2 +- joplin/config.yaml | 2 +- joplin/rootfs/etc/cont-init.d/99-run.sh | 48 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/joplin/CHANGELOG.md b/joplin/CHANGELOG.md index 439941037..742e5b719 100644 --- a/joplin/CHANGELOG.md +++ b/joplin/CHANGELOG.md @@ -1,4 +1,8 @@ +## 3.5.2 (22-12-2025) +- Automatically clear stale migration locks for SQLite and PostgreSQL databases on startup to avoid blocked fresh installs +- Add database client utilities for unlocking migrations + ## 3.5.1 (06-12-2025) - Update to latest version from etechonomy/joplin-server (changelog : https://github.com/etechonomy/joplin-server/releases) - Added support for configuring extra environment variables via the `env_vars` add-on option alongside config.yaml. See https://github.com/alexbelgium/hassio-addons/wiki/Add-Environment-variables-to-your-Addon-2 for details. diff --git a/joplin/Dockerfile b/joplin/Dockerfile index 024293d6d..82ee1e9b6 100644 --- a/joplin/Dockerfile +++ b/joplin/Dockerfile @@ -55,7 +55,7 @@ ADD "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templat RUN chmod 744 /ha_automodules.sh && /ha_automodules.sh "$MODULES" && rm /ha_automodules.sh # Manual apps -ENV PACKAGES="procps" +ENV PACKAGES="procps sqlite3 postgresql-client" # Automatic apps & bashio ADD "https://raw.githubusercontent.com/alexbelgium/hassio-addons/master/.templates/ha_autoapps.sh" "/ha_autoapps.sh" diff --git a/joplin/config.yaml b/joplin/config.yaml index 78c819c0d..6d8abfb71 100644 --- a/joplin/config.yaml +++ b/joplin/config.yaml @@ -104,5 +104,5 @@ schema: slug: joplin udev: true url: https://github.com/alexbelgium/hassio-addons -version: "3.5.1" +version: "3.5.2" webui: "[PROTO:ssl]://[HOST]:[PORT:22300]" diff --git a/joplin/rootfs/etc/cont-init.d/99-run.sh b/joplin/rootfs/etc/cont-init.d/99-run.sh index 4e61bce67..78be84b82 100755 --- a/joplin/rootfs/etc/cont-init.d/99-run.sh +++ b/joplin/rootfs/etc/cont-init.d/99-run.sh @@ -5,6 +5,52 @@ set -e bashio::log.warning "Warning - minimum configuration recommended : 2 cpu cores and 4 GB of memory. Otherwise the system will become unresponsive and crash." +unlock_sqlite_migrations() { + local db_path="$1" + + if ! command -v sqlite3 >/dev/null 2>&1; then + bashio::log.warning "sqlite3 not available; skipping SQLite migration lock check." + return + fi + + if ! sqlite3 "$db_path" "SELECT name FROM sqlite_master WHERE type='table' AND name='knex_migrations_lock';" | grep -q "knex_migrations_lock"; then + return + fi + + local is_locked + is_locked=$(sqlite3 "$db_path" "SELECT is_locked FROM knex_migrations_lock LIMIT 1;" 2>/dev/null || true) + + if [[ "$is_locked" == "1" ]]; then + bashio::log.warning "Locked SQLite migration table detected, attempting to unlock." + sqlite3 "$db_path" "UPDATE knex_migrations_lock SET is_locked = 0;" || bashio::log.warning "Failed to clear SQLite migration lock." + fi +} + +unlock_postgres_migrations() { + if ! command -v psql >/dev/null 2>&1; then + bashio::log.warning "psql not available; skipping PostgreSQL migration lock check." + return + fi + + if [[ -z "${POSTGRES_DATABASE:-}" || -z "${POSTGRES_USER:-}" || -z "${POSTGRES_HOST:-}" ]]; then + bashio::log.warning "PostgreSQL configuration incomplete; skipping migration lock check." + return + fi + + local pg_port="${POSTGRES_PORT:-5432}" + export PGPASSWORD="${POSTGRES_PASSWORD:-}" + + local is_locked + is_locked=$(psql -h "$POSTGRES_HOST" -p "$pg_port" -U "$POSTGRES_USER" -d "$POSTGRES_DATABASE" -Atqc "SELECT is_locked FROM knex_migrations_lock LIMIT 1;" 2>/dev/null || true) + + if [[ "$is_locked" == "1" ]]; then + bashio::log.warning "Locked PostgreSQL migration table detected, attempting to unlock." + psql -h "$POSTGRES_HOST" -p "$pg_port" -U "$POSTGRES_USER" -d "$POSTGRES_DATABASE" -Atqc "UPDATE knex_migrations_lock SET is_locked = 0;" || bashio::log.warning "Failed to clear PostgreSQL migration lock." + fi + + unset PGPASSWORD +} + # Check data location LOCATION=$(bashio::config 'data_location') if [[ "$LOCATION" = "null" || -z "$LOCATION" ]]; then @@ -41,9 +87,11 @@ if bashio::config.has_value 'POSTGRES_DATABASE'; then bashio::config.has_value 'POSTGRES_USER' && export POSTGRES_USER=$(bashio::config 'POSTGRES_USER') && bashio::log.info 'Postgrep User set' bashio::config.has_value 'POSTGRES_PORT' && export POSTGRES_PORT=$(bashio::config 'POSTGRES_PORT') && bashio::log.info 'Postgrep Port set' bashio::config.has_value 'POSTGRES_HOST' && export POSTGRES_HOST=$(bashio::config 'POSTGRES_HOST') && bashio::log.info 'Postgrep Host set' + unlock_postgres_migrations else bashio::log.info "Using sqlite" + unlock_sqlite_migrations "$SQLITE_DATABASE" fi