From c763ed55bc98adc1b6bdf86ce81076b07b810c47 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Mon, 20 Apr 2026 08:35:23 +0200 Subject: [PATCH] Pangolin: pre-apply schema migrations to prevent data loss Fixes #13857 drizzle-kit push recreates tables when adding NOT NULL columns without defaults, deleting all rows. Pre-apply settingsLogRetentionDaysConnection and isJitMode columns with defaults via sqlite3 before running drizzle. Migrate roleId data from userOrgs to userOrgRoles before the column is dropped. Add --force flag since destructive ops are now safe. --- ct/pangolin.sh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ct/pangolin.sh b/ct/pangolin.sh index b30455068..68be7ca19 100644 --- a/ct/pangolin.sh +++ b/ct/pangolin.sh @@ -69,7 +69,18 @@ function update_script() { msg_info "Running database migrations" cd /opt/pangolin - ENVIRONMENT=prod $STD npx drizzle-kit push --config drizzle.sqlite.config.ts + + # Pre-apply potentially destructive schema changes safely so drizzle-kit + # does not recreate tables (which would delete all rows). + local DB="/opt/pangolin/config/db/db.sqlite" + if [[ -f "$DB" ]]; then + sqlite3 "$DB" "ALTER TABLE 'orgs' ADD COLUMN 'settingsLogRetentionDaysConnection' integer DEFAULT 0 NOT NULL;" 2>/dev/null || true + sqlite3 "$DB" "ALTER TABLE 'clientSitesAssociationsCache' ADD COLUMN 'isJitMode' integer DEFAULT 0 NOT NULL;" 2>/dev/null || true + # Migrate roleId from userOrgs → userOrgRoles before the column is dropped + sqlite3 "$DB" "INSERT OR IGNORE INTO 'userOrgRoles' (userId, orgId, roleId) SELECT userId, orgId, roleId FROM 'userOrgs' WHERE roleId IS NOT NULL;" 2>/dev/null || true + fi + + ENVIRONMENT=prod $STD npx drizzle-kit push --force --config drizzle.sqlite.config.ts msg_ok "Ran database migrations" msg_info "Updating Badger plugin version"