# Staging to production workflow

The staging domain uses the shared application code under development and a
dedicated tenant database. Production tenants continue using the published
production code and their own databases.

## One-time platform setup

Run `database/platform/20260923_release_migration_core.sql` on
`webdemoo_simple_saas_platform`. Then run the idempotent staging configuration:

```text
database/platform/20260923_configure_staging_tenant.sql
```

It maps `staging.webdemo258.io.vn` to the existing development tenant behind
`se.webdemo258.io.vn` and marks that tenant as `staging`. All other existing
tenants remain `production`. DNS and SSL stay pending until separately verified.

## Prepare a release

1. Choose an immutable version, for example `2026.09.24.1`.
2. Create `database/tenant-migrations/2026.09.24.1/`.
3. Add ordered, retry-safe SQL files. Leave the directory empty for a code-only release.
4. Use a stable code reference such as a Git commit hash or release archive checksum.

## Validate staging

```bash
php bin/tenant-migrate-all.php \
  --release=2026.09.24.1 \
  --environment=staging \
  --dry-run

php bin/tenant-migrate-all.php \
  --release=2026.09.24.1 \
  --environment=staging \
  --execute \
  --code-ref=YOUR_TESTED_CODE_REF
```

Test login, CRM, classes, attendance, tuition, salaries, finance and permissions
on staging. Production remains blocked until every active staging tenant passes.

## Migrate production

Create and verify backups first. Then inspect the exact plan and execute it:

```bash
php bin/tenant-migrate-all.php \
  --release=2026.09.24.1 \
  --environment=production \
  --dry-run

php bin/tenant-migrate-all.php \
  --release=2026.09.24.1 \
  --environment=production \
  --execute \
  --backup-confirmed \
  --code-ref=YOUR_TESTED_CODE_REF
```

The production command is blocked without a successful staging release and the
explicit backup flag. It uses a MySQL advisory lock so two rollout commands
cannot run concurrently. The first failure stops the batch and is recorded.

After database rollout succeeds, publish the exact tested code to the shared
production source. Then mark it live:

```bash
php bin/release-mark-live.php \
  --release=2026.09.24.1 \
  --code-ref=YOUR_TESTED_CODE_REF
```

Database migrations are forward-only. Restore from the verified backup or add
a corrective migration if a release must be repaired.
