Skip to content

fix: reset db to fix P2002 error and enhance setupDB.sh with nice CLI #751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sreecharan-desu
Copy link

Improved Database Setup CLI with P2002 Fix

Summary

This PR updates the local database setup experience by:

  • Fixing a PrismaClientKnownRequestError (P2002) when running seed.ts.
  • Rewriting setupDB.sh into a cleaner, interactive CLI inspired by Vite’s setup process.

Fix: Prisma P2002 Error

The seed script was failing with a P2002 error when trying to insert duplicate categories. To handle this:

  • The script now resets the database using npx prisma migrate reset --force --skip-seed before applying migrations.
  • A confirmation prompt is added to prevent accidental data loss.
  • This approach is intended for development use only, where full resets are acceptable.

New CLI Experience (setupDB.sh)

The old shell script was functional but clunky. It’s been upgraded to feel like a modern CLI:

  • Interactive Menus: Use of select for choosing DB type, local/cloud setup, Docker options, username input, and reset confirmation.
  • Visual Feedback: Progress bars (#) simulate progress during reset, migration, seeding, and Docker startup.
  • Better UX: Clears terminal, masks sensitive inputs, shows next steps, and logs all output to /tmp/setupdb.log.
  • Failsafe Handling: Gracefully manages errors like invalid input, Docker issues, and .env write failures.
  • Bash & Zsh Compatibility: Works across common shells and falls back in minimal terminals (e.g., CI/CD or Windows fallback).

Screenshot

Here’s a preview of the improved CLI interface:
Screenshot From 2025-06-13 16-15-44


Testing Performed

  • Docker Setup: Ran full reset, migration, and seeding flows without errors.
  • Terminal Output: Verified colors, symbols, progress bars, and menus in Bash and Zsh.
  • Prisma Studio: Confirmed seeded categories after running.
  • Cloud/Local DB Support: Checked flow using both local Postgres and external URIs.
  • Edge Case Handling:
    • Wrong or missing .env entries
    • Canceled reset
    • Docker errors
  • Idempotency: Ran multiple times — setup stays consistent.
  • Logs: Confirmed log output captured correctly in /tmp/setupdb.log.

How to Try It

  1. Run:
     chmod +x setupDB.sh
    ./setupDB.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant