refactor: standardize database env variables to use DB_* prefix
Some checks failed
Deploy to Production / deploy (push) Failing after 8s

- Renamed MYSQL_ROOT_PASSWORD to DB_ROOT_PASSWORD and MYSQL_PASSWORD to DB_PASSWORD for consistent naming
- Updated docker-compose.yml to map DB_* variables to MySQL container's expected MYSQL_* format
- Removed redundant DATABASE_URL variable since individual DB_* variables are now used
- Added default values for DB_USER, DB_NAME, DB_HOST, and DB_PORT in docker-compose.yml
- Updated all documentation files to reference new standar
This commit is contained in:
Ender 2025-10-28 13:11:57 +01:00
parent 669fff9843
commit 26c3c0bb0e
11 changed files with 556 additions and 39 deletions

View File

@ -1,6 +1,11 @@
# Database
MYSQL_ROOT_PASSWORD=your_root_password_here
MYSQL_PASSWORD=your_mysql_password_here
# Database Configuration
# All database variables use DB_* prefix for consistency
DB_ROOT_PASSWORD=your_root_password_here
DB_PASSWORD=your_mysql_password_here
DB_USER=voxblog
DB_NAME=voxblog
DB_HOST=localhost
DB_PORT=3306
# Application
ADMIN_PASSWORD=your_admin_password_here

View File

@ -123,15 +123,12 @@ docker exec voxblog-api sh -c "cd /app/apps/api && pnpm drizzle:migrate"
Database connection uses these variables from `.env`:
```env
# MySQL Root
MYSQL_ROOT_PASSWORD=voxblogRootPass123!
# Application User
MYSQL_PASSWORD=voxblogAppPass123!
# Database Configuration (all use DB_* prefix)
DB_ROOT_PASSWORD=voxblogRootPass123!
DB_PASSWORD=voxblogAppPass123!
DB_HOST=mysql
DB_PORT=3306
DB_USER=voxblog
DB_PASSWORD=voxblogAppPass123!
DB_NAME=voxblog
```

84
DB_VARS_UNIFIED.md Normal file
View File

@ -0,0 +1,84 @@
# Database Variables Unified ✅
## Summary
All database configuration variables have been standardized to use the `DB_*` prefix for consistency and simplicity.
## Quick Reference
### Old Variable Names (REMOVED)
- ❌ `MYSQL_ROOT_PASSWORD`
- ❌ `MYSQL_PASSWORD`
- ❌ `DATABASE_URL`
### New Variable Names (CURRENT)
- ✅ `DB_ROOT_PASSWORD` - MySQL root password
- ✅ `DB_PASSWORD` - Application database password
- ✅ `DB_USER` - Database username (default: `voxblog`)
- ✅ `DB_NAME` - Database name (default: `voxblog`)
- ✅ `DB_HOST` - Database host (default: `mysql` in docker, `localhost` otherwise)
- ✅ `DB_PORT` - Database port (default: `3306`)
## Files Updated
1. **`.env.example`** - Updated with unified variable names and defaults
2. **`docker-compose.yml`** - MySQL container and API service use `DB_*` variables
3. **`scripts/migrate-db-vars.sh`** - Migration helper script
4. **`docs/DB_VARIABLE_MIGRATION.md`** - Comprehensive migration guide
## Application Code (No Changes Needed)
The following files already used `DB_*` variables:
- ✅ `apps/api/src/db.ts`
- ✅ `apps/api/drizzle.config.ts`
- ✅ `apps/api/src/db/migrate.ts`
## Migration Instructions
### For Existing Users
Run the migration script:
```bash
./scripts/migrate-db-vars.sh
```
Or manually update your `.env`:
```bash
# Replace:
MYSQL_ROOT_PASSWORD=xxx → DB_ROOT_PASSWORD=xxx
MYSQL_PASSWORD=xxx → DB_PASSWORD=xxx
```
Then restart services:
```bash
docker-compose down && docker-compose up -d
```
### For New Users
Just copy and configure:
```bash
cp .env.example .env
# Edit .env with your passwords
docker-compose up -d
```
## Benefits
1. **Consistency** - Single naming convention across the entire project
2. **Simplicity** - Easier to remember and use
3. **Clarity** - Clear distinction between container and application config
4. **Flexibility** - Default values for easier local development
5. **Maintainability** - Less confusion, fewer errors
## Why This Change?
Previously, the project mixed two naming conventions:
- `MYSQL_*` for some variables (from MySQL Docker image convention)
- `DB_*` for application code
This created confusion about which variables to set and where. Now everything uses `DB_*`, and docker-compose handles the mapping to MySQL container's expected `MYSQL_*` environment variables internally.
## Need Help?
See the full migration guide: `docs/DB_VARIABLE_MIGRATION.md`

