|
1 |
| -# Drizzle Kit Migration Guide |
| 1 | +# Migration Guide |
2 | 2 |
|
3 |
| -This project now uses Drizzle Kit for database migrations, providing better schema management and migration tracking. |
| 3 | +This guide covers database migrations and version upgrades for Gitea Mirror. |
| 4 | + |
| 5 | +## Version 3.0 Migration Guide |
| 6 | + |
| 7 | +### Overview of v3 Changes |
| 8 | + |
| 9 | +Version 3.0 introduces significant security improvements and authentication changes: |
| 10 | +- **Token Encryption**: All GitHub and Gitea tokens are now encrypted in the database |
| 11 | +- **Better Auth**: Complete authentication system overhaul with session-based auth |
| 12 | +- **SSO/OIDC Support**: Enterprise authentication options |
| 13 | +- **Enhanced Security**: Improved error handling and security practices |
| 14 | + |
| 15 | +### Breaking Changes in v3 |
| 16 | + |
| 17 | +#### 1. Authentication System Overhaul |
| 18 | +- Users now log in with **email** instead of username |
| 19 | +- Session-based authentication replaces JWT tokens |
| 20 | +- New auth endpoints: `/api/auth/[...all]` instead of `/api/auth/login` |
| 21 | +- Password reset may be required for existing users |
| 22 | + |
| 23 | +#### 2. Token Encryption |
| 24 | +- All stored GitHub and Gitea tokens are encrypted using AES-256-GCM |
| 25 | +- Requires encryption secret configuration |
| 26 | +- Existing unencrypted tokens must be migrated |
| 27 | + |
| 28 | +#### 3. Environment Variables |
| 29 | +**Required changes:** |
| 30 | +- `JWT_SECRET` → `BETTER_AUTH_SECRET` (backward compatible) |
| 31 | +- New: `BETTER_AUTH_URL` (required) |
| 32 | +- New: `ENCRYPTION_SECRET` (recommended) |
| 33 | + |
| 34 | +#### 4. Database Schema Updates |
| 35 | +New tables added: |
| 36 | +- `sessions` - User session management |
| 37 | +- `accounts` - Authentication accounts |
| 38 | +- `verification_tokens` - Email verification |
| 39 | +- `oauth_applications` - OAuth app registrations |
| 40 | +- `sso_providers` - SSO configuration |
| 41 | + |
| 42 | +### Migration Steps from v2 to v3 |
| 43 | + |
| 44 | +**⚠️ IMPORTANT: Backup your database before upgrading!** |
| 45 | + |
| 46 | +```bash |
| 47 | +cp data/gitea-mirror.db data/gitea-mirror.db.backup |
| 48 | +``` |
| 49 | + |
| 50 | +#### Automated Migration (Docker Compose) |
| 51 | + |
| 52 | +For Docker Compose users, v3 migration is **fully automated**: |
| 53 | + |
| 54 | +1. **Update your docker-compose.yml** to use v3: |
| 55 | +```yaml |
| 56 | +services: |
| 57 | + gitea-mirror: |
| 58 | + image: ghcr.io/raylabshq/gitea-mirror:v3 |
| 59 | +``` |
| 60 | +
|
| 61 | +2. **Pull and restart the container**: |
| 62 | +```bash |
| 63 | +docker compose pull |
| 64 | +docker compose down |
| 65 | +docker compose up -d |
| 66 | +``` |
| 67 | + |
| 68 | +**That's it!** The container will automatically: |
| 69 | +- ✅ Generate BETTER_AUTH_SECRET (from existing JWT_SECRET if available) |
| 70 | +- ✅ Generate ENCRYPTION_SECRET for token encryption |
| 71 | +- ✅ Create Better Auth database tables |
| 72 | +- ✅ Migrate existing users to Better Auth system |
| 73 | +- ✅ Encrypt all stored GitHub/Gitea tokens |
| 74 | +- ✅ Apply all necessary database migrations |
| 75 | + |
| 76 | +#### Manual Migration (Non-Docker) |
| 77 | + |
| 78 | +#### Step 1: Update Environment Variables |
| 79 | +Add to your `.env` file: |
| 80 | +```bash |
| 81 | +# Set your application URL (required) |
| 82 | +BETTER_AUTH_URL=http://localhost:4321 # or your production URL |
| 83 | + |
| 84 | +# Optional: These will be auto-generated if not provided |
| 85 | +# BETTER_AUTH_SECRET=your-existing-jwt-secret # Will use existing JWT_SECRET |
| 86 | +# ENCRYPTION_SECRET=your-48-character-secret # Will be auto-generated |
| 87 | +``` |
| 88 | + |
| 89 | +#### Step 2: Stop the Application |
| 90 | +```bash |
| 91 | +# Stop your running instance |
| 92 | +pkill -f "bun run start" # or your process manager command |
| 93 | +``` |
| 94 | + |
| 95 | +#### Step 3: Update to v3 |
| 96 | +```bash |
| 97 | +# Pull latest changes |
| 98 | +git pull origin v3 |
| 99 | + |
| 100 | +# Install dependencies |
| 101 | +bun install |
| 102 | +``` |
| 103 | + |
| 104 | +#### Step 4: Run Migrations |
| 105 | +```bash |
| 106 | +# Option 1: Automatic migration on startup |
| 107 | +bun run build |
| 108 | +bun run start # Migrations run automatically |
| 109 | + |
| 110 | +# Option 2: Manual migration |
| 111 | +bun run migrate:better-auth # Migrate users to Better Auth |
| 112 | +bun run migrate:encrypt-tokens # Encrypt stored tokens |
| 113 | +``` |
| 114 | + |
| 115 | +### Post-Migration Tasks |
| 116 | + |
| 117 | +1. **All users must log in again** - Sessions are invalidated |
| 118 | +2. **Users log in with email** - Not username anymore |
| 119 | +3. **Check token encryption** - Verify GitHub/Gitea connections still work |
| 120 | +4. **Update API integrations** - Switch to new auth endpoints |
| 121 | + |
| 122 | +### Troubleshooting v3 Migration |
| 123 | + |
| 124 | +#### Users Can't Log In |
| 125 | +- Ensure they're using email, not username |
| 126 | +- They may need to reset password if migration failed |
| 127 | +- Check Better Auth migration logs |
| 128 | + |
| 129 | +#### Token Decryption Errors |
| 130 | +- Verify ENCRYPTION_SECRET is set correctly |
| 131 | +- Re-run token encryption migration |
| 132 | +- Users may need to re-enter tokens |
| 133 | + |
| 134 | +#### Database Errors |
| 135 | +- Ensure all migrations completed |
| 136 | +- Check disk space for new tables |
| 137 | +- Review migration logs in console |
| 138 | + |
| 139 | +### Rollback Procedure |
| 140 | +If migration fails: |
| 141 | +```bash |
| 142 | +# Stop application |
| 143 | +pkill -f "bun run start" |
| 144 | + |
| 145 | +# Restore database backup |
| 146 | +cp data/gitea-mirror.db.backup data/gitea-mirror.db |
| 147 | + |
| 148 | +# Checkout previous version |
| 149 | +git checkout v2.22.0 |
| 150 | + |
| 151 | +# Restart with old version |
| 152 | +bun run start |
| 153 | +``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## Drizzle Kit Migration Guide |
| 158 | + |
| 159 | +This project uses Drizzle Kit for database migrations, providing better schema management and migration tracking. |
4 | 160 |
|
5 | 161 | ## Overview
|
6 | 162 |
|
|
0 commit comments