# Database Scripts

This directory contains utility scripts for managing your database.

## Available Scripts

### Drop Tables Script (`drop-tables.js`)

Drops all tables from the database in the correct order to respect foreign key constraints.

**Usage:**
```bash
# Using npm scripts (recommended)
npm run db:drop          # Uses .env.local
npm run db:drop:dev      # Uses .env.development  
npm run db:drop:prod     # Uses .env.production

# Direct execution
node scripts/drop-tables.js
```

**What it does:**
- Drops all forum tables (posts, comments, reactions, etc.)
- Drops all auth tables (user, session, account, etc.)
- Drops all organization tables (organization, member, invitation)
- Drops all custom enums (reaction_type, post_status, etc.)
- Optionally drops drizzle migration tracking schema

### Reset Database Script (`reset-database.js`)

Performs a complete database reset: drops tables, clears migrations, generates fresh migrations, and applies them.

**Usage:**
```bash
# Using npm scripts (recommended)
npm run db:reset         # Uses .env.local
npm run db:reset:dev     # Uses .env.development
npm run db:reset:prod    # Uses .env.production

# Direct execution
node scripts/reset-database.js
```

**What it does:**
1. Drops all database tables
2. Clears local migration files
3. Generates fresh migrations from current schema
4. Applies migrations to database

## Environment Files

Make sure you have the appropriate environment file with your `DATABASE_URL`:

- `.env.local` - Local development
- `.env.development` - Development environment
- `.env.production` - Production environment

## Safety Notes

⚠️ **WARNING**: These scripts will **permanently delete all data** in your database. Use with caution!

- Always backup your data before running these scripts
- Test on development/staging environments first
- Never run on production without proper backups

## Common Workflows

### Complete Database Reset
```bash
npm run db:reset
```

### Just Drop Tables (keep migration history)
```bash
npm run db:drop
```

### Generate and Apply Migrations After Schema Changes
```bash
npm run db:generate
npm run db:migrate
```

### View Database in Drizzle Studio
```bash
npm run db:studio
```

## Troubleshooting

### "Command not found: env-cmd"
```bash
npm install -g env-cmd
# or
npx env-cmd -f .env.local node scripts/drop-tables.js
```

### "DATABASE_URL not found"
Make sure your environment file exists and contains the `DATABASE_URL` variable.

### "Permission denied"
```bash
chmod +x scripts/*.js
```
