|
| 1 | +# Vercel Deployment Fix - PostgreSQL Schema Sync |
| 2 | + |
| 3 | +## Issue |
| 4 | +Vercel deployment failed with the following error: |
| 5 | +``` |
| 6 | +Type error: Module '"@prisma/client"' has no exported member 'AuditLog'. |
| 7 | +``` |
| 8 | + |
| 9 | +## Root Cause Analysis |
| 10 | + |
| 11 | +The codebase maintains two Prisma schema files: |
| 12 | +1. `prisma/schema.sqlite.prisma` - For local development (SQLite) |
| 13 | +2. `prisma/schema.postgres.prisma` - For production deployments (PostgreSQL) |
| 14 | + |
| 15 | +The PostgreSQL schema was **out of sync** with the SQLite schema. Specifically, it was missing: |
| 16 | +- `AuditLog` model (26 lines) |
| 17 | +- `InventoryLog` model (21 lines) |
| 18 | +- Related relations in User, Store, and Product models |
| 19 | + |
| 20 | +When Vercel built the application using the PostgreSQL schema, the Prisma client generated didn't include the `AuditLog` and `InventoryLog` types, causing the TypeScript compilation to fail. |
| 21 | + |
| 22 | +## Files Affected |
| 23 | + |
| 24 | +### Service Files Using AuditLog |
| 25 | +- `src/lib/services/audit-log.service.ts` - Imports `AuditLog` type from `@prisma/client` |
| 26 | + |
| 27 | +### Service Files Using InventoryLog |
| 28 | +- `src/lib/services/inventory.service.ts` - Uses `InventoryLog` for tracking inventory changes |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +### 1. Added InventoryLog Model |
| 33 | +```prisma |
| 34 | +model InventoryLog { |
| 35 | + id String @id @default(cuid()) |
| 36 | + storeId String |
| 37 | + productId String |
| 38 | + product Product @relation("InventoryLogs", fields: [productId], references: [id], onDelete: Cascade) |
| 39 | + |
| 40 | + previousQty Int |
| 41 | + newQty Int |
| 42 | + changeQty Int |
| 43 | + reason String |
| 44 | + note String? |
| 45 | + |
| 46 | + userId String? |
| 47 | + user User? @relation("InventoryUserLogs", fields: [userId], references: [id], onDelete: SetNull) |
| 48 | + |
| 49 | + createdAt DateTime @default(now()) |
| 50 | + |
| 51 | + @@index([storeId, productId, createdAt]) |
| 52 | + @@index([productId, createdAt]) |
| 53 | + @@index([userId, createdAt]) |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### 2. Added AuditLog Model |
| 58 | +```prisma |
| 59 | +model AuditLog { |
| 60 | + id String @id @default(cuid()) |
| 61 | + storeId String? |
| 62 | + store Store? @relation(fields: [storeId], references: [id], onDelete: Cascade) |
| 63 | +
|
| 64 | + userId String? |
| 65 | + user User? @relation("AuditUserLogs", fields: [userId], references: [id], onDelete: SetNull) |
| 66 | +
|
| 67 | + action String // e.g., "CREATE", "UPDATE", "DELETE" |
| 68 | + entityType String // e.g., "Product", "Order", "User" |
| 69 | + entityId String |
| 70 | + |
| 71 | + changes String? // JSON { "field": { "old": "value", "new": "value" } } |
| 72 | + |
| 73 | + ipAddress String? |
| 74 | + userAgent String? |
| 75 | +
|
| 76 | + createdAt DateTime @default(now()) |
| 77 | +
|
| 78 | + @@index([storeId, createdAt]) |
| 79 | + @@index([userId, createdAt]) |
| 80 | + @@index([entityType, entityId, createdAt]) |
| 81 | + @@map("audit_logs") |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. Added Missing Relations |
| 86 | + |
| 87 | +**User Model:** |
| 88 | +```prisma |
| 89 | +inventoryLogs InventoryLog[] @relation("InventoryUserLogs") |
| 90 | +auditLogs AuditLog[] @relation("AuditUserLogs") |
| 91 | +``` |
| 92 | + |
| 93 | +**Store Model:** |
| 94 | +```prisma |
| 95 | +auditLogs AuditLog[] |
| 96 | +``` |
| 97 | + |
| 98 | +**Product Model:** |
| 99 | +```prisma |
| 100 | +inventoryLogs InventoryLog[] @relation("InventoryLogs") |
| 101 | +``` |
| 102 | + |
| 103 | +## Verification |
| 104 | + |
| 105 | +### Before Fix: |
| 106 | +- PostgreSQL schema: **19 models** |
| 107 | +- SQLite schema: **21 models** |
| 108 | +- ❌ Schemas out of sync |
| 109 | + |
| 110 | +### After Fix: |
| 111 | +- PostgreSQL schema: **21 models** ✅ |
| 112 | +- SQLite schema: **21 models** ✅ |
| 113 | +- ✅ Schemas in sync |
| 114 | + |
| 115 | +## Deployment Notes |
| 116 | + |
| 117 | +1. **Automatic Migration:** When Vercel deploys, it will automatically run `prisma migrate deploy` which will create the new tables |
| 118 | +2. **No Data Loss:** New tables are being added, no existing data is modified |
| 119 | +3. **Backward Compatible:** The changes are additive only |
| 120 | + |
| 121 | +## Prevention |
| 122 | + |
| 123 | +To prevent schema drift in the future: |
| 124 | +1. Always update **both** schema files when adding new models |
| 125 | +2. Run validation on both schemas in CI/CD |
| 126 | +3. Consider using a single schema with conditional configuration |
| 127 | +4. Add a pre-commit hook to check schema parity |
| 128 | + |
| 129 | +## Commit |
| 130 | + |
| 131 | +- **Hash:** `6ac6cc6` |
| 132 | +- **Message:** Fix Vercel deployment: Add missing AuditLog and InventoryLog models to PostgreSQL schema |
| 133 | +- **Files Changed:** `prisma/schema.postgres.prisma` (+55 lines) |
0 commit comments