From cec8b3cf688064f44c28466eef83d75bd036d753 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 19 Sep 2026 08:49:26 +0200 Subject: [PATCH] fix(linkwarden): convert SQL_ASCII database to UTF8 for und-x-icu migration (#3073) (#3075) * fix(linkwarden): convert SQL_ASCII database to UTF8 for und-x-icu migration (#3073) Linkwarden 2.16.3 adds a migration using the ICU collation "und-x-icu", which does not exist for SQL_ASCII databases. Databases created by older add-on versions under the POSIX locale are SQL_ASCII, so startup fails with Prisma P3018/P3009. New databases are now created explicitly as UTF8. An existing SQL_ASCII database is copied once into a UTF8 database, the failed migration is marked rolled back so Prisma re-applies it, and the original is kept as linkwarden_sql_ascii_backup. Installs already on UTF8 only pay one encoding lookup. Co-Authored-By: Claude Opus 5 * fix(linkwarden): validate UTF-8 and swap databases atomically during conversion Addresses Codex review: pipe the dump through iconv so bytes that are not valid UTF-8 abort the conversion instead of being copied silently, rename both databases in one transaction, and log how to drop the backup. Co-Authored-By: Claude Opus 5 --------- Co-authored-by: Claude Opus 5 --- linkwarden/CHANGELOG.md | 3 +++ linkwarden/config.yaml | 2 +- linkwarden/rootfs/etc/cont-init.d/99-run.sh | 21 +++++++++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/linkwarden/CHANGELOG.md b/linkwarden/CHANGELOG.md index de5e146a8b..7bb5346bd4 100644 --- a/linkwarden/CHANGELOG.md +++ b/linkwarden/CHANGELOG.md @@ -1,4 +1,7 @@ +## 2.16.3.1 (19-09-2026) +- Fix startup failure (Prisma P3018/P3009, collation "und-x-icu" for encoding "SQL_ASCII" does not exist) after the 2.16.3 update (#3073): a local database created as SQL_ASCII by older versions is converted once to UTF8 at startup, the original is kept as `linkwarden_sql_ascii_backup`. New databases are created as UTF8. + ## 2.16.3 (2026-09-12) - Update to latest version from linkwarden/linkwarden (changelog : https://github.com/linkwarden/linkwarden/releases) - Migrate legacy add-on configuration map names to current app configuration terminology. diff --git a/linkwarden/config.yaml b/linkwarden/config.yaml index 8b8f4ef951..049125ea04 100644 --- a/linkwarden/config.yaml +++ b/linkwarden/config.yaml @@ -45,5 +45,5 @@ schema: STORAGE_FOLDER: str? slug: linkwarden url: https://github.com/alexbelgium/hassio-addons/tree/master/linkwarden -version: "2.16.3" +version: "2.16.3.1" webui: "[PROTO:ssl]://[HOST]:[PORT:3000]" diff --git a/linkwarden/rootfs/etc/cont-init.d/99-run.sh b/linkwarden/rootfs/etc/cont-init.d/99-run.sh index ac20ed97ec..531ab6c7a7 100755 --- a/linkwarden/rootfs/etc/cont-init.d/99-run.sh +++ b/linkwarden/rootfs/etc/cont-init.d/99-run.sh @@ -75,10 +75,27 @@ if [[ "$DATABASE_URL" == *"localhost"* ]]; then # Set password sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'homeassistant';" - # Create database if does not exist - echo "CREATE DATABASE linkwarden; GRANT ALL PRIVILEGES ON DATABASE linkwarden to postgres; + # Create database if does not exist (UTF8 is required for the ICU collations used by Linkwarden migrations) + echo "CREATE DATABASE linkwarden TEMPLATE template0 ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C'; GRANT ALL PRIVILEGES ON DATABASE linkwarden to postgres; \q" > setup_postgres.sql sudo -u postgres bash -c 'cat setup_postgres.sql | psql "postgres://postgres:homeassistant@localhost:5432"' || true + + # Databases created by older versions are SQL_ASCII, where the "und-x-icu" collation fails (Prisma P3018/P3009) + # One-time conversion: copy into a new UTF8 database, keep the original as linkwarden_sql_ascii_backup + if [ "$(sudo -u postgres psql -tAc "SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname='linkwarden'")" = "SQL_ASCII" ]; then + bashio::log.warning "Database linkwarden uses SQL_ASCII encoding, converting it to UTF8" + sudo -u postgres psql -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS linkwarden_utf8;" \ + -c "CREATE DATABASE linkwarden_utf8 TEMPLATE template0 ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C';" + if sudo -u postgres bash -o pipefail -c 'pg_dump -d linkwarden | iconv -f UTF-8 -t UTF-8 | psql -q -v ON_ERROR_STOP=1 -d linkwarden_utf8 >/dev/null'; then + # Let Prisma re-apply the migration that failed on SQL_ASCII + sudo -u postgres psql -v ON_ERROR_STOP=1 -d linkwarden_utf8 -c "UPDATE \"_prisma_migrations\" SET rolled_back_at=NOW() WHERE migration_name='20260818000000_case_insensitive_name_sorting' AND finished_at IS NULL AND rolled_back_at IS NULL;" + sudo -u postgres psql -v ON_ERROR_STOP=1 -c "ALTER DATABASE linkwarden RENAME TO linkwarden_sql_ascii_backup; ALTER DATABASE linkwarden_utf8 RENAME TO linkwarden;" + bashio::log.info "Database converted to UTF8, original kept as linkwarden_sql_ascii_backup (once everything works, free the space with: sudo -u postgres psql -c 'DROP DATABASE linkwarden_sql_ascii_backup;')" + else + sudo -u postgres psql -c "DROP DATABASE IF EXISTS linkwarden_utf8;" + bashio::log.error "Conversion to UTF8 failed, the original database was left untouched" + fi + fi fi ########################