From bb616c6db1b75e7b006cf5ce83f967531c246b09 Mon Sep 17 00:00:00 2001 From: Ashwin Kumar Sivakumar Date: Sun, 19 Jul 2026 04:40:31 +0530 Subject: [PATCH] fix(db): make three more migrations idempotent; log swallowed login DB error Same class of bug as the ai_plans_and_limits fix: ai_credit_packages and users_litellm_key used plain CREATE TABLE/ADD COLUMN with no re-run guard, and payu_rename_columns did a bare RENAME COLUMN that fails outright on any second run ("column razorpay_order_id does not exist"). All three were discovered by actually running the db-migrate job end to end for the first time and fixed in the same pass as the verification_logs FK fix - already baked into the db-migrate image that was built and run manually, this commit just brings the source in the repo in sync with what's deployed. Also: apps/employees login handler's DB error was being discarded via .map_err(|_| ...) with zero logging, making the reported "db error while logging in" impossible to diagnose from pod logs. Log the real error. Co-Authored-By: Claude Sonnet 5 --- apps/employees/src/handlers/auth.rs | 5 ++++- .../20260615060600_ai_credit_packages.up.sql | 20 ++++++++++++------- .../20260626000000_payu_rename_columns.up.sql | 13 ++++++++++-- .../20260716500000_users_litellm_key.up.sql | 2 +- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/apps/employees/src/handlers/auth.rs b/apps/employees/src/handlers/auth.rs index 8b736ed..1a4deb6 100644 --- a/apps/employees/src/handlers/auth.rs +++ b/apps/employees/src/handlers/auth.rs @@ -70,7 +70,10 @@ async fn login( let employee = EmployeeRepository::get_by_email(&state.pool, &email) .await - .map_err(|_| err(StatusCode::INTERNAL_SERVER_ERROR, "DB error", "DB_ERROR"))? + .map_err(|e| { + tracing::error!(error = %e, email = %email, "employee login: get_by_email failed"); + err(StatusCode::INTERNAL_SERVER_ERROR, "DB error", "DB_ERROR") + })? .ok_or_else(|| err(StatusCode::UNAUTHORIZED, "Invalid credentials", "INVALID_CREDENTIALS"))?; if employee.status != "ACTIVE" { diff --git a/crates/db/migrations/20260615060600_ai_credit_packages.up.sql b/crates/db/migrations/20260615060600_ai_credit_packages.up.sql index 56f2e28..f4dee4e 100644 --- a/crates/db/migrations/20260615060600_ai_credit_packages.up.sql +++ b/crates/db/migrations/20260615060600_ai_credit_packages.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE ai_credit_packages ( +CREATE TABLE IF NOT EXISTS ai_credit_packages ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name VARCHAR(150) NOT NULL, description TEXT, @@ -9,10 +9,16 @@ CREATE TABLE ai_credit_packages ( updated_at TIMESTAMP NOT NULL DEFAULT NOW() ); -CREATE INDEX idx_ai_credit_packages_active ON ai_credit_packages(is_active, price_inr); +CREATE INDEX IF NOT EXISTS idx_ai_credit_packages_active ON ai_credit_packages(is_active, price_inr); -INSERT INTO ai_credit_packages (name, description, credits, price_inr) VALUES -('Starter AI Credits', '50 AI credits for casual usage', 50, 99), -('Pro AI Credits', '200 AI credits for power users', 200, 349), -('Business AI Credits', '750 AI credits for teams', 750, 999), -('Enterprise AI Credits', '2500 AI credits for heavy usage', 2500, 2499); +-- name has no unique constraint to key an ON CONFLICT off of, so guard the +-- whole seed on the table being empty (this migration only ever ran once +-- successfully; a second run inserted zero extra rows before this fix). +INSERT INTO ai_credit_packages (name, description, credits, price_inr) +SELECT * FROM (VALUES + ('Starter AI Credits', '50 AI credits for casual usage', 50, 99), + ('Pro AI Credits', '200 AI credits for power users', 200, 349), + ('Business AI Credits', '750 AI credits for teams', 750, 999), + ('Enterprise AI Credits', '2500 AI credits for heavy usage', 2500, 2499) +) AS v(name, description, credits, price_inr) +WHERE NOT EXISTS (SELECT 1 FROM ai_credit_packages); diff --git a/crates/db/migrations/20260626000000_payu_rename_columns.up.sql b/crates/db/migrations/20260626000000_payu_rename_columns.up.sql index 27d4ea6..f84c52f 100644 --- a/crates/db/migrations/20260626000000_payu_rename_columns.up.sql +++ b/crates/db/migrations/20260626000000_payu_rename_columns.up.sql @@ -1,5 +1,14 @@ -- Rename Razorpay columns to PayU equivalents on the payments table. -- PayU uses `txnid` (transaction id) and `mihpayid` (PayU payment id) instead of Razorpay's order_id / payment_id. +-- Guarded so re-running after the first successful rename is a no-op instead +-- of failing with "column razorpay_order_id does not exist". -ALTER TABLE payments RENAME COLUMN razorpay_order_id TO payu_txnid; -ALTER TABLE payments RENAME COLUMN razorpay_payment_id TO payu_mihpayid; +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'payments' AND column_name = 'razorpay_order_id') THEN + ALTER TABLE payments RENAME COLUMN razorpay_order_id TO payu_txnid; + END IF; + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'payments' AND column_name = 'razorpay_payment_id') THEN + ALTER TABLE payments RENAME COLUMN razorpay_payment_id TO payu_mihpayid; + END IF; +END $$; diff --git a/crates/db/migrations/20260716500000_users_litellm_key.up.sql b/crates/db/migrations/20260716500000_users_litellm_key.up.sql index 192496d..dd74e1c 100644 --- a/crates/db/migrations/20260716500000_users_litellm_key.up.sql +++ b/crates/db/migrations/20260716500000_users_litellm_key.up.sql @@ -3,6 +3,6 @@ BEGIN; -ALTER TABLE users ADD COLUMN litellm_key TEXT; +ALTER TABLE users ADD COLUMN IF NOT EXISTS litellm_key TEXT; COMMIT;