This document explains how to use the database synchronization and migration scripts for the CAMC Backend.
The project includes several scripts to manage database synchronization:
- sync-database.ts - Basic database synchronization
- migrate-database.ts - Advanced migration with backup support
- backup-database.ts - Database backup utility
- restore-database.ts - Database restore from backup files
# Preview what will be done
npm run db:migrate:dry
# Create backup and sync database
npm run db:migrate:safe# Simple sync (creates tables if they don't exist)
npm run db:sync
# Update existing schema
npm run db:sync:alter
# Create database backup
npm run db:backupBasic Sequelize sync operations:
npm run db:sync # Safe sync (create missing tables)
npm run db:sync:alter # Update existing schema
npm run db:sync:force # ⚠️ DROP and recreate all tablesAdvanced migration with additional safety features:
npm run db:migrate:dry # Preview changes (no actual changes)
npm run db:migrate:safe # Migration with automatic backup
npm run db:migrate # Basic migration
# Manual options:
ts-node src/migrate-database.ts --backup --alter # Update schema with backup
ts-node src/migrate-database.ts --force --backup # Full reset with backupCreates a complete PostgreSQL dump:
npm run db:backupBackups are saved to ../backups/backup-{DB_NAME}-{timestamp}.sql (outside the code directory)
Restores database from backup files:
npm run db:restore:list # List available backups
npm run db:restore:latest # Restore from latest backup
npm run db:restore # Interactive restore (requires backup filename)
# Manual options:
ts-node src/restore-database.ts --file ../backups/backup.sql # Restore specific file
ts-node src/restore-database.ts backup-tpc-2024-01-01.sql # Restore by filename
ts-node src/restore-database.ts --latest --no-backup # Skip safety backupEnsure these environment variables are configured:
DB_NAME=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432- Always backup first:
npm run db:backup - Test with dry run:
npm run db:migrate:dry - Use safe migration:
npm run db:migrate:safe
- Preview changes:
npm run db:migrate:dry - Schema updates:
npm run db:sync:alter - Fresh start:
npm run db:sync:force(⚠️ destroys data)
# Recommended CI/CD pipeline step
npm run db:migrate:safe| Command | Description | Safety Level |
|---|---|---|
db:migrate:dry |
Preview changes only | ✅ Very Safe |
db:backup |
Create backup | ✅ Safe |
db:restore:list |
List available backups | ✅ Safe |
db:sync |
Create missing tables | ✅ Safe |
db:migrate:safe |
Migration with backup | ✅ Safe |
db:sync:alter |
Update schema | |
db:migrate |
Basic migration | |
db:restore:latest |
Restore from latest backup | ❌ Destructive |
db:restore |
Restore from backup | ❌ Destructive |
db:sync:force |
Drop and recreate | ❌ Destructive |
- Verify database is running
- Check environment variables
- Ensure user has proper permissions
- Ensure
pg_dumpis installed and in PATH - Check PostgreSQL user permissions
- Verify disk space for backups
- Check the error logs
- Restore from backup:
npm run db:restore:latestornpm run db:restore backup-file.sql - Review model changes for conflicts
- Ensure backup file exists and is not corrupted
- Check PostgreSQL user has CREATE DATABASE permissions
- Verify backup file was created with compatible PostgreSQL version
- Use
--no-backupflag to skip safety backup if needed
When you modify Sequelize models:
-
Development:
npm run db:migrate:dry # See what will change npm run db:sync:alter # Apply changes
-
Staging/Production:
npm run db:backup # Create backup npm run db:migrate:dry # Preview changes npm run db:migrate:safe # Apply with backup
Complete backup and restore cycle:
# 1. List available backups
npm run db:restore:list
# 2. Create current backup before changes
npm run db:backup
# 3. If something goes wrong, restore from backup
npm run db:restore:latest # Restore from most recent
# or
npm run db:restore backup-file.sql # Restore specific backupThe sync scripts work alongside your existing upload:data script:
# Typical workflow
npm run db:migrate:safe # Sync database schema
npm run upload:data file.xlsx 2024 "Computer Science" 1 # Upload data