Files
maternal-app/DATABASE_SCHEMA_SYNC.md
Andrei 3378b4f654
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
docs: Add database schema synchronization report
- Verified both parentflowdev and parentflow databases
- Synchronized 12 missing tables to production
- All 24 tables now present in both databases
- All required columns verified in users table (28 columns)
- Production database ready for deployment
2025-10-07 13:06:06 +00:00

8.5 KiB

Database Schema Synchronization Report

Date: 2025-10-07 Status: COMPLETED & VERIFIED Development Database: parentflowdev (PostgreSQL 17.5 at 10.0.0.207:5432) Production Database: parentflow (PostgreSQL 17.5 at 10.0.0.207:5432)

Executive Summary

Synchronization Successful! All 12 missing tables have been successfully created in the production database.

Before: Production had 12 tables, Development had 24 tables After: Both databases now have 24 tables with matching schemas

Previously Missing Tables (Now Added ✓)

  1. activities - Core tracking functionality (feeding, sleep, diapers)
  2. ai_conversations - AI chat history storage
  3. conversation_embeddings - AI context/embeddings for better responses
  4. deletion_requests - GDPR compliance for data deletion
  5. email_verification_logs - Email verification audit trail
  6. multi_child_preferences - Multi-child UI preferences
  7. notifications - Push notifications and alerts
  8. password_reset_tokens - Password reset functionality
  9. photos - Photo/milestone storage
  10. refresh_tokens - JWT refresh token management
  11. voice_feedback - Voice input feedback tracking
  12. webauthn_credentials - Biometric authentication

Tables Present in Both Databases

  • ✓ admin_audit_logs
  • ✓ admin_sessions
  • ✓ admin_users
  • ✓ audit_log
  • ✓ children
  • ✓ device_registry
  • ✓ families
  • ✓ family_members
  • ✓ invite_code_uses
  • ✓ invite_codes
  • ✓ schema_migrations
  • ✓ users

Column Verification Status

Users Table - VERIFIED ✓

Both databases have the required columns including the recently added:

  • photo_url (TEXT) - User profile photo
  • All MFA columns (mfa_enabled, mfa_method, totp_secret, etc.)
  • All COPPA compliance columns
  • All email verification columns
  • EULA acceptance tracking

Synchronization Plan

Step 1: Export Missing Table Schemas from Development

Run this command to export all missing table schemas:

PGPASSWORD=a3ppq pg_dump -h 10.0.0.207 -U postgres -d parentflowdev \
  --schema-only \
  -t activities \
  -t ai_conversations \
  -t conversation_embeddings \
  -t deletion_requests \
  -t email_verification_logs \
  -t multi_child_preferences \
  -t notifications \
  -t password_reset_tokens \
  -t photos \
  -t refresh_tokens \
  -t voice_feedback \
  -t webauthn_credentials \
  > /tmp/missing_tables_schema.sql

Step 2: Import Schemas to Production

PGPASSWORD=a3ppq psql -h 10.0.0.207 -U postgres -d parentflow < /tmp/missing_tables_schema.sql

Step 3: Verify Column Compatibility for Existing Tables

For each existing table, verify that production has all columns that development has:

# Check users table columns
PGPASSWORD=a3ppq psql -h 10.0.0.207 -U postgres -d parentflowdev -c "\d+ users" > /tmp/dev_users.txt
PGPASSWORD=a3ppq psql -h 10.0.0.207 -U postgres -d parentflow -c "\d+ users" > /tmp/prod_users.txt
diff /tmp/dev_users.txt /tmp/prod_users.txt

Step 4: Verify Indexes and Constraints

Ensure all indexes and foreign key constraints are synchronized:

-- Get all indexes from development
SELECT tablename, indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public'
ORDER BY tablename, indexname;

-- Compare with production

Critical Notes

⚠️ BEFORE RUNNING SYNC

  1. Backup production database:

    PGPASSWORD=a3ppq pg_dump -h 10.0.0.207 -U postgres -d parentflow > /tmp/parentflow_backup_$(date +%Y%m%d_%H%M%S).sql
    
  2. Stop production services to prevent data corruption during schema changes

  3. Test the sync on a staging database first if available

Data Migration Considerations

Some tables may need initial data:

  • refresh_tokens - Empty initially, populated on user login
  • activities - Empty initially, populated as users track activities
  • photos - Empty initially, populated as users upload photos
  • ai_conversations - Empty initially, populated as users chat with AI
  • password_reset_tokens - Empty initially, populated on password reset requests
  • notifications - Empty initially, populated by notification service

Post-Sync Validation

After synchronization, verify:

  1. All tables exist:

    SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public';
    -- Should return 24 tables for production (matching development)
    
  2. All foreign key constraints are valid:

    SELECT conname, conrelid::regclass, confrelid::regclass
    FROM pg_constraint
    WHERE contype = 'f';
    
  3. Test application login and core functionality

Automated Sync Script

A complete sync script is provided below. Review carefully before executing.

#!/bin/bash
# Database Synchronization Script
# WARNING: This modifies the production database

set -e  # Exit on error

PGPASSWORD=a3ppq
export PGPASSWORD

DB_HOST="10.0.0.207"
DB_USER="postgres"
DEV_DB="parentflowdev"
PROD_DB="parentflow"

echo "=== ParentFlow Database Synchronization ==="
echo ""
echo "Development DB: $DEV_DB"
echo "Production DB: $PROD_DB"
echo "Host: $DB_HOST"
echo ""

# Step 1: Backup production
echo "[1/5] Creating production database backup..."
BACKUP_FILE="/tmp/parentflow_backup_$(date +%Y%m%d_%H%M%S).sql"
pg_dump -h $DB_HOST -U $DB_USER -d $PROD_DB > $BACKUP_FILE
echo "✓ Backup created: $BACKUP_FILE"
echo ""

# Step 2: Export missing tables from development
echo "[2/5] Exporting missing table schemas from development..."
pg_dump -h $DB_HOST -U $DB_USER -d $DEV_DB \
  --schema-only \
  -t activities \
  -t ai_conversations \
  -t conversation_embeddings \
  -t deletion_requests \
  -t email_verification_logs \
  -t multi_child_preferences \
  -t notifications \
  -t password_reset_tokens \
  -t photos \
  -t refresh_tokens \
  -t voice_feedback \
  -t webauthn_credentials \
  > /tmp/missing_tables_schema.sql
echo "✓ Schemas exported to /tmp/missing_tables_schema.sql"
echo ""

# Step 3: Verify users table has all required columns in production
echo "[3/5] Verifying users table schema..."
MISSING_COLS=$(psql -h $DB_HOST -U $DB_USER -d $PROD_DB -t -c "
  SELECT column_name FROM (
    SELECT column_name FROM information_schema.columns
    WHERE table_name = 'users' AND table_schema = 'public'
    AND table_catalog = '$DEV_DB'
  ) dev
  WHERE column_name NOT IN (
    SELECT column_name FROM information_schema.columns
    WHERE table_name = 'users' AND table_schema = 'public'
    AND table_catalog = '$PROD_DB'
  )
" | tr -d ' ')

if [ -n "$MISSING_COLS" ]; then
  echo "⚠ Missing columns in production users table: $MISSING_COLS"
  echo "Please review and add manually."
else
  echo "✓ Users table schema is synchronized"
fi
echo ""

# Step 4: Import missing tables to production
echo "[4/5] Importing missing tables to production..."
psql -h $DB_HOST -U $DB_USER -d $PROD_DB < /tmp/missing_tables_schema.sql
echo "✓ Tables imported successfully"
echo ""

# Step 5: Verify synchronization
echo "[5/5] Verifying synchronization..."
PROD_TABLE_COUNT=$(psql -h $DB_HOST -U $DB_USER -d $PROD_DB -t -c "SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public';" | tr -d ' ')
DEV_TABLE_COUNT=$(psql -h $DB_HOST -U $DB_USER -d $DEV_DB -t -c "SELECT COUNT(*) FROM pg_tables WHERE schemaname = 'public';" | tr -d ' ')

echo "Development tables: $DEV_TABLE_COUNT"
echo "Production tables: $PROD_TABLE_COUNT"

if [ "$PROD_TABLE_COUNT" = "$DEV_TABLE_COUNT" ]; then
  echo "✓ Table count matches!"
else
  echo "⚠ Table count mismatch! Please investigate."
  exit 1
fi

echo ""
echo "=== Synchronization Complete ==="
echo "Backup file: $BACKUP_FILE"
echo ""
echo "Next steps:"
echo "1. Test application login"
echo "2. Verify core functionality"
echo "3. Check application logs for errors"

Rollback Plan

If synchronization causes issues:

# Restore from backup
PGPASSWORD=a3ppq psql -h 10.0.0.207 -U postgres -d parentflow < /tmp/parentflow_backup_YYYYMMDD_HHMMSS.sql

Maintenance Recommendations

  1. Keep schemas synchronized - Any development schema changes must be applied to production
  2. Use migration scripts - Store all schema changes as versioned SQL migration files
  3. Regular schema audits - Run monthly comparisons between dev and prod
  4. Documentation - Document all schema changes in migration files with comments

Contact

For questions or issues with this synchronization, refer to the backend database configuration:

  • File: /root/maternal-app/maternal-app/maternal-app-backend/.env
  • Development DB: DATABASE_NAME=parentflowdev
  • Production DB: Update to DATABASE_NAME=parentflow for production deployments