# Upgrade Notes: Legacy MT5 Structure to MT4 Bridge

## Compatibility strategy

The previous codebase used MT5 terminology throughout class names, route names, permission identifiers, columns, and tables. Renaming all persisted identifiers would make upgrades unnecessarily destructive. This release therefore separates **runtime meaning** from **legacy storage identifiers**:

- `Mt4Service` is the canonical provider integration.
- `Mt5Service` is a compatibility alias that extends `Mt4Service`.
- `ClientMt4Account`, `ClientMt4Transaction`, and `Mt4Group` are canonical aliases.
- Existing tables such as `client_mt5_accounts` and fields such as `mt5_login` remain in place.
- Existing permission names such as `mt5_groups_access` remain valid so assigned roles are not broken.
- Administrator-facing permission labels and website/client wording are relabeled to MT4.

## Database changes

The MT4 upgrade migration adds:

- `provider`
- `account_label`
- `currency`
- `investor_password_enc`
- `is_primary`
- `enabled`
- `read_only`
- `last_synced_at`
- `last_sync_error`

It also adds `client_mt5_account_id` to credit and withdrawal requests, creates account-oriented indexes, backfills one primary account per client, and removes the historical single-account constraint when present.


## Installer modes during upgrade

- **Existing database** keeps all business data, runs pending migrations, and runs only safe, repeatable metadata seeders.
- **Fresh database** performs `migrate:fresh` only after an explicit destructive confirmation and is not appropriate for an in-place production upgrade.
- Normal installation no longer creates sample administrators, sample leads, clients, or trading records.
- A process lock prevents two installer requests from running migrations simultaneously.

## Upgrade procedure

1. Back up the application files and database.
2. Deploy the complete project, not selected individual files.
3. Add the MT4 variables from `.env.example` to the production `.env`.
4. Confirm the provider credentials against a demo MT4 account.
5. Run:

```bash
php artisan down
php artisan optimize:clear
php artisan migrate --force
php artisan db:seed --force
php artisan storage:link
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan up
```

6. Open an existing client and verify that the first historical account is marked primary.
7. Create a second demo account and verify account switching, stats, trades, and account-scoped requests.
8. Approve only low-value demo credit/withdrawal requests before enabling production processing.

## Rollback warning

The upgrade is intentionally designed to preserve old records and identifiers. Nevertheless, rolling back after new multi-account activity can lose account metadata and request-account references. Restore the pre-upgrade database backup rather than relying on a migration rollback for a production reversal.

## 2026-08-12 final update

The final update also adds the `contact_messages` table and four Contact Messages permissions. After deploying to an existing installation, run the pending migrations and reset the permission cache:

```bash
php artisan migrate --force
php artisan permission:cache-reset
php artisan optimize:clear
```

Configure Pusher only when realtime website-message popups are required. The five public websites use the exact supplied template asset trees under `public/themes/supplied/`; their Blade pages retain the supplied layout and load administrator branding through runtime hooks. The previously generated replacement-layout directory is not part of the release.

MT4 account creation now requires the provider to return a recognizable remote login before a local account record is created. The bridge fields are intentionally separated as `uname`, `lname`, `pwd`, and `trading_account_pass`; API authentication remains `name` and `pass` and is injected by the server-side service.