View File

@ -181,10 +181,10 @@ services:
container_name: voxblog-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: voxblog
MYSQL_USER: voxblog
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME:-voxblog}
MYSQL_USER: ${DB_USER:-voxblog}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
networks:
@ -206,7 +206,11 @@ services:
environment:
NODE_ENV: production
PORT: 3301
DATABASE_URL: mysql://voxblog:${MYSQL_PASSWORD}@mysql:3306/voxblog
DB_HOST: ${DB_HOST:-mysql}
DB_PORT: ${DB_PORT:-3306}
DB_USER: ${DB_USER:-voxblog}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME:-voxblog}
ADMIN_PASSWORD: ${ADMIN_PASSWORD}
OPENAI_API_KEY: ${OPENAI_API_KEY}
GHOST_ADMIN_API_KEY: ${GHOST_ADMIN_API_KEY}
@ -274,8 +278,12 @@ jobs:
- name: Create .env file
run: |
cat > .env << EOF
MYSQL_ROOT_PASSWORD=${{ secrets.MYSQL_ROOT_PASSWORD }}
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}
DB_ROOT_PASSWORD=${{ secrets.DB_ROOT_PASSWORD }}
DB_PASSWORD=${{ secrets.DB_PASSWORD }}
DB_USER=${{ secrets.DB_USER }}
DB_NAME=${{ secrets.DB_NAME }}
DB_HOST=${{ secrets.DB_HOST }}
DB_PORT=${{ secrets.DB_PORT }}
ADMIN_PASSWORD=${{ secrets.ADMIN_PASSWORD }}
OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}
GHOST_ADMIN_API_KEY=${{ secrets.GHOST_ADMIN_API_KEY }}

View File

