Skip to content

fix: add missing customer_email and total_amount columns to orders table - #11

Open
asarkar157 wants to merge 1 commit into
mainfrom
fix/add-customer-email-to-orders
Open

fix: add missing customer_email and total_amount columns to orders table#11
asarkar157 wants to merge 1 commit into
mainfrom
fix/add-customer-email-to-orders

Conversation

@asarkar157

Copy link
Copy Markdown

Problem

The orders table schema in cmd/initdb/main.go was missing the customer_email and total_amount columns that the application handler code expects, causing HTTP 500 errors on every POST /api/orders request.

Error:

SQL logic error: table orders has no column named customer_email (1)

Root Cause

Schema divergence between DB initialization and application code:

  • internal/handlers/orders.go writes INSERT INTO orders (customer_email, total_amount, status)
  • cmd/initdb/main.go CREATE TABLE only defined amount and status columns

The column customer_email (and total_amount) were added to the handler layer but the initdb schema was never updated.

Fix

  1. Updated CREATE TABLE IF NOT EXISTS orders DDL to include customer_email TEXT NOT NULL and total_amount REAL NOT NULL — fixes all fresh database initializations.
  2. Added ALTER TABLE migration statements for existing databases already initialized without these columns — fixes running deployments without requiring a full DB wipe.
  3. Added isDuplicateColumnErr helper to gracefully skip ALTER TABLE when columns already exist (idempotent migrations).

Files Changed

  • cmd/initdb/main.go — schema DDL + migration statements

Verification

After applying this fix and running make init-db (or restarting with the updated initdb):

  • POST /api/orders should return HTTP 201 instead of 500
  • Datadog APM monitor [aiden-demo] order-service HTTP errors should recover from Alert → OK within 1–2 minutes
  • [aiden-demo] order-service schema mismatch logs monitor should remain OK

Created using StackGen

…ble\n\nThe initdb schema was missing the customer_email and total_amount columns\nthat the application handler code writes to and reads from. This caused\nHTTP 500 errors on every POST /api/orders request with the error:\n\n SQL logic error: table orders has no column named customer_email (1)\n\nChanges:\n- Add customer_email TEXT NOT NULL and total_amount REAL NOT NULL to the\n CREATE TABLE IF NOT EXISTS orders DDL (fixes fresh databases)\n- Add ALTER TABLE migration statements for existing databases that were\n initialized without these columns (fixes running deployments)\n- Add isDuplicateColumnErr helper to gracefully ignore duplicate-column\n errors when ALTER TABLE runs on already-migrated databases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant