# 🚀 Deployment Checklist - MariaDB 5.1.69

## Pre-Deployment

### 1. Environment Configuration
- [ ] Copy `.env.example` to `.env`
- [ ] Update database credentials:
  ```env
  DB_HOST=172.16.2.97
  DB_PORT=3306
  DB_DATABASE=sales_service1
  DB_USERNAME=root
  DB_PASSWORD=your_password
  DB_PREFIX=crm
  DB_CHARSET=utf8
  DB_COLLATION=utf8_general_ci
  ```
- [ ] Update application URL:
  ```env
  APP_URL=http://172.16.2.97:900
  APP_ENV=production
  APP_DEBUG=false
  ```

### 2. Verify PHP Version
- [ ] PHP >= 8.2.0 (confirmed: 8.2.20 on server)
- [ ] Required extensions: pdo_mysql, mbstring, openssl

### 3. Clear Caches
```bash
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
```

## Deployment Steps

### 1. Test Database Connection
```bash
php artisan tinker
```
Then run:
```php
DB::connection()->getPdo();
DB::select('SELECT @@character_set_database, @@collation_database');
exit
```

Expected output:
- `@@character_set_database: utf8`
- `@@collation_database: utf8_general_ci`

### 2. Run Migrations
```bash
# Fresh migration (WARNING: drops all tables)
php artisan migrate:fresh

# Or regular migration
php artisan migrate
```

### 3. Verify Tables Created
```bash
php artisan tinker
```
```php
DB::select('SHOW TABLES');
exit
```

Expected tables:
- users
- customers
- helpdesk_teams
- helpdesk_teams_member
- tags
- within
- sla_policies
- my_tickets
- all_tickets
- ticket_logs
- ticket_notifications
- ticket_images
- my_ticket_images
- failed_jobs
- personal_access_tokens

### 4. Seed Database (if needed)
```bash
php artisan db:seed
```

### 5. Set Permissions
```bash
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data storage bootstrap/cache
```

### 6. Generate Application Key (if not set)
```bash
php artisan key:generate
```

### 7. Create Storage Link
```bash
php artisan storage:link
```

### 8. Optimize for Production
```bash
php artisan config:cache
php artisan route:cache
php artisan view:cache
```

## Post-Deployment Verification

### 1. Check Application Access
- [ ] Visit: `http://172.16.2.97:900`
- [ ] Login page loads correctly
- [ ] No errors in browser console

### 2. Test Core Functionality
- [ ] User login works
- [ ] Create ticket works
- [ ] View tickets works
- [ ] Upload images works
- [ ] Notifications work

### 3. Check Logs
```bash
tail -f storage/logs/laravel.log
```

### 4. Verify Database Charset
```sql
SELECT 
    table_name,
    table_collation 
FROM information_schema.tables 
WHERE table_schema = 'sales_service1';
```

All tables should show `utf8_general_ci`

## Troubleshooting

### Error: "Unknown collation: 'utf8mb4_unicode_ci'"
**Solution:**
```bash
php artisan config:clear
# Verify .env has:
# DB_CHARSET=utf8
# DB_COLLATION=utf8_general_ci
```

### Error: "SQLSTATE[HY000] [2002] Connection refused"
**Solution:**
- Check DB_HOST is correct (172.16.2.97)
- Verify MySQL is running on server
- Check firewall allows port 3306

### Error: "Syntax error near 'json'"
**Solution:**
- All migrations updated to use `text` instead of `json`
- Run: `php artisan migrate:fresh`

### Error: "Incorrect string value"
**Solution:**
- Data contains emoji or 4-byte UTF-8 characters
- Remove emoji from input
- Or upgrade to MySQL 5.5.3+ with utf8mb4

### Permission Denied Errors
**Solution:**
```bash
sudo chown -R www-data:www-data /var/www/html/laravel-crm
sudo chmod -R 775 /var/www/html/laravel-crm/storage
sudo chmod -R 775 /var/www/html/laravel-crm/bootstrap/cache
```

## Rollback Plan

### If Migration Fails
```bash
# Rollback last migration
php artisan migrate:rollback

# Rollback all migrations
php artisan migrate:reset

# Check migration status
php artisan migrate:status
```

### If Application Breaks
1. Restore previous `.env` file
2. Clear all caches
3. Rollback migrations
4. Check error logs

## Monitoring

### Check Application Health
```bash
# Check queue workers
php artisan queue:work --once

# Check scheduled tasks
php artisan schedule:list

# Monitor logs
tail -f storage/logs/laravel.log
```

### Database Monitoring
```sql
-- Check table sizes
SELECT 
    table_name,
    ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.tables
WHERE table_schema = 'sales_service1'
ORDER BY (data_length + index_length) DESC;

-- Check connection count
SHOW STATUS WHERE variable_name = 'Threads_connected';
```

## Success Criteria

- [ ] All migrations completed without errors
- [ ] All tables created with correct charset (utf8)
- [ ] Application accessible at http://172.16.2.97:900
- [ ] Users can login
- [ ] Tickets can be created and viewed
- [ ] No errors in Laravel logs
- [ ] Database connections stable

## Support Contacts

- **Database Issues**: Contact DBA
- **Application Issues**: Check `storage/logs/laravel.log`
- **Server Issues**: Contact System Administrator

## Documentation References

- [Legacy Database Setup Guide](docs/legacy-database-setup.md)
- [Migration Audit Summary](docs/migration-audit-summary.md)
- [Laravel Documentation](https://laravel.com/docs)
