124 lines
4.1 KiB
Markdown
124 lines
4.1 KiB
Markdown
# 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
|
||
- Base configuration is restored (pipelines, categories, departments, roles, default users)
|
||
- All business data (contacts, deals, quotes, projects, etc.) is removed so you can enter new real data
|
||
|
||
## ⚠️ 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.)
|
||
- Restored base data: departments, positions, permissions, employees, users, contact categories, product categories, pipelines, one warehouse
|
||
|
||
### 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 (users, departments, positions, permissions, employees, contact/product categories, pipelines, one warehouse).
|
||
|
||
---
|
||
|
||
## Default users after re-seed
|
||
|
||
| Role | Email | Password | Access |
|
||
|-------------------|--------------------------|-----------|---------------|
|
||
| General Manager | gm@atmata.com | Admin@123 | Full system |
|
||
| Sales Manager | sales.manager@atmata.com | Admin@123 | Contacts, CRM |
|
||
| Sales Representative | sales.rep@atmata.com | Admin@123 | Basic CRM |
|
||
|
||
Change these passwords 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 you’re 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.
|