|
| 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) |
0 commit comments