# Using database dumps with Laravel

The project can use an existing database restored from either:

- **LRS43_full_dump_*.sql** – Alfresco + boundary + LRS tables (no `plg_user_credentials` in dump).
- **production_dump_*.sql** – Same schema as LRS43 plus `plg_user_credentials` with bcrypt passwords (e.g. `production_dump_20260212_122352.sql`).

Migrations are written to **skip creating tables that already exist**, so you can restore a dump and then run Laravel migrations without conflicts. There is **no separate migration or seed that recreates the full dump schema**; the dump file is the source of truth.

---

## 1. Restore the dump

From the **repo root** (paths below are relative to repo root). Use the same database name as in Laravel’s `.env`, or restore into a DB and set that name in `.env`:

```bash
# LRS43-style or production dump (database name LRS43 in the dump)
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS LRS43 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p LRS43 < database_dump/LRS43_full_dump_20260209_013445.sql
# or
mysql -u root -p LRS43 < database_dump/production_dump_20260212_122352.sql
```

Or restore into a different database and point Laravel at it:

```bash
mysql -u root -p -e "CREATE DATABASE boundary_laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -u root -p boundary_laravel < database_dump/production_dump_20260212_122352.sql
```

## 2. Configure Laravel

In `boundary-laravel/.env`:

```env
DB_DATABASE=LRS43
DB_USERNAME=root
DB_PASSWORD=yourpassword
```

(Use the database name you restored into.)

## 3. Run migrations

Migrations **only create tables that don’t exist** (e.g. `refresh_tokens`, `landing_*`). Tables that already exist in the dump (e.g. `alf_authority`, `boundary`, `plg_user_credentials` on production dump) are skipped.

```bash
cd boundary-laravel
php artisan migrate
```

## 4. Login credentials

| Dump type        | `plg_user_credentials` in dump? | What to do |
|------------------|----------------------------------|------------|
| **LRS43_full_*** | No                               | Run `php artisan db:seed` so Laravel creates rows for users in `alf_authority`. Default password: **`password`** (set `SEED_ADMIN_PASSWORD` in `.env` to override). |
| **production_*** | Yes (bcrypt hashes)              | You can log in with the passwords that were in production. Optional: run `php artisan db:seed` to set a single known password for all users (overwrites existing hashes). |

```bash
# Only needed for LRS43 dumps (no credentials in dump); optional for production dump
php artisan db:seed
```

Seeder: `Database\Seeders\PlgUserCredentialsSeeder` – inserts/updates `plg_user_credentials` for every non-role user in `alf_authority` using `Hash::make(SEED_ADMIN_PASSWORD ?? 'password')`.

## Summary

| Step              | Action |
|-------------------|--------|
| Restore dump      | `mysql ... < database_dump/LRS43_full_dump_*.sql` or `database_dump/production_dump_*.sql` |
| Set `.env`        | `DB_DATABASE` = database you restored into |
| Run migrations    | `php artisan migrate` (skips existing tables) |
| Seed (if needed)  | `php artisan db:seed` for LRS43 dumps; optional for production dump |

---

## Production dump as migration and seeder

The **production_dump_20260212_122352.sql** is wired into Laravel so you can run it via migrate or seed.

### Migration (first-time load)

Migration **`2024_01_01_000000_import_production_dump`** runs first and loads the production dump when the database has no `alf_authority` table (fresh DB). It:

- Looks for the dump file in:
  - `boundary-laravel/database/sql/production_dump_20260212_122352.sql`, or
  - repo root `database_dump/production_dump_20260212_122352.sql`
- Strips `CREATE DATABASE`, `DROP DATABASE`, and `USE` from the dump and executes the rest (schema + data).

So on a **fresh** database you can:

1. Set `.env` (e.g. `DB_DATABASE=LRS43`).
2. Run `php artisan migrate` – the first migration (000000) runs the production dump if `alf_authority` is missing; then 000001–000003 create Laravel auth/landing/boundary tables if missing (e.g. `refresh_tokens`, `landing_*`).

No need to restore the dump manually in that case.

### Seeder (re-load dump)

**`ProductionDumpSeeder`** runs the same dump file. Use it when you want to **reset** the database to the production dump state (drops and recreates tables in the dump, then re-inserts data):

```bash
php artisan db:seed --class=ProductionDumpSeeder
```

Dump file location is the same as for the migration. Default `php artisan db:seed` does **not** call this seeder (it only runs `PlgUserCredentialsSeeder`).

---

Migration order: **000000** imports the production dump on a fresh DB; **000001** adds auth tables if missing (`plg_user_credentials`, `refresh_tokens`); **000002** adds landing tables; **000003** adds boundary tables if missing. The dump is the source of truth for alf_*, avm_*, boundary, lr_*, JBPM_*, etc.