@ -169,9 +169,13 @@ git push origin main
All required variables in `.env`:
```bash
# Database
MYSQL_ROOT_PASSWORD=strong_password
MYSQL_PASSWORD=voxblog_password
# Database (all use DB_* prefix)
DB_ROOT_PASSWORD=strong_password
DB_PASSWORD=voxblog_password
DB_USER=voxblog
DB_NAME=voxblog
DB_HOST=mysql
DB_PORT=3306
# Application
ADMIN_PASSWORD=admin_password

View File

@ -34,9 +34,13 @@ nano .env
**Minimal .env for local testing:**
```bash
# Database
MYSQL_ROOT_PASSWORD=localrootpass123
MYSQL_PASSWORD=localvoxblogpass123
# Database (all use DB_* prefix)
DB_ROOT_PASSWORD=localrootpass123
DB_PASSWORD=localvoxblogpass123
DB_USER=voxblog
DB_NAME=voxblog
DB_HOST=mysql
DB_PORT=3306
# Application
ADMIN_PASSWORD=admin123
@ -162,7 +166,7 @@ docker-compose exec api sh
# MySQL container
docker-compose exec mysql mysql -u voxblog -p
# Enter MYSQL_PASSWORD when prompted
# Enter DB_PASSWORD when prompted
```
### Clean Up Everything

145
MIGRATION_COMPLETE.md Normal file
View File

@ -0,0 +1,145 @@
# Database Variable Migration Complete ✅
## Summary
All database configuration variables across the entire project have been successfully unified to use the `DB_*` prefix.
## Files Updated
### Configuration Files
- ✅ `.env.example` - Updated with unified `DB_*` variables
- ✅ `docker-compose.yml` - Uses `DB_*` variables with proper MySQL container mapping
### Documentation Files
- ✅ `QUICK_START.md` - All references updated
- ✅ `DATABASE_SETUP.md` - All references updated
- ✅ `DEPLOYMENT_SUMMARY.md` - All references updated
- ✅ `DEPLOYMENT_GUIDE.md` - Docker compose example and GitHub Actions updated
- ✅ `LOCAL_TESTING.md` - All references updated
### Migration Tools
- ✅ `scripts/migrate-db-vars.sh` - Helper script for existing installations
- ✅ `docs/DB_VARIABLE_MIGRATION.md` - Comprehensive migration guide
- ✅ `DB_VARS_UNIFIED.md` - Quick reference guide
### Application Code
- ✅ `apps/api/src/db.ts` - Already using `DB_*` (no changes needed)
- ✅ `apps/api/drizzle.config.ts` - Already using `DB_*` (no changes needed)
- ✅ `apps/api/src/db/migrate.ts` - Already using `DB_*` (no changes needed)
## Unified Variable Names
| Variable | Purpose | Default |
|----------|---------|---------|
| `DB_ROOT_PASSWORD` | MySQL root password | - |
| `DB_PASSWORD` | Application database password | - |
| `DB_USER` | Database username | `voxblog` |
| `DB_NAME` | Database name | `voxblog` |
| `DB_HOST` | Database host | `mysql` (docker) / `localhost` |
| `DB_PORT` | Database port | `3306` |
## What Was Changed
### Before
```bash
# Mixed naming conventions
MYSQL_ROOT_PASSWORD=xxx
MYSQL_PASSWORD=xxx
DATABASE_URL=mysql://...
DB_HOST=mysql
DB_PORT=3306
DB_USER=voxblog
DB_PASSWORD=xxx # Duplicate!
DB_NAME=voxblog
```
### After
```bash
# Unified DB_* naming
DB_ROOT_PASSWORD=xxx
DB_PASSWORD=xxx
DB_USER=voxblog
DB_NAME=voxblog
DB_HOST=mysql
DB_PORT=3306
```
## Benefits Achieved
1. ✅ **Consistency** - Single naming pattern across entire codebase
2. ✅ **Simplicity** - No more confusion about which variable to use
3. ✅ **No Duplication** - Removed redundant `DATABASE_URL` and duplicate password variables
4. ✅ **Better Defaults** - All variables have sensible defaults in docker-compose
5. ✅ **Clear Documentation** - All docs updated with correct variable names
6. ✅ **Easy Migration** - Script provided for existing installations
## Verification
Run this to verify no legacy variables remain in your project:
```bash
# Should only show references in migration docs (as "before" examples)
grep -r "MYSQL_ROOT_PASSWORD\|MYSQL_PASSWORD" --include="*.md" --include="*.yml" --include="*.env*" .
```
## Next Steps for Users
### New Installations
Just use the updated `.env.example`:
```bash
cp .env.example .env
# Edit .env with your values
docker-compose up -d
```
### Existing Installations
Run the migration script:
```bash
./scripts/migrate-db-vars.sh
docker-compose down
docker-compose up -d
```
## Technical Notes
### MySQL Container Mapping
The MySQL Docker container still requires `MYSQL_*` environment variables internally. Our `docker-compose.yml` handles this mapping:
```yaml
mysql:
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} # Maps DB_* to MYSQL_*
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USER:-voxblog}
MYSQL_DATABASE: ${DB_NAME:-voxblog}
```
This is transparent to users - they only need to set `DB_*` variables.
### Application Code
The application code in `apps/api/src/db.ts` and related files already used `DB_*` variables, so no code changes were needed. This migration was purely about configuration consistency.
## Rollback (If Needed)
If you need to rollback for any reason:
```bash
# Restore your .env backup
cp .env.backup.YYYYMMDD_HHMMSS .env
# Or manually change:
DB_ROOT_PASSWORD → MYSQL_ROOT_PASSWORD
DB_PASSWORD → MYSQL_PASSWORD
```
However, we recommend moving forward with the unified naming as it's cleaner and more maintainable.
## Support
- See `docs/DB_VARIABLE_MIGRATION.md` for detailed migration guide
- See `DB_VARS_UNIFIED.md` for quick reference
- Check docker-compose logs if services don't start: `docker-compose logs`
---
**Migration completed successfully!** 🎉
All database variables are now unified under the `DB_*` prefix for consistency and simplicity.

View File

@ -31,8 +31,12 @@ nano .env
```
Fill in all values:
- `MYSQL_ROOT_PASSWORD` - Strong password for MySQL root
- `MYSQL_PASSWORD` - Password for voxblog database user
- `DB_ROOT_PASSWORD` - Strong password for MySQL root
- `DB_PASSWORD` - Password for voxblog database user
- `DB_USER` - Database username (default: voxblog)
- `DB_NAME` - Database name (default: voxblog)
- `DB_HOST` - Database host (default: localhost)
- `DB_PORT` - Database port (default: 3306)
- `ADMIN_PASSWORD` - Password for admin login
- `OPENAI_API_KEY` - Your OpenAI API key
- `GHOST_ADMIN_API_KEY` - Your Ghost CMS API key
@ -101,8 +105,12 @@ sudo systemctl status gitea-runner
Go to: Repository → Settings → Secrets → Actions
Add all variables from `.env`:
- `MYSQL_ROOT_PASSWORD`
- `MYSQL_PASSWORD`
- `DB_ROOT_PASSWORD`
- `DB_PASSWORD`
- `DB_USER`
- `DB_NAME`
- `DB_HOST`
- `DB_PORT`
- `ADMIN_PASSWORD`
- `OPENAI_API_KEY`
- `GHOST_ADMIN_API_KEY`
@ -277,7 +285,7 @@ docker-compose logs admin
```bash
docker-compose exec mysql mysql -u voxblog -p
# Enter MYSQL_PASSWORD when prompted
# Enter DB_PASSWORD when prompted
SHOW DATABASES;
```

View File

@ -6,16 +6,16 @@ services:
container_name: voxblog-mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: voxblog
MYSQL_USER: voxblog
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME:-voxblog}
MYSQL_USER: ${DB_USER:-voxblog}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
networks:
- voxblog-network
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
interval: 10s
timeout: 5s
retries: 5
@ -34,12 +34,11 @@ services:
environment:
NODE_ENV: production
PORT: 3301
DATABASE_URL: mysql://voxblog:${MYSQL_PASSWORD}@mysql:3306/voxblog
DB_HOST: mysql
DB_PORT: 3306
DB_USER: voxblog
DB_PASSWORD: ${MYSQL_PASSWORD}
DB_NAME: voxblog
DB_HOST: ${DB_HOST:-mysql}
DB_PORT: ${DB_PORT:-3306}
DB_USER: ${DB_USER:-voxblog}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME:-voxblog}
ADMIN_PASSWORD: ${ADMIN_PASSWORD}
OPENAI_API_KEY: ${OPENAI_API_KEY}
GHOST_ADMIN_API_KEY: ${GHOST_ADMIN_API_KEY}

View File

@ -0,0 +1,166 @@
# Database Variable Migration Guide
## Overview
This project has been updated to use a **unified naming convention** for all database configuration variables. All database-related environment variables now use the `DB_*` prefix for consistency and simplicity.
## What Changed
### Before (Mixed Naming)
```bash
# .env file had:
MYSQL_ROOT_PASSWORD=...
MYSQL_PASSWORD=...
# docker-compose.yml used both:
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
MYSQL_PASSWORD=${MYSQL_PASSWORD}
DATABASE_URL=mysql://voxblog:${MYSQL_PASSWORD}@mysql:3306/voxblog
DB_HOST=mysql
DB_PORT=3306
DB_USER=voxblog
DB_PASSWORD=${MYSQL_PASSWORD}
DB_NAME=voxblog
```
### After (Unified Naming)
```bash
# All variables use DB_* prefix:
DB_ROOT_PASSWORD=...
DB_PASSWORD=...
DB_USER=voxblog
DB_NAME=voxblog
DB_HOST=localhost # or 'mysql' in docker
DB_PORT=3306
```
## Variable Reference
| Variable | Purpose | Default | Required |
|----------|---------|---------|----------|
| `DB_ROOT_PASSWORD` | MySQL root user password | - | Yes |
| `DB_PASSWORD` | Application database user password | - | Yes |
| `DB_USER` | Application database username | `voxblog` | No |
| `DB_NAME` | Application database name | `voxblog` | No |
| `DB_HOST` | Database host | `mysql` (docker) / `localhost` | No |
| `DB_PORT` | Database port | `3306` | No |
## Migration Steps
### For Existing Installations
1. **Backup your current `.env` file:**
```bash
cp .env .env.backup
```
2. **Run the migration script:**
```bash
chmod +x scripts/migrate-db-vars.sh
./scripts/migrate-db-vars.sh
```
Or manually update your `.env` file:
```bash
# Change these lines:
MYSQL_ROOT_PASSWORD=your_password
MYSQL_PASSWORD=your_password
# To:
DB_ROOT_PASSWORD=your_password
DB_PASSWORD=your_password
```
3. **Restart your services:**
```bash
docker-compose down
docker-compose up -d
```
### For New Installations
Simply copy `.env.example` to `.env` and fill in your values:
```bash
cp .env.example .env
# Edit .env with your database passwords
```
## Benefits of Unified Naming
1. **Consistency**: All database variables follow the same `DB_*` pattern
2. **Clarity**: Clear separation between MySQL container config and application config
3. **Simplicity**: Easier to remember and less prone to typos
4. **Flexibility**: Default values allow for easier local development
5. **Maintainability**: Single source of truth for database configuration
## Code Usage
All application code uses the unified variables:
```typescript
// apps/api/src/db.ts
const host = process.env.DB_HOST || 'localhost';
const port = Number(process.env.DB_PORT || 3306);
const user = process.env.DB_USER || '';
const password = process.env.DB_PASSWORD || '';
const database = process.env.DB_NAME || '';
```
## Docker Compose Configuration
The MySQL container environment variables are mapped from `DB_*` to `MYSQL_*`:
```yaml
mysql:
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME:-voxblog}
MYSQL_USER: ${DB_USER:-voxblog}
MYSQL_PASSWORD: ${DB_PASSWORD}
```
This maintains compatibility with the official MySQL Docker image while using our unified naming convention.
## Troubleshooting
### Services won't start after migration
1. Check that all required variables are set:
```bash
grep "^DB_" .env
```
2. Verify docker-compose can read the variables:
```bash
docker-compose config
```
3. Check container logs:
```bash
docker-compose logs mysql
docker-compose logs api
```
### Database connection errors
Ensure the `DB_PASSWORD` matches what was set when the MySQL container was first created. If you need to reset:
```bash
docker-compose down -v # WARNING: This deletes your database!
docker-compose up -d
```
## CI/CD and Deployment
If you have CI/CD pipelines or deployment scripts, update them to use the new variable names:
- Replace `MYSQL_ROOT_PASSWORD``DB_ROOT_PASSWORD`
- Replace `MYSQL_PASSWORD``DB_PASSWORD`
- Remove `DATABASE_URL` (no longer needed)
## Questions?
If you encounter any issues during migration, please check:
1. All variables are correctly renamed in your `.env` file
2. No old `MYSQL_*` variables remain
3. Services have been restarted after the change

