gcp-hockey-results/motm_app/WHERE_CLAUSE_FIXES.md

2.9 KiB

SQL WHERE Clause Fixes

Problem

When activating the MOTM vote, the application was throwing an error: "Database not initialized. Please go to Database Setup to initialize the database."

Root Cause

Multiple SQL queries on the admin_settings table were missing WHERE userid = 'admin' clauses, causing:

  1. UPDATE queries to update ALL rows instead of just the admin row
  2. SELECT queries to return unexpected results
  3. The application to think the database wasn't initialized

Fixes Applied

1. UPDATE Queries Fixed

Line 599 - Main settings update:

-- BEFORE:
UPDATE admin_settings SET next_date = :next_date, next_club = :next_club, ...

-- AFTER:
UPDATE admin_settings SET next_date = :next_date, next_club = :next_club, ... WHERE userid = 'admin'

Line 613 - Opponent logo update:

-- BEFORE:
UPDATE admin_settings SET oppo_logo = :logo_url

-- AFTER:
UPDATE admin_settings SET oppo_logo = :logo_url WHERE userid = 'admin'

2. SELECT Queries Fixed

Line 256 - MOTM vote page:

-- BEFORE:
SELECT next_club, next_team, next_date, ... FROM admin_settings

-- AFTER:
SELECT next_club, next_team, next_date, ... FROM admin_settings WHERE userid = 'admin'

Line 348 - Match comments page:

-- BEFORE:
SELECT next_club, next_team, next_date, oppo_logo, hkfc_logo FROM admin_settings

-- AFTER:
SELECT next_club, next_team, next_date, oppo_logo, hkfc_logo FROM admin_settings WHERE userid = 'admin'

Line 683 - Admin settings page:

-- BEFORE:
SELECT next_club, oppo_logo FROM admin_settings

-- AFTER:
SELECT next_club, oppo_logo FROM admin_settings WHERE userid = 'admin'

Why This Matters

Without the WHERE clause:

  • UPDATE queries would modify all rows in the table (even if there's only one row, this is bad practice)
  • SELECT queries might return multiple rows when only one is expected
  • The application logic assumes only one admin settings row exists

Testing

After these fixes:

  1. The MOTM admin page should save settings correctly
  2. Activating the MOTM vote should work without errors
  3. The voting page should load correctly
  4. All admin settings queries will target only the admin row

Deployment

To apply these fixes to production:

  1. Commit the changes to your repository
  2. Rebuild and redeploy the Docker image
  3. Restart the application pods
# Build new image
docker build -t your-registry/motm-app:latest .

# Push to registry
docker push your-registry/motm-app:latest

# Deploy to Kubernetes
helm upgrade motm-app ./helm-chart/motm-app --namespace motm-app

These fixes are related to:

  • The voting deadline feature implementation
  • The table name migration from motmadminsettings to admin_settings
  • The column name migration to snake_case

Summary

All SQL queries on the admin_settings table now properly filter by userid = 'admin', ensuring:

  • Data integrity
  • Predictable query results
  • Proper application functionality