From 2ea917fdaa25c4d463954b27a83c972ae27b0b26 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Wed, 1 Oct 2025 07:57:16 +0530 Subject: [PATCH] fix: resolve migration 0005 duplicate constraint failure (#97) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Problem:** - Users upgrading to v3.7.2 encountered database migration failures - Migration 0005 tried to add unique index without handling existing duplicates - Hybrid initialization (manual SQL + Drizzle) caused schema inconsistencies - Error: "UNIQUE constraint failed: repositories.user_id, repositories.full_name" **Solution:** 1. **Fixed Migration 0005:** - Added deduplication step before creating unique index - Removes duplicate (user_id, full_name) entries, keeping most recent - Safely creates unique constraint after cleanup 2. **Removed Hybrid Database Initialization:** - Eliminated 154 lines of manual SQL from docker-entrypoint.sh - Now uses Drizzle exclusively for schema management - Single source of truth prevents schema drift - Migrations run automatically via src/lib/db/index.ts **Testing:** - ✅ Fresh database initialization works - ✅ Duplicate deduplication verified - ✅ Unique constraint properly enforced - ✅ All 6 migrations apply cleanly **Changes:** - docker-entrypoint.sh: Removed manual table creation SQL - drizzle/0005_polite_preak.sql: Added deduplication before index creation Fixes #97 --- docker-entrypoint.sh | 158 ++-------------------------------- drizzle/0005_polite_preak.sql | 12 ++- 2 files changed, 16 insertions(+), 154 deletions(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 0a649e0..4e25c3e 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -120,161 +120,13 @@ fi # Dependencies are already installed during the Docker build process # Initialize the database if it doesn't exist +# Note: Drizzle migrations will be run automatically when the app starts (see src/lib/db/index.ts) if [ ! -f "/app/data/gitea-mirror.db" ]; then - echo "Initializing database..." - if [ -f "dist/scripts/init-db.js" ]; then - bun dist/scripts/init-db.js - elif [ -f "dist/scripts/manage-db.js" ]; then - bun dist/scripts/manage-db.js init - elif [ -f "scripts/manage-db.ts" ]; then - bun scripts/manage-db.ts init - else - echo "Warning: Could not find database initialization scripts in dist/scripts." - echo "Creating and initializing database manually..." - - # Create the database file - touch /app/data/gitea-mirror.db - - # Initialize the database with required tables - sqlite3 /app/data/gitea-mirror.db < statement-breakpoint +-- Step 2: Now create the unique index safely +CREATE UNIQUE INDEX uniq_repositories_user_full_name ON repositories (user_id, full_name); \ No newline at end of file