97
scripts/migrate-db-vars.sh Executable file
View File

@ -0,0 +1,97 @@
#!/bin/bash
# Database Variable Migration Script
# This script helps migrate from MYSQL_* to unified DB_* variable names
#
# Usage: ./scripts/migrate-db-vars.sh [--dry-run]
set -e
DRY_RUN=false
if [[ "$1" == "--dry-run" ]]; then
DRY_RUN=true
echo "🔍 DRY RUN MODE - No files will be modified"
echo ""
fi
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE} Database Variable Migration Script${NC}"
echo -e "${BLUE}================================================${NC}"
echo ""
echo "This script will standardize database configuration variables:"
echo " MYSQL_ROOT_PASSWORD → DB_ROOT_PASSWORD"
echo " MYSQL_PASSWORD → DB_PASSWORD"
echo " MYSQL_DATABASE → DB_NAME (already standardized)"
echo " MYSQL_USER → DB_USER (already standardized)"
echo ""
# Check if .env file exists
if [ -f ".env" ]; then
echo -e "${YELLOW}⚠️ Found .env file${NC}"
echo ""
echo "Current database variables in .env:"
grep -E "^(MYSQL_|DB_)" .env 2>/dev/null || echo " (none found)"
echo ""
if [ "$DRY_RUN" = false ]; then
read -p "Do you want to update your .env file? (y/N) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Backup .env
cp .env .env.backup.$(date +%Y%m%d_%H%M%S)
echo -e "${GREEN}✓ Created backup of .env${NC}"
# Replace variables
if grep -q "^MYSQL_ROOT_PASSWORD=" .env; then
sed -i.tmp 's/^MYSQL_ROOT_PASSWORD=/DB_ROOT_PASSWORD=/' .env
echo -e "${GREEN}✓ Renamed MYSQL_ROOT_PASSWORD → DB_ROOT_PASSWORD${NC}"
fi
if grep -q "^MYSQL_PASSWORD=" .env; then
sed -i.tmp 's/^MYSQL_PASSWORD=/DB_PASSWORD=/' .env
echo -e "${GREEN}✓ Renamed MYSQL_PASSWORD → DB_PASSWORD${NC}"
fi
# Clean up temp files
rm -f .env.tmp
echo ""
echo -e "${GREEN}✓ .env file updated successfully${NC}"
echo ""
echo "Updated variables in .env:"
grep -E "^DB_" .env 2>/dev/null || echo " (none found)"
else
echo -e "${YELLOW}⚠️ Skipped .env update${NC}"
fi
else
echo -e "${BLUE}[DRY RUN] Would update .env file${NC}"
fi
else
echo -e "${YELLOW}⚠️ No .env file found (this is OK if using docker-compose)${NC}"
fi
echo ""
echo -e "${BLUE}================================================${NC}"
echo -e "${GREEN}✓ Migration script completed${NC}"
echo -e "${BLUE}================================================${NC}"
echo ""
echo "Next steps:"
echo "1. Review the changes in docker-compose.yml"
echo "2. Update any deployment scripts or CI/CD pipelines"
echo "3. Restart your services: docker-compose down && docker-compose up -d"
echo ""
echo "Variable mapping reference:"
echo " DB_ROOT_PASSWORD - MySQL root password"
echo " DB_PASSWORD - Application database password"
echo " DB_USER - Application database user (default: voxblog)"
echo " DB_NAME - Application database name (default: voxblog)"
echo " DB_HOST - Database host (default: mysql in docker, localhost otherwise)"
echo " DB_PORT - Database port (default: 3306)"
echo ""