# Backend scripts

One-off data loaders used to build the database from the plant's Excel workbooks. The
live application never calls them. Run them from the `backend` folder with the virtual
environment active. They use the database in `backend/.env`.

```bash
cd backend
source venv/bin/activate          # Windows: venv\Scripts\activate
python scripts/<script>.py --help # where the script takes options
```

Scripts that load a workbook take its path with `--file`, or read it from the
location named in the script. Workbooks are not kept in the repository.

## Seed scripts (reference data)

| Script | Loads |
|---|---|
| `seed_data.py` | Roles, modules, permissions and demo users. Needs `SEED_DEFAULT_PASSWORD` set in the shell; it becomes every demo user's password. |
| `seed_client_admin.py` | The role-management module and the client admin role |
| `seed_operator_roles.py` | Operator and QA roles for the shop floor |
| `seed_locations_and_stations.py` | Factory locations and production stations |
| `seed_types.py`, `seed_dealers.py`, `seed_transporters.py` | Customer types, dealers, transporters |
| `seed_production.py`, `seed_production_module.py` | Production masters and sample chassis |
| `seed_procurement_sales.py` | Procurement and sales reference data |
| `seed_qa_checksheets.py` | QA checksheet templates from the in-process checksheet workbook (`--file`) |
| `seed_maintenance.py` | Maintenance assets and checklists |
| `seed_feature_segments.py`, `seed_special_features_from_csv.py`, `seed_special_features_from_excel.py` | Special features, options and the segments they apply to |
| `seed_seat_model_configs.py`, `seed_seating_layouts.py` | Seat model rules and seating layouts |
| `seed_excel_mappings.py` | Raw material to store and vendor mappings |
| `seed_vendor_passwords.py` | Enables vendor portal access. Needs `VENDOR_PORTAL_INITIAL_PASSWORD` set in the shell. |

Example, macOS / Linux:

```bash
SEED_DEFAULT_PASSWORD='choose-a-password' python scripts/seed_data.py
```

Windows PowerShell:

```powershell
$env:SEED_DEFAULT_PASSWORD = 'choose-a-password'; python scripts\seed_data.py
```

## Ingest scripts (bulk data from Excel)

| Script | Loads |
|---|---|
| `ingest_rm_master.py` | Raw material master (`--file`, `--dry-run`) |
| `ingest_vendors.py`, `ingest_stations.py`, `ingest_inventory.py` | Vendors, stations, opening stock |
| `ingest_bom_matrix.py` | Base model BOMs |
| `ingest_incoming_materials_v2.py`, `ingest_monthly_stock.py`, `ingest_machine_cost.py`, `ingest_pm_schedule.py`, `ingest_history_cards.py`, `ingest_all_maintenance.py` | Maintenance registers |

## Maintenance utilities

| Script | Purpose |
|---|---|
| `apply_table_comments.py` | Writes the table descriptions in `app/models/table_catalog.py` into the database, so pgAdmin shows them. Run after adding a table. |
