# Database Migration Guide - Agent 1 ## Task: Replace vehicles.* schema with new ETL-generated database **Status**: Ready for Implementation **Dependencies**: None (can start immediately) **Estimated Time**: 30 minutes **Assigned To**: Agent 1 (Database) --- ## Overview Replace the normalized vehicles.* schema with a denormalized vehicle_options table populated from ETL-generated data (1.1M+ records from 1980-2026). --- ## Prerequisites ### Required Files All files are already present in the repository: ``` data/make-model-import/migrations/001_create_vehicle_database.sql data/make-model-import/output/01_engines.sql data/make-model-import/output/02_transmissions.sql data/make-model-import/output/03_vehicle_options.sql ``` ### Database Access ```bash # Verify Docker container is running docker ps | grep mvp-postgres # Access PostgreSQL docker exec -it mvp-postgres psql -U postgres -d motovaultpro ``` --- ## Step 1: Backup Current Schema (Safety) Before making any changes, backup the existing vehicles.* schema: ```bash # Create backup directory mkdir -p data/backups # Dump vehicles schema only docker exec mvp-postgres pg_dump -U postgres -d motovaultpro \ --schema=vehicles \ --format=plain \ --file=/tmp/vehicles_schema_backup.sql # Copy backup to host docker cp mvp-postgres:/tmp/vehicles_schema_backup.sql \ data/backups/vehicles_schema_backup_$(date +%Y%m%d_%H%M%S).sql # Verify backup exists ls -lh data/backups/ ``` **Verification**: Backup file should be 100KB-1MB in size --- ## Step 2: Drop Existing vehicles.* Tables Drop all normalized tables in the vehicles schema: ```bash docker exec -it mvp-postgres psql -U postgres -d motovaultpro < 10 minutes **Solution**: 1. Check Docker resources (increase memory/CPU if needed) 2. Check disk space: `df -h` 3. Check PostgreSQL logs: `docker logs mvp-postgres` 4. Try importing in smaller batches (split SQL files if necessary) ### Performance Issues **Symptom**: Queries take > 100ms **Solution**: ```bash # Verify indexes were created docker exec mvp-postgres psql -U postgres -d motovaultpro \ -c "\di vehicle_options*" # Analyze tables for query optimizer docker exec mvp-postgres psql -U postgres -d motovaultpro <.sql mvp-postgres:/tmp/ docker exec -i mvp-postgres psql -U postgres -d motovaultpro \ < /tmp/vehicles_schema_backup_.sql ``` --- ## Handoff to Agent 2 Once complete, provide this information to Agent 2 (Platform Repository): ### Database Contract **Tables Available:** ```sql engines (id, name) transmissions (id, type) vehicle_options (id, year, make, model, trim, engine_id, transmission_id) ``` **Functions Available:** ```sql get_makes_for_year(year INT) → TABLE(make VARCHAR) get_models_for_year_make(year INT, make VARCHAR) → TABLE(model VARCHAR) get_trims_for_year_make_model(year INT, make VARCHAR, model VARCHAR) → TABLE(trim_name VARCHAR) get_options_for_vehicle(year INT, make VARCHAR, model VARCHAR, trim VARCHAR) → TABLE(engine_name VARCHAR, transmission_type VARCHAR, ...) ``` **Data Quality Notes:** - Makes are in Title Case: "Ford", not "FORD" - 1.1% of records have NULL engine_id (electric vehicles) - Year range: 1980-2026 - 53 makes, 1,741 models, 1,122,644 total configurations **Performance:** - All queries using indexes perform sub-50ms - Cascade queries optimized with composite indexes ### Verification Command Agent 2 can verify database is ready: ```bash docker exec mvp-postgres psql -U postgres -d motovaultpro \ -c "SELECT COUNT(*) FROM vehicle_options;" ``` Should return: 1122644 --- ## Completion Message Template ``` Agent 1 (Database Migration): COMPLETE Changes Made: - Dropped vehicles.* schema tables (backup created) - Executed 001_create_vehicle_database.sql migration - Imported 30,066 engines - Imported 828 transmissions - Imported 1,122,644 vehicle options Verification: ✓ All tables created with correct record counts ✓ Database functions operational ✓ Composite indexes created ✓ Query performance sub-50ms ✓ Data quality checks passed Database is ready for Agent 2 (Platform Repository) to begin implementation. Files modified: None (database only) New schema: public.engines, public.transmissions, public.vehicle_options ``` --- **Document Version**: 1.0 **Last Updated**: 2025-11-10 **Status**: Ready for Implementation