# SQL Express MySQL Sync

One-way sync coordinator: **Microsoft Access → SQL Server Express → MySQL** (`shop_products`).

The engine mirrors an Access database into SQL Server Express, then publishes an active product catalogue to MySQL for the web shop. Each leg can be run independently or together.

## Quick start

1. **Install prerequisites** — Windows, .NET Framework 4.8, [ACE OLEDB 12.0](https://www.microsoft.com/en-us/download/details.aspx?id=54920) (x64), SQL Server Express, MySQL 5.7+/8.x. See [docs/getting-started.md](docs/getting-started.md).
2. **Configure** — Copy [`config/.env.example`](config/.env.example) to **`.env` in the repo root** and fill in connection settings.
3. **Build** — `dotnet build SyncEngine.sln -c Release`
4. **Provision schema** — `SyncEngine\bin\Release\net48\SyncEngine.exe schema-only`
5. **Run full sync** — `SyncEngine\bin\Release\net48\SyncEngine.exe all`

Each build copies `.env` and `config\` into the output folder automatically. Do not maintain a separate `.env` by hand in `bin\`.

## CLI commands

| Command | Description |
|---------|-------------|
| `all` | Access → SQL Express, then SQL Express → MySQL (default) |
| `access` | Access → SQL Express only |
| `mysql` | SQL Express → MySQL (`shop_products`) only |
| `schema-only` | Provision SQL Express schema (tables, keys, defaults, FKs, CHECKs, views) — no data sync |

```bat
SyncEngine.exe all
SyncEngine.exe access
SyncEngine.exe mysql
SyncEngine.exe schema-only
```

Legs respect feature switches in `.env` (`SYNC_ACCESS_TO_SQL_EXPRESS`, `SYNC_SQL_EXPRESS_TO_MYSQL`).

## Documentation

| Guide | Description |
|-------|-------------|
| [getting-started.md](docs/getting-started.md) | Prerequisites, setup, first run, deployment, troubleshooting |
| [build-and-test.md](docs/build-and-test.md) | Build commands, output artifacts, unit tests |
| [configuration.md](docs/configuration.md) | `.env` and config file reference |
| [sync-architecture.md](docs/sync-architecture.md) | How the sync works — flows and diagrams |
| [access-sql-express-compatibility.md](docs/access-sql-express-compatibility.md) | Access → SQL Express schema mirroring |
| [shop_products.md](docs/shop_products.md) | MySQL target schema and field mapping |

## Logging

- Console output
- Rolling files in `logs\sync-YYYYMMDD.log`
- SQL Express run history in `sync_metadata.runs`
- MySQL run history in `shop_sync_log`

## Exit codes

| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | Runtime/sync failure (database connection, sync error) |
| 2 | Configuration error (missing or invalid `.env`) |
| 3 | Another `SyncEngine.exe` is already running on this machine |

## Tests

```bat
dotnet test SyncEngine.sln -c Release
```

## Deployment

Ship folder: `SyncEngine.exe`, `.env`, `config\`, `App.config`, `logs\`. Schedule with **Task Scheduler**.

Only one sync process may run at a time per machine (global mutex). Overlapping Task Scheduler triggers exit with code **3** without starting a second sync. See [docs/sync-architecture.md](docs/sync-architecture.md#single-instance).

**ACE bitness:** Default build is `x64`. For 32-bit ACE only, set `<PlatformTarget>x86</PlatformTarget>` in the `.csproj` files. See [docs/build-and-test.md](docs/build-and-test.md).
