From 0239d01d3b45eca1dd137e4f0b4f287db75d15fa Mon Sep 17 00:00:00 2001 From: Daniel Volz Date: Sat, 20 Dec 2025 21:01:18 +0100 Subject: [PATCH] feat: add migration for image_url column in medications table with error handling --- .github/copilot-instructions.md | 19 +++++++++++++++++++ backend/src/db/migrate.ts | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index d239e48..26ba2e1 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -62,6 +62,25 @@ Notifications: data/notification-settings.json (editable via UI) - **API responses**: Return objects directly, Fastify serializes to JSON - **Environment**: Copy `.env.example` → `.env`, secrets must be 10+ chars +## ⚠️ Database Migrations (CRITICAL) + +**When adding/modifying database columns, ALWAYS:** + +1. **Update schema**: `backend/src/db/schema.ts` +2. **Create migration file**: `backend/src/db/migrations/XXXX_description.sql` + ```sql + -- Example: Adding a new column + ALTER TABLE medications ADD COLUMN new_column TEXT; + ``` +3. **Update journal**: `backend/src/db/migrations/meta/_journal.json` + ```json + { "idx": X, "version": 1, "when": TIMESTAMP, "tag": "XXXX_description", "breakpoint": false } + ``` + +**Why this matters**: The dev database might get updated manually, but production will break without proper migration files. This causes `SQLITE_ERROR: no such column` errors in prod. + +**Migration naming**: `0001_add_strips.sql`, `0002_pack_inventory.sql`, `0003_add_image_url.sql` + ## File Locations | Purpose | Location | diff --git a/backend/src/db/migrate.ts b/backend/src/db/migrate.ts index e2c502f..5485092 100644 --- a/backend/src/db/migrate.ts +++ b/backend/src/db/migrate.ts @@ -73,6 +73,21 @@ async function main() { console.log("Executing:", stmt.trim().substring(0, 50) + "..."); await client.execute(stmt); } + + // Run migrations for existing databases + console.log("Running migrations for existing databases..."); + + // Migration: Add image_url column if it doesn't exist + try { + await client.execute("ALTER TABLE medications ADD COLUMN image_url TEXT"); + console.log("Added image_url column"); + } catch (e: any) { + if (e.message?.includes("duplicate column") || e.message?.includes("already exists")) { + console.log("image_url column already exists, skipping"); + } else { + throw e; + } + } console.log("Database setup complete!"); process.exit(0);