Files
zerp/docs/PRODUCTION_DATABASE_CLEANUP.md

122 lines
3.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Production Database Cleanup Task
## Purpose
Clean the production database so you can load **new real data** that will reflect across the system at all levels. This removes existing (e.g. test/demo) data and leaves the database in a state where:
- Schema and migrations are unchanged
- One System Administrator user remains for configuration
- All business data (contacts, deals, quotes, projects, etc.) is removed so you can enter new real data manually
## ⚠️ Important
- **Back up the production database** before running any cleanup.
- Run this **only on the production server** (or a copy of it) when you are ready to wipe current data.
- After cleanup, the seed will recreate **default login users** (see credentials at the end of this doc). Change their passwords after first login if needed.
---
## Option 1: Clean + Re-seed (Recommended)
This truncates all tables and then runs the seed so you get:
- Empty business data (contacts, deals, quotes, projects, inventory, etc.)
- One System Administrator user (admin@system.local) with full access to all modules
### Steps on production server
**Option A One script (backup + clean + seed)**
From the repo root on the production server:
```bash
bash backend/scripts/run-production-clean-and-seed.sh
```
This will prompt for `YES`, create a backup under `backups/`, then run clean-and-seed. Restart the app after.
**Option B Manual steps**
1. **Back up the database**
```bash
pg_dump $DATABASE_URL > backup_before_cleanup_$(date +%Y%m%d_%H%M%S).sql
```
2. **Go to backend and run the cleanup script**
```bash
cd backend
npm run db:clean-and-seed
```
3. **Restart the application** so it uses the cleaned DB.
4. Log in with one of the default users and start entering real data (contacts, deals, etc.).
---
## Option 2: Full reset (drop and recreate database)
Use only if you want to **drop the entire database** and recreate it from migrations (e.g. to fix schema drift).
1. **Back up the database** (see above).
2. **Reset and seed**
```bash
cd backend
npx prisma migrate reset --force
```
This will:
- Drop the database
- Recreate it from migrations
- Run the seed
3. **Restart the application.**
---
## What gets removed (Option 1)
All rows are removed from every table, including:
- Contacts, contact relationships, contact categories links
- Deals, quotes, cost sheets, contracts, invoices
- Projects, tasks, project members, expenses
- Inventory, products, warehouses, movements, assets
- Campaigns, activities, notes, attachments
- HR data: attendances, leaves, salaries, evaluations, etc.
- Audit logs, notifications, approvals
- Users, employees, departments, positions, permissions
Then the **seed** recreates only the base data (one System Administrator user with full access). No categories, pipelines, or warehouses—you configure these manually.
---
## Default user after re-seed
| Role | Email | Password | Access |
|-------------------|----------------------|-----------|-------------|
| System Administrator | admin@system.local | Admin@123 | Full system |
Change the password after first login in production.
---
## If you use Docker
Run the cleanup inside the backend container or against the same `DATABASE_URL` your app uses:
```bash
# Example: run inside backend container
docker compose exec backend npm run db:clean-and-seed
```
Or run the script from your host with `DATABASE_URL` set to the production DB connection string.
---
## Troubleshooting
- **"Cannot truncate because of foreign key"** The script uses `CASCADE`; if you see this, ensure youre on PostgreSQL and using the provided script.
- **Seed fails (e.g. duplicate key)** Ensure the cleanup step completed; run the script again (cleanup is idempotent, then seed runs once).
- **App still shows old data** Restart the backend/API and clear browser cache; ensure `DATABASE_URL` points to the database you cleaned.