# Data Migration Guide

Use this guide when moving live CRM data into the upgraded application.

## Scope

The release archive contains code and built frontend assets only. It does not include live SQLite database files.

Live data usually includes:

- leads and orders
- sales and finance records
- invoice records and payment links
- reminders and follow-up history
- SMTP profiles
- user accounts and permissions
- site content, plans, and settings
- email logs and audit logs

## Mandatory Backup

Before replacing code or changing schema:

```bash
cp server/db/office365.sqlite server/db/office365.sqlite.before-upgrade-$(date +%Y%m%d-%H%M%S)
```

Also back up:

- `.env` or cPanel environment variables
- uploaded profile/logo assets if stored outside the database
- current application folder

## Safe Migration Order

1. Stop the live Node.js app.
2. Back up the live database and environment values.
3. Upload and extract the validated release archive.
4. Keep the existing live `server/db/office365.sqlite` in place.
5. Keep existing production secrets unchanged.
6. Confirm the server runtime is Node.js 20+ with npm 10+.
7. Run `npm install --omit=dev`.
8. Run `npm run preflight:production` to confirm runtime and built frontend readiness.
9. Start the app once so schema upgrades can run.
10. Restart the app from cPanel or the process manager.
11. Confirm `/api/health` returns `ok: true`, `frontendReady: true`, and `databaseReady: true`.

Production startup and production preflight require `SESSION_SECRET`, `SMTP_ENCRYPTION_KEY`, and `PASSWORD_RESET_SECRET` to be unique non-placeholder values with at least 32 characters.

## Post-Migration Checks

Check these pages with the live admin account:

- Dashboard
- Leads
- Sales
- Invoices
- Reminders
- Reports
- Finance
- Settings
- Plans
- Users
- Site Editor

Confirm:

- Existing leads are visible.
- Existing sales are visible.
- Existing invoice records are visible.
- Existing SMTP profiles still show as configured.
- Report downloads work with correct user permissions.
- New lead submission creates a CRM record.
- New invoice send creates an email log.

## Rollback

If the upgraded app does not behave correctly:

1. Stop the Node.js app.
2. Restore the previous application folder.
3. Restore the database backup if schema/data changed.
4. Restore environment values if they changed.
5. Restart the app.

Do not delete the backup until the upgraded CRM has been verified with live data.
