# Rollback: ERP bilang main source of inventory

Runbook para i-off ang Shopify source-of-truth (SoT) at ayusin ang data ayon sa pinili mong layunin. **Huwag i-edit ang plan file** — ito ang executable na dokumento.

## 1. Tukuyin muna ang layunin sa “previous data” (A / B / C)

| Pinili | Kahulugan | Susunod na hakbang |
|--------|-----------|---------------------|
| **A** | Eksaktong ERP tulad **bago** mag-SoT | Supabase **PITR / backup / pg_dump restore** — walang substitute sa app. Kumuha ng backup na may petsa bago ang cutover. |
| **B** | ERP = **kasalukuyang Shopify levels** (mirror) | Pagkatapos i-off ang SoT, tingnan ang preview SQL: [`supabase/sql/rollback_mirror_to_erp_inventory_preview.sql`](../supabase/sql/rollback_mirror_to_erp_inventory_preview.sql). I-run ang `UPDATE` doon sa maintenance window pagkatapos i-review ang preview. |
| **C** | Manu-manong wasto ang ERP, tapos Shopify susunod | I-off ang SoT → ayusin `inventory` (count / adjustments) → `npm run shopify:inventory-push:full:live` pagkatapos QA. |

Kung hindi sigurado: gawin muna ang **export snapshot** (Section 2) bago anumang `UPDATE`.

---

## 2. I-export ang snapshot (fallback bago magbago)

```bash
npm run shopify:rollback:export-snapshot
```

Output: `rollback-snapshots/<timestamp>/` na may JSON para sa `inventory`, `shopify_inventory_mirror`, queues, at buod ng `inventory_out_requests`.

I-archive ang folder sa secure storage.

---

## 3. I-off ang SoT flag at i-verify

**Dry-run (walang babaguhin):**

```bash
npm run shopify:rollback:disable-sot:dry-run
```

**Live (i-set `shopify_is_source_of_truth` → false):**

```bash
npm run shopify:rollback:disable-sot:live
```

Pagkatapos:

```bash
npm run shopify:verify-inventory-out-ops
```

Dapat `shopify_inventory_sot_enabled = false`. Suriin na may `supabase_project_url` at `inventory_push_secret` para sa ERP→Shopify queue.

---

## 4. I-audit ang queues at inventory-out requests

```bash
npm run shopify:rollback:audit-queues
```

Tingnan ang:

- `inventory_out_requests`: `pending_shopify`, `apply_failed` — tapusin manually sa Warehouse UI o ayon sa policy.
- `shopify_inventory_adjustment_queue`: `pending` / `processing` — i-drain o i-retry pagkatapos mag-stabilize ang Edge worker.

---

## 5. Reconcile o restore (ayon sa A / B / C)

- **A:** I-restore mula backup (Supabase Dashboard o `pg_restore`) — lampas sa scope ng mga script dito.
- **B:** Buksan ang [`rollback_mirror_to_erp_inventory_preview.sql`](../supabase/sql/rollback_mirror_to_erp_inventory_preview.sql): i-run ang **preview** (`SELECT`), pagkatapos ang **UPDATE** kung tama na ang diff.
- **C:** Walang mirror copy; direktang ayusin ang ERP counts, tapos full push (Section 6).

---

## 6. Full push at smoke test (pag ERP na ang truth)

Pagkatapos maging tama ang `public.inventory`:

```bash
npm run shopify:inventory-push:full:dry-run
npm run shopify:inventory-push:full:live
```

Buod ng checklist: [SMOKE_TEST_AFTER_ROLLBACK.md](./SMOKE_TEST_AFTER_ROLLBACK.md).

**Mabilis:**

1. Sa ERP app: branch stock / inventory screen — random SKU, tumugma sa inaasahan.
2. Sa Shopify Admin: parehong SKU sa mapped location — malapit sa ERP pagkatapos ng push (may propagation delay).
3. Subukan ang isang maliit na inventory movement kung applicable; tingnan na may bagong row sa `inventory_sync_queue` kung naka-enable ang sync.

---

## Mga script / file (reference)

| Artifact | Purpose |
|----------|---------|
| [`scripts/shopify/export-inventory-rollback-snapshot.js`](../scripts/shopify/export-inventory-rollback-snapshot.js) | Snapshot export |
| [`scripts/shopify/disable-shopify-sot.js`](../scripts/shopify/disable-shopify-sot.js) | Off flag + verify |
| [`scripts/shopify/audit-inventory-rollback-queues.js`](../scripts/shopify/audit-inventory-rollback-queues.js) | Queue / request audit |
| [`supabase/sql/rollback_mirror_to_erp_inventory_preview.sql`](../supabase/sql/rollback_mirror_to_erp_inventory_preview.sql) | Path B: mirror → ERP |
| [`docs/SMOKE_TEST_AFTER_ROLLBACK.md`](./SMOKE_TEST_AFTER_ROLLBACK.md) | Checklist pagkatapos ng full push |

**Env:** `SUPABASE_URL`, `SUPABASE_SERVICE_ROLE_KEY` (o `SERVICE_ROLE_KEY`), tulad ng ibang Shopify scripts.
