# Migration Checklist

Use this checklist to ensure a smooth database migration.

## Pre-Migration

- [ ] Backup old database
- [ ] Backup new database (even if empty)
- [ ] Verify you can connect to both databases
- [ ] Ensure new database schema is created (all migrations run)
- [ ] Test connection strings
- [ ] Review MIGRATION_GUIDE.md

## Prepare SQL Script

- [ ] Open `migration_old_to_new.sql`
- [ ] Find and replace all instances of `old_database` with your actual old database name
- [ ] Verify you're using the correct database name (case-sensitive on some systems)
- [ ] Review table mappings for tables with major changes:
  - [ ] users table (new fields)
  - [ ] custom_plans table (complete restructure - manual review needed)
  - [ ] wallets table (structure changes)
  - [ ] investment_plans table (new fields)
  - [ ] investments table (data type changes)

## Data Type Conversions

Verify these conversions will work:
- [ ] investments.amount_invested: varchar → decimal(20,4)
- [ ] withdrawals.amount: varchar → decimal(20,4)
- [ ] wallets.amount: varchar → decimal(20,4)
- [ ] depay_transactions.local_amount: varchar → decimal(20,4)

Check if any source values might cause conversion errors.

## Run Migration

- [ ] Connect to NEW database
- [ ] Run `SET FOREIGN_KEY_CHECKS = 0;` (included in script)
- [ ] Execute migration script
- [ ] Monitor for errors
- [ ] Note any warnings

## Post-Migration Verification

### Record Counts
- [ ] Users count matches
- [ ] Investments count matches
- [ ] Wallets count matches
- [ ] Fundings count matches
- [ ] Withdrawals count matches
- [ ] Referrals count matches

### Data Integrity
- [ ] No orphaned investments (all have valid user_id and investment_plan_id)
- [ ] No orphaned referrals (all have valid inviter_id and invited_id)
- [ ] All users have valid email addresses
- [ ] All wallet amounts are valid decimals (not NULL or 0 where unexpected)
- [ ] All investment amounts are valid decimals

### Relationships
- [ ] Users → Investments (foreign key relationships intact)
- [ ] Users → Wallets (relationships intact)
- [ ] Investments → Investment Plans (relationships intact)
- [ ] Referrals → Users (both inviter and invited)

### Sample Data Checks
- [ ] Check a few sample users and their data
- [ ] Verify investment calculations
- [ ] Verify wallet balances
- [ ] Check referral relationships

## Special Handling Required

### Tables Needing Manual Review

- [ ] **custom_plans**: Structure completely different. Decide if data should be migrated or left empty.
- [ ] **job_seekers**: Exists in old DB but not in new DB structure. Decide how to handle.
- [ ] **k_y_c_verifications**: Exists in old DB but not in new DB structure. Decide how to handle.

### New Features (No Migration Needed)

These tables are new and will be populated through application usage:
- [ ] applications
- [ ] billings
- [ ] copiers
- [ ] forex_markets
- [ ] funding_chats / funding_messages
- [ ] gas_fee_payments
- [ ] investor_profiles / investor_subscriptions
- [ ] jobs / job_applications
- [ ] live_chats
- [ ] p2p_* tables
- [ ] payment_proofs
- [ ] traders / trades / tradings
- [ ] withdrawal_requirements / withdrawal_verifications

## Application Testing

After migration, test these user flows:
- [ ] User login
- [ ] User registration (new users)
- [ ] View investment history
- [ ] Create new investment
- [ ] View wallet balance
- [ ] Fund wallet
- [ ] Request withdrawal
- [ ] View referral earnings
- [ ] Joint account operations (if applicable)

## Rollback Plan

If issues are found:
- [ ] Restore new database from backup
- [ ] Document what went wrong
- [ ] Fix migration script
- [ ] Re-run migration

## Post-Migration Tasks

- [ ] Re-enable foreign key checks
- [ ] Create necessary indexes (if not already created)
- [ ] Update application configuration if needed
- [ ] Monitor application logs for errors
- [ ] Inform users of any maintenance/downtime
- [ ] Document any data anomalies found
- [ ] Update any data that needed manual correction

## Sign-off

- [ ] Migration completed successfully
- [ ] All verification checks passed
- [ ] Application tested and working
- [ ] Documentation updated
- [ ] Team notified

**Migration Date**: ___________

**Performed By**: ___________

**Verified By**: ___________

**Notes**:
_______________________________________________________
_______________________________________________________
_______________________________________________________

