db consolidation done

This commit is contained in:
echo 2025-11-26 00:54:27 +01:00
parent bf532ba35d
commit 684481fd2c
7 changed files with 226 additions and 3 deletions

Binary file not shown.

View File

@ -1,7 +1,7 @@
const Database = require('better-sqlite3');
const path = require('path');
const dbPath = path.join(__dirname, '../../../data/fitai.db');
const dbPath = path.join(__dirname, '../data/fitai.db');
const db = new Database(dbPath);
console.log('Migrating fitness_profiles table...');

View File

@ -1,5 +1,5 @@
export const API_BASE_URL = __DEV__
? 'https://0ccbc9f6f846.ngrok-free.app'
? 'https://221260dc4a2b.ngrok-free.app'
: 'https://your-production-url.com'
export const API_ENDPOINTS = {

217
docs/DATABASE_SETUP.md Normal file
View File

@ -0,0 +1,217 @@
# Database Setup Guide
## Database Location
**Primary Database**: `apps/admin/data/fitai.db`
This is the single source of truth for all application data. All API routes, migration scripts, and database operations use this file.
## Database Path Configuration
### Database Package (`@fitai/database`)
The database package supports configurable paths via environment variables:
```typescript
// Default behavior:
// - Development: ../../apps/admin/data/fitai.db
// - Production: ./data/fitai.db
// - Custom: Set DATABASE_URL environment variable
```
**Environment Variable**:
```bash
# Optional: Override default database path
DATABASE_URL=/path/to/your/fitai.db
```
### Admin App
The admin app uses the following database path configuration:
```typescript
// Default: ./data/fitai.db (relative to apps/admin/)
filename: './data/fitai.db'
```
All API routes and scripts use this consistent path.
## Migration Scripts
All migration scripts are located in `apps/admin/scripts/` and use the correct database path:
- `seed-superadmin.js` - Creates super admin user
- `migrate-roles.js` - Adds role column to users table
- `migrate-fitness-profile.js` - Adds allergies and injuries columns
- `migrate-last-visit.js` - Adds lastVisit column to clients table
**Running Migrations**:
```bash
cd apps/admin
node scripts/migrate-fitness-profile.js
node scripts/migrate-roles.js
node scripts/migrate-last-visit.js
```
## Drizzle ORM
Drizzle Kit is configured to use the admin database:
```typescript
// packages/database/drizzle.config.ts
dbCredentials: {
url: "../../apps/admin/data/fitai.db"
}
```
**Drizzle Commands**:
```bash
cd packages/database
# Push schema changes to database
npm run db:push
# Open Drizzle Studio (database GUI)
npm run db:studio
```
## Backup and Restore
### Creating a Backup
```bash
# From project root
cp apps/admin/data/fitai.db backups/fitai-$(date +%Y%m%d-%H%M%S).db
```
### Restoring from Backup
```bash
# From project root
cp backups/fitai-YYYYMMDD-HHMMSS.db apps/admin/data/fitai.db
```
## Database Schema
The database uses SQLite with the following main tables:
- `users` - User accounts (admin, trainer, client)
- `clients` - Client profiles linked to users
- `fitness_profiles` - Fitness goals, preferences, and health info
- `payments` - Payment records
- `attendance` - Check-in/check-out records
- `notifications` - User notifications
- `recommendations` - AI-generated fitness recommendations
## Troubleshooting
### Database Not Found Error
If you see "database not found" errors:
1. Ensure you're running commands from the correct directory
2. Check that `apps/admin/data/fitai.db` exists
3. Verify file permissions (should be readable/writable)
### Multiple Database Files
**There should only be ONE database file**: `apps/admin/data/fitai.db`
If you find other `fitai.db` files:
- `data/fitai.db` (root level) - Old duplicate, should not exist
- `packages/database/fitai.db` - Old duplicate, should not exist
These were consolidated on 2025-11-26. Backups are in `backups/database-consolidation-2025-11-26/`.
### Migration Script Errors
If migration scripts fail:
1. Check that you're in the `apps/admin` directory
2. Verify the database file exists and is writable
3. Check the script output for specific error messages
4. Ensure no other process has the database locked
## Development Workflow
### Starting Fresh
```bash
# Remove existing database
rm apps/admin/data/fitai.db
# Run migrations to create tables
cd packages/database
npm run db:push
# Seed super admin user
cd ../../apps/admin
node scripts/seed-superadmin.js
```
### Viewing Database Contents
**Option 1: Drizzle Studio (Recommended)**
```bash
cd packages/database
npm run db:studio
# Opens at http://localhost:4983
```
**Option 2: SQLite CLI**
```bash
cd apps/admin/data
sqlite3 fitai.db
# Then run SQL queries
SELECT * FROM users;
.exit
```
**Option 3: VS Code Extension**
Install "SQLite Viewer" extension and open `apps/admin/data/fitai.db`
## Production Deployment
### Environment Variables
Set the following in production:
```bash
# Optional: Custom database path
DATABASE_URL=/var/data/fitai.db
# Set production mode
NODE_ENV=production
```
### Database Location in Production
Ensure the database directory exists and has proper permissions:
```bash
mkdir -p /path/to/data
chmod 755 /path/to/data
```
### Backup Strategy
Implement automated backups in production:
```bash
# Daily backup cron job
0 2 * * * cp /var/data/fitai.db /var/backups/fitai-$(date +\%Y\%m\%d).db
```
## Security Considerations
1. **Never commit the database file to git** - It's in `.gitignore`
2. **Protect database file permissions** - Only application should have access
3. **Regular backups** - Implement automated backup strategy
4. **Encryption at rest** - Consider encrypting the database file in production
5. **Access control** - Ensure only authorized users can access the database
## Related Documentation
- [Clerk Webhook Setup](./CLERK_WEBHOOK_SETUP.md) - User synchronization
- [Testing Fitness Profile](./TESTING_FITNESS_PROFILE.md) - Feature testing
- [Webhook Database Fix](./WEBHOOK_DATABASE_FIX.md) - Webhook implementation details

View File

@ -2,7 +2,13 @@ import Database from 'better-sqlite3'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import * as schema from './schema'
const sqlite = new Database('./fitai.db')
// Configurable database path with intelligent defaults
const dbPath = process.env.DATABASE_URL ||
(process.env.NODE_ENV === 'production'
? './data/fitai.db'
: '../../apps/admin/data/fitai.db')
const sqlite = new Database(dbPath)
export const db = drizzle(sqlite, { schema })
export * from './schema'