Skip to content

Commit a6ce976

Browse files
Phase 5 complete: Testing, validation, and documentation updates
Co-authored-by: rezwana-karim <126201034+rezwana-karim@users.noreply.github.com>
1 parent fd58ba8 commit a6ce976

File tree

6 files changed

+355
-3
lines changed

6 files changed

+355
-3
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,42 @@ export $(cat .env.local | xargs) && npm run prisma:migrate:dev
7373
npm run dev
7474
```
7575

76+
## 🚀 Deployment
77+
78+
### Deploy to Vercel
79+
80+
1. **Prepare Database**: Set up a PostgreSQL database (Vercel Postgres, Supabase, Neon, or Railway)
81+
82+
2. **Configure Environment Variables** in Vercel:
83+
- `DATABASE_URL` - PostgreSQL connection string
84+
- `NEXTAUTH_SECRET` - Generate with `openssl rand -base64 32`
85+
- `NEXTAUTH_URL` - Your production URL
86+
- `EMAIL_FROM` - Sender email address
87+
- `RESEND_API_KEY` - Email service API key
88+
89+
3. **Deploy**:
90+
```bash
91+
# Via GitHub (recommended)
92+
# Push to GitHub and import in Vercel dashboard
93+
94+
# Or via CLI
95+
vercel --prod
96+
```
97+
98+
4. **Run Migrations**:
99+
```bash
100+
vercel env pull .env.local
101+
npm run prisma:migrate:deploy
102+
```
103+
104+
See [VERCEL_DEPLOYMENT.md](./docs/VERCEL_DEPLOYMENT.md) for detailed instructions.
105+
76106
## 📚 Documentation
77107

78-
See [TASK.md](./TASK.md) and [.github/copilot-instructions.md](./.github/copilot-instructions.md) for detailed implementation guidance.
108+
- [Deployment Guide](./docs/VERCEL_DEPLOYMENT.md) - Deploy to Vercel with PostgreSQL
109+
- [PostgreSQL Migration Guide](./docs/POSTGRESQL_MIGRATION.md) - Migrate from SQLite to PostgreSQL
110+
- [Development Guide](./TASK.md) - Implementation guidance
111+
- [Copilot Instructions](./.github/copilot-instructions.md) - Detailed project structure
79112

80113
For Next.js 16 specifics, see official documentation at https://nextjs.org/docs
81114

docs/POSTGRESQL_MIGRATION.md

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# PostgreSQL Migration Guide
2+
3+
This guide explains how to migrate your StormCom database from SQLite (development) to PostgreSQL (production).
4+
5+
## Overview
6+
7+
StormCom uses:
8+
- **SQLite** for local development (`schema.sqlite.prisma`)
9+
- **PostgreSQL** for production deployment (`schema.postgres.prisma`)
10+
11+
Both schemas are kept in sync to ensure consistency between environments.
12+
13+
## Prerequisites
14+
15+
- PostgreSQL database set up (see [VERCEL_DEPLOYMENT.md](./VERCEL_DEPLOYMENT.md))
16+
- Access to your SQLite database (if migrating data)
17+
- `DATABASE_URL` environment variable configured
18+
19+
## Migration Options
20+
21+
### Option 1: Fresh Start (Recommended for New Deployments)
22+
23+
If you're deploying for the first time or don't need to preserve existing data:
24+
25+
1. **Set up PostgreSQL connection**:
26+
```bash
27+
export DATABASE_URL="postgresql://user:pass@host:5432/database?sslmode=require"
28+
```
29+
30+
2. **Run migrations**:
31+
```bash
32+
npm run prisma:migrate:deploy
33+
```
34+
35+
3. **Seed initial data** (optional):
36+
```bash
37+
npm run db:seed
38+
```
39+
40+
### Option 2: Data Migration (Preserve Existing Data)
41+
42+
If you need to migrate existing data from SQLite to PostgreSQL:
43+
44+
#### Step 1: Export SQLite Data
45+
46+
There are several approaches:
47+
48+
**A. Using Prisma Studio**
49+
```bash
50+
# Open Prisma Studio for SQLite
51+
npx prisma studio --schema=prisma/schema.sqlite.prisma
52+
# Manually export data as needed
53+
```
54+
55+
**B. Using SQL Dump**
56+
```bash
57+
sqlite3 prisma/dev.db .dump > data-dump.sql
58+
# Note: SQL syntax differences may require manual editing
59+
```
60+
61+
**C. Using a Node.js Script (Recommended)**
62+
63+
Create `scripts/migrate-data.js`:
64+
65+
```javascript
66+
const { PrismaClient: PrismaClientSqlite } = require('@prisma/client');
67+
const { PrismaClient: PrismaClientPostgres } = require('@prisma/client');
68+
69+
// Generate Prisma clients for both databases
70+
// First run: npx prisma generate --schema=prisma/schema.sqlite.prisma --generator client --output=./node_modules/.prisma/client-sqlite
71+
// Then run: npx prisma generate --schema=prisma/schema.postgres.prisma --generator client --output=./node_modules/.prisma/client-postgres
72+
73+
const sqlite = new PrismaClientSqlite();
74+
const postgres = new PrismaClientPostgres();
75+
76+
async function migrateData() {
77+
try {
78+
console.log('Starting data migration...');
79+
80+
// Migrate Users
81+
const users = await sqlite.user.findMany();
82+
for (const user of users) {
83+
await postgres.user.upsert({
84+
where: { id: user.id },
85+
update: user,
86+
create: user,
87+
});
88+
}
89+
console.log(`Migrated ${users.length} users`);
90+
91+
// Migrate Organizations
92+
const orgs = await sqlite.organization.findMany();
93+
for (const org of orgs) {
94+
await postgres.organization.upsert({
95+
where: { id: org.id },
96+
update: org,
97+
create: org,
98+
});
99+
}
100+
console.log(`Migrated ${orgs.length} organizations`);
101+
102+
// Continue for other models...
103+
// Add memberships, projects, stores, products, orders, etc.
104+
105+
console.log('Migration completed successfully!');
106+
} catch (error) {
107+
console.error('Migration failed:', error);
108+
} finally {
109+
await sqlite.$disconnect();
110+
await postgres.$disconnect();
111+
}
112+
}
113+
114+
migrateData();
115+
```
116+
117+
#### Step 2: Initialize PostgreSQL Schema
118+
119+
```bash
120+
# Set PostgreSQL DATABASE_URL
121+
export DATABASE_URL="postgresql://user:pass@host:5432/database"
122+
123+
# Run migrations
124+
npm run prisma:migrate:deploy
125+
```
126+
127+
#### Step 3: Run Data Migration
128+
129+
```bash
130+
# Run your migration script
131+
node scripts/migrate-data.js
132+
```
133+
134+
#### Step 4: Verify Migration
135+
136+
```bash
137+
# Open Prisma Studio for PostgreSQL
138+
npx prisma studio --schema=prisma/schema.postgres.prisma
139+
140+
# Verify all data is present and correct
141+
```
142+
143+
## Schema Differences
144+
145+
Both schemas are functionally identical. The only differences are:
146+
147+
1. **Provider**: `sqlite` vs `postgresql`
148+
2. **Connection String Format**:
149+
- SQLite: `file:./dev.db`
150+
- PostgreSQL: `postgresql://user:pass@host:5432/database`
151+
152+
## Environment-Specific Commands
153+
154+
### Development (SQLite)
155+
```bash
156+
# Generate Prisma Client
157+
npm run prisma:generate
158+
159+
# Create migration
160+
npm run prisma:migrate:dev
161+
162+
# Open Prisma Studio
163+
npx prisma studio --schema=prisma/schema.sqlite.prisma
164+
```
165+
166+
### Production (PostgreSQL)
167+
```bash
168+
# Generate Prisma Client
169+
npm run prisma:generate:postgres
170+
171+
# Deploy migrations (no interactive prompts)
172+
npm run prisma:migrate:deploy
173+
174+
# Open Prisma Studio
175+
npx prisma studio --schema=prisma/schema.postgres.prisma
176+
```
177+
178+
## Troubleshooting
179+
180+
### Error: "Prisma Client not generated"
181+
182+
**Solution**: Generate the correct client for your database:
183+
```bash
184+
# For PostgreSQL
185+
npm run prisma:generate:postgres
186+
187+
# For SQLite
188+
npm run prisma:generate
189+
```
190+
191+
### Error: "Migration already applied"
192+
193+
**Solution**: Use `prisma migrate deploy` instead of `prisma migrate dev` in production.
194+
195+
### Error: "Column type mismatch"
196+
197+
**Cause**: SQLite and PostgreSQL handle some types differently.
198+
199+
**Solution**: Ensure your schema files are in sync. Check:
200+
- String lengths (Text vs VARCHAR)
201+
- DateTime formats
202+
- JSON field handling
203+
204+
### Data Loss During Migration
205+
206+
**Prevention**:
207+
1. Always backup your SQLite database before migration
208+
2. Test migration on a copy of your data first
209+
3. Verify data in PostgreSQL before deleting SQLite database
210+
4. Keep SQLite database as backup until production is stable
211+
212+
## Best Practices
213+
214+
1. **Keep Schemas in Sync**: When modifying the schema, update both files
215+
2. **Test Migrations**: Always test on a staging database first
216+
3. **Backup Before Deploy**: Back up production database before running migrations
217+
4. **Use Transactions**: Wrap data migrations in transactions when possible
218+
5. **Incremental Migration**: For large datasets, migrate in batches
219+
220+
## Rollback Strategy
221+
222+
If you need to rollback:
223+
224+
1. **Restore Database Backup**:
225+
```bash
226+
# Restore from backup (provider-specific)
227+
psql -U user -d database < backup.sql
228+
```
229+
230+
2. **Revert Migrations**:
231+
```bash
232+
# Create a new migration that reverses changes
233+
npx prisma migrate dev --name revert_previous_migration --schema=prisma/schema.postgres.prisma
234+
```
235+
236+
3. **Switch Back to SQLite** (development only):
237+
```bash
238+
export DATABASE_URL="file:./dev.db"
239+
npm run dev
240+
```
241+
242+
## Production Checklist
243+
244+
Before deploying to production:
245+
246+
- [ ] PostgreSQL database provisioned and accessible
247+
- [ ] Environment variables configured in Vercel/hosting platform
248+
- [ ] Schema validated with `npx prisma validate`
249+
- [ ] Migrations tested on staging database
250+
- [ ] Backup strategy in place
251+
- [ ] Data migration completed (if applicable)
252+
- [ ] Connection pooling configured
253+
- [ ] Database backups scheduled
254+
- [ ] Monitoring and logging set up
255+
256+
## Additional Resources
257+
258+
- [Prisma PostgreSQL Documentation](https://www.prisma.io/docs/concepts/database-connectors/postgresql)
259+
- [Prisma Migrate Guide](https://www.prisma.io/docs/concepts/components/prisma-migrate)
260+
- [SQLite to PostgreSQL Migration Tools](https://pgloader.io/)
261+
- [Vercel Postgres Documentation](https://vercel.com/docs/storage/vercel-postgres)
262+
263+
## Support
264+
265+
For issues specific to StormCom:
266+
- GitHub Issues: [rezwana-karim/stormcom/issues](https://github.com/rezwana-karim/stormcom/issues)
267+
- Project Documentation: [README.md](../README.md)
268+
269+
For Prisma-specific issues:
270+
- Prisma Documentation: [prisma.io/docs](https://prisma.io/docs)
271+
- Prisma Community: [slack.prisma.io](https://slack.prisma.io)

eslint.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const eslintConfig = defineConfig([
1212
"out/**",
1313
"build/**",
1414
"next-env.d.ts",
15+
// Custom ignores:
16+
"scripts/**/*.js",
17+
"scripts/**/*.sh",
1518
]),
1619
]);
1720

scripts/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22

33
This directory contains utility scripts for the StormCom project.
44

5-
## collect-type-errors.ps1
5+
## Build and Deployment Scripts
6+
7+
### build.js / build.sh
8+
9+
Intelligent build scripts that detect the database type from `DATABASE_URL` and generate the appropriate Prisma schema before building Next.js.
10+
11+
**Features:**
12+
- Auto-detects PostgreSQL vs SQLite from `DATABASE_URL`
13+
- Generates correct Prisma Client for the environment
14+
- Builds Next.js application
15+
- Cross-platform support (Node.js and Bash versions)
16+
17+
**Usage:**
18+
```bash
19+
npm run build
20+
```
21+
22+
**Environment Detection:**
23+
- `postgresql://` or `postgres://` → Uses `schema.postgres.prisma`
24+
- `file:` → Uses `schema.sqlite.prisma`
25+
- Unknown → Defaults to PostgreSQL schema
26+
27+
### postinstall.js / postinstall.sh
28+
29+
Runs automatically after `npm install` to generate the appropriate Prisma Client based on environment.
30+
31+
**Features:**
32+
- Auto-runs on `npm install`
33+
- Detects database type from `DATABASE_URL`
34+
- Generates Prisma Client automatically
35+
- Graceful failure (won't break install if DATABASE_URL is missing)
36+
37+
**Manual Usage:**
38+
```bash
39+
node scripts/postinstall.js
40+
# or
41+
bash scripts/postinstall.sh
42+
```
43+
44+
## Development Scripts
45+
46+
### collect-type-errors.ps1
647

748
PowerShell script that runs TypeScript type checking and saves all errors to a JSON file.
849

scripts/build.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Build script for Next.js with dynamic Prisma schema selection (Node.js version)
33
// This script determines which Prisma schema to use based on DATABASE_URL
44

5+
/* eslint-disable @typescript-eslint/no-require-imports */
56
const { execSync } = require('child_process');
67
const path = require('path');
8+
/* eslint-enable @typescript-eslint/no-require-imports */
79

810
console.log('🔧 Starting build process...');
911

0 commit comments

Comments
 (0)