# Final Blockscout Fix - Corrected Commands

## Issues Found
1. `Explorer.Release.migrate/0 is undefined` - Need to use `bin/blockscout migrate` instead
2. docker-compose.yml syntax error - sed command created invalid YAML quotes

## Corrected Commands (Run in VMID 5000)

### Step 1: Run Migrations Using Correct Command

```bash
# Use 'migrate' command instead of 'eval'
docker run --rm \
  --network host \
  -e DATABASE_URL=postgresql://blockscout:blockscout@localhost:5432/blockscout \
  blockscout/blockscout:latest \
  bin/blockscout migrate
```

### Step 2: Verify Tables

```bash
docker exec blockscout-postgres psql -U blockscout -d blockscout -c "
SELECT 
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'migrations_status') 
    THEN '✅ migrations_status' ELSE '❌ MISSING' END,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'blocks') 
    THEN '✅ blocks' ELSE '❌ MISSING' END,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'transactions') 
    THEN '✅ transactions' ELSE '❌ MISSING' END;
"
```

### Step 3: Fix docker-compose.yml Properly

```bash
cd /opt/blockscout

# Check current command
grep -A 1 "command:" docker-compose.yml

# Backup
cp docker-compose.yml docker-compose.yml.backup

# Method 1: Use Python to properly escape (if available)
python3 << 'PYTHON'
import re

with open('docker-compose.yml', 'r') as f:
    content = f.read()

# Replace command line with properly escaped version
pattern = r'command:\s*.*blockscout start'
replacement = 'command: sh -c "bin/blockscout migrate && bin/blockscout start"'

content = re.sub(pattern, replacement, content)

with open('docker-compose.yml', 'w') as f:
    f.write(content)

print("✅ Updated docker-compose.yml")
PYTHON

# Method 2: Manual edit (if Python not available)
# Edit docker-compose.yml manually and change:
# command: /app/bin/blockscout start
# To:
# command: sh -c "bin/blockscout migrate && bin/blockscout start"
```

### Step 4: Alternative - Edit docker-compose.yml Manually

If sed is causing issues, edit manually:

```bash
cd /opt/blockscout
nano docker-compose.yml  # or vi docker-compose.yml

# Find the line with:
#   command: /app/bin/blockscout start
# Or:
#   command: bin/blockscout start

# Replace with:
#   command: sh -c "bin/blockscout migrate && bin/blockscout start"

# Save and exit
```

### Step 5: Restart

```bash
cd /opt/blockscout
docker-compose down blockscout
docker-compose up -d blockscout

# Wait and check
sleep 30
docker ps | grep blockscout
docker logs blockscout 2>&1 | tail -20
```

## Complete One-Line Fix (Manual Edit Required)

```bash
# Run migrations
docker run --rm --network host -e DATABASE_URL=postgresql://blockscout:blockscout@localhost:5432/blockscout blockscout/blockscout:latest bin/blockscout migrate && \
cd /opt/blockscout && \
# Then manually edit docker-compose.yml to add: command: sh -c "bin/blockscout migrate && bin/blockscout start" && \
docker-compose restart blockscout
```